2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
4
using System . Linq ;
5
+ using System . Threading ;
5
6
6
7
namespace GPUPrefSwitcher
7
8
{
@@ -26,7 +27,13 @@ public List<AppEntry> CurrentAppEntries
26
27
27
28
public void RevertAppEntriesToPrevious ( )
28
29
{
29
- currentAppEntries = DeepCopyAppEntries ( prevAppEntries ) ;
30
+ try
31
+ {
32
+ currentAppEntries = DeepCopyAppEntries ( prevAppEntries ) ;
33
+ } finally
34
+ {
35
+ semaphoreSlim . Wait ( ) ;
36
+ }
30
37
}
31
38
private static List < AppEntry > DeepCopyAppEntries ( List < AppEntry > appEntries )
32
39
{
@@ -36,6 +43,7 @@ private static List<AppEntry> DeepCopyAppEntries(List<AppEntry> appEntries)
36
43
return newList ;
37
44
}
38
45
46
+ private SemaphoreSlim semaphoreSlim ;
39
47
40
48
internal readonly PreferencesXML PreferencesXML ;
41
49
@@ -44,28 +52,37 @@ public AppEntrySaveHandler()
44
52
PreferencesXML = new PreferencesXML ( ) ;
45
53
prevAppEntries = PreferencesXML . GetAppEntries ( ) ;
46
54
currentAppEntries = PreferencesXML . GetAppEntries ( ) ;
55
+ semaphoreSlim = new SemaphoreSlim ( 1 ) ;
47
56
}
48
57
49
58
public void ChangeAppEntryByPath ( string path , AppEntry updatedAppEntry )
50
59
{
51
- int index = CurrentAppEntries . IndexOf ( CurrentAppEntries . Single ( x => x . AppPath == path ) ) ;
52
-
53
- /* //for-loop alternative, but the above should throw an error with an obvious enough meaning
54
- int index = -1;
55
- for (int i = 0; i < CurrentAppEntries.Count; i++)
60
+ try
56
61
{
57
- AppEntry appEntry = CurrentAppEntries[i];
58
- if (appEntry.AppPath == path)
62
+ semaphoreSlim . Wait ( ) ;
63
+
64
+ int index = CurrentAppEntries . IndexOf ( CurrentAppEntries . Single ( x => x . AppPath == path ) ) ;
65
+
66
+ /* //for-loop alternative, but the above should throw an error with an obvious enough meaning
67
+ int index = -1;
68
+ for (int i = 0; i < CurrentAppEntries.Count; i++)
59
69
{
60
- index = i; break;
70
+ AppEntry appEntry = CurrentAppEntries[i];
71
+ if (appEntry.AppPath == path)
72
+ {
73
+ index = i; break;
74
+ }
61
75
}
62
- }
63
76
64
- if(index<0)
65
- throw new AppEntrySaverException($"UpdateAppEntry: No AppEntry with path {path} was found");
66
- */
77
+ if(index<0)
78
+ throw new AppEntrySaverException($"UpdateAppEntry: No AppEntry with path {path} was found");
79
+ */
67
80
68
- CurrentAppEntries [ index ] = updatedAppEntry ;
81
+ CurrentAppEntries [ index ] = updatedAppEntry ;
82
+ } finally
83
+ {
84
+ semaphoreSlim . Release ( ) ;
85
+ }
69
86
}
70
87
71
88
/*
@@ -83,103 +100,118 @@ public void ChangeAppEntryByPath(string path, AppEntry updatedAppEntry)
83
100
*/
84
101
public void SaveAppEntryChanges ( )
85
102
{
103
+ try
104
+ {
105
+ semaphoreSlim . Wait ( ) ;
86
106
87
- List < AppEntry > differences = new ( ) ;
88
- differences . AddRange ( currentAppEntries . Where ( entry => NotSameOrInPrevAppEntries ( entry ) ) ) ;
107
+ List < AppEntry > differences = new ( ) ;
108
+ differences . AddRange ( currentAppEntries . Where ( entry => NotSameOrInPrevAppEntries ( entry ) ) ) ;
89
109
90
- List < string > existingAppPaths = new List < string > ( from appEntry in PreferencesXML . GetAppEntries ( ) select appEntry . AppPath ) ;
91
- List < AppEntry > needToAdd = new ( ) ;
92
- needToAdd . AddRange ( currentAppEntries . Where ( entry => ! existingAppPaths . Contains ( entry . AppPath ) ) ) ;
110
+ List < string > existingAppPaths = new List < string > ( from appEntry in PreferencesXML . GetAppEntries ( ) select appEntry . AppPath ) ;
111
+ List < AppEntry > needToAdd = new ( ) ;
112
+ needToAdd . AddRange ( currentAppEntries . Where ( entry => ! existingAppPaths . Contains ( entry . AppPath ) ) ) ;
93
113
94
- //remove appentries whose *paths* exist in the prev but not the current
95
- List < AppEntry > needToRemoveFromXML = new ( ) ;
96
- foreach ( AppEntry entry in prevAppEntries )
97
- {
98
- if ( ! currentAppEntries . Exists ( a => a . AppPath == entry . AppPath ) )
114
+ //remove appentries whose *paths* exist in the prev but not the current
115
+ List < AppEntry > needToRemoveFromXML = new ( ) ;
116
+ foreach ( AppEntry entry in prevAppEntries )
99
117
{
100
- needToRemoveFromXML . Add ( entry ) ;
118
+ if ( ! currentAppEntries . Exists ( a => a . AppPath == entry . AppPath ) )
119
+ {
120
+ needToRemoveFromXML . Add ( entry ) ;
121
+ }
101
122
}
102
- }
103
123
104
- //new AppEntries should be added first so that ModifyAppEntry can actually find the AppEntry with the path
105
- foreach ( AppEntry appEntry in needToAdd )
106
- {
107
- PreferencesXML . AddAppEntryAndSave ( appEntry ) ;
108
- }
124
+ //new AppEntries should be added first so that ModifyAppEntry can actually find the AppEntry with the path
125
+ foreach ( AppEntry appEntry in needToAdd )
126
+ {
127
+ PreferencesXML . AddAppEntryAndSave ( appEntry ) ;
128
+ }
109
129
110
- foreach ( AppEntry appEntry in differences )
111
- {
112
- PreferencesXML . ModifyAppEntryAndSave ( appEntry . AppPath , appEntry ) ;
113
- }
130
+ foreach ( AppEntry appEntry in differences )
131
+ {
132
+ PreferencesXML . ModifyAppEntryAndSave ( appEntry . AppPath , appEntry ) ;
133
+ }
114
134
115
- foreach ( AppEntry appEntry in needToRemoveFromXML )
116
- {
117
- bool success = PreferencesXML . TryDeleteAppEntryAndSave ( appEntry . AppPath ) ;
118
- if ( ! success ) //this would probably only ever fail if AppEntries were deleted externally whilst the GUI or Service was still running
119
- throw new XMLHelperException ( $ "DeleteAppEntry: AppEntry with the specified path not found in data store: { appEntry . AppPath } ") ;
120
- }
135
+ foreach ( AppEntry appEntry in needToRemoveFromXML )
136
+ {
137
+ bool success = PreferencesXML . TryDeleteAppEntryAndSave ( appEntry . AppPath ) ;
138
+ if ( ! success ) //this would probably only ever fail if AppEntries were deleted externally whilst the GUI or Service was still running
139
+ throw new XMLHelperException ( $ "DeleteAppEntry: AppEntry with the specified path not found in data store: { appEntry . AppPath } ") ;
140
+ }
121
141
122
- prevAppEntries = DeepCopyAppEntries ( currentAppEntries ) ; //update the saved entries
142
+ prevAppEntries = DeepCopyAppEntries ( currentAppEntries ) ; //update the saved entries
123
143
124
- Logger . inst . Log ( "Concluded saving" ) ;
144
+ Logger . inst . Log ( "Concluded saving" ) ;
125
145
126
- bool NotSameOrInPrevAppEntries ( AppEntry appEntry )
146
+ bool NotSameOrInPrevAppEntries ( AppEntry appEntry )
147
+ {
148
+ return ! prevAppEntries . Contains ( appEntry ) ; //Contains uses Equals() which is implemented in AppEntry
149
+ }
150
+ } finally
127
151
{
128
- return ! prevAppEntries . Contains ( appEntry ) ; //Contains uses Equals() which is implemented in AppEntry
152
+ semaphoreSlim . Release ( ) ;
129
153
}
130
154
}
131
155
132
156
public bool AppEntriesHaveChangedFromLastSave ( )
133
157
{
134
- if ( prevAppEntries . SequenceEqual ( CurrentAppEntries ) )
135
- {
136
- return false ;
137
- }
138
- else
139
- {
140
-
141
- var diffs = currentAppEntries . Except ( prevAppEntries ) ;
142
- return true ; //move to end for debug info
143
- foreach ( AppEntry e in diffs )
158
+ try {
159
+
160
+ semaphoreSlim . Wait ( ) ;
161
+
162
+ if ( prevAppEntries . SequenceEqual ( CurrentAppEntries ) )
144
163
{
145
-
146
- Debug . WriteLine ( "CURRENT:" ) ;
147
- Debug . WriteLine ( e . ToString ( ) ) ;
148
-
149
- //WARNING: causes crash if Preferences.xml AppsList is empty
150
- var prev = prevAppEntries . Single ( x => x . AppPath == e . AppPath ) ;
151
- Debug . WriteLine ( "PREVIOUS:" ) ;
152
- Debug . WriteLine ( prev . ToString ( ) ) ;
153
-
154
- Debug . WriteLine ( e . GetHashCode ( ) ) ;
155
- Debug . WriteLine ( prev . GetHashCode ( ) ) ;
156
-
157
- Debug . WriteLine ( $ "DIFFERENT?: { ( e . Equals ( prev ) ? "no" : "yes" ) } ") ;
158
-
159
- /*
160
- Debug.WriteLine(e.EnableFileSwapper.GetHashCode());
161
- Debug.WriteLine(prev.EnableFileSwapper.GetHashCode());
162
- */
163
-
164
- /*
165
- Debug.WriteLine(e.AppPath.GetHashCode());
166
- Debug.WriteLine(prev.AppPath.GetHashCode());
167
- */
168
-
169
- /*
170
- Debug.WriteLine(e.FileSwapperPaths.GetHashCode());
171
- Debug.WriteLine(prev.FileSwapperPaths.GetHashCode());
172
- Debug.WriteLine(e.SwapperStates.GetHashCode());
173
- Debug.WriteLine(prev.SwapperStates.GetHashCode());
174
- */
175
-
176
- /*
177
- Debug.WriteLine(e.getStringArrHash(e.FileSwapperPaths));
178
- Debug.WriteLine(prev.getStringArrHash(prev.FileSwapperPaths));
179
- Debug.WriteLine(e.getStringArrHash(e.SwapperStates));
180
- Debug.WriteLine(prev.getStringArrHash(prev.SwapperStates));
181
- */
164
+ return false ;
182
165
}
166
+ else
167
+ {
168
+
169
+ var diffs = currentAppEntries . Except ( prevAppEntries ) ;
170
+ return true ; //move to end for debug info
171
+ foreach ( AppEntry e in diffs )
172
+ {
173
+
174
+ Debug . WriteLine ( "CURRENT:" ) ;
175
+ Debug . WriteLine ( e . ToString ( ) ) ;
176
+
177
+ //WARNING: causes crash if Preferences.xml AppsList is empty
178
+ var prev = prevAppEntries . Single ( x => x . AppPath == e . AppPath ) ;
179
+ Debug . WriteLine ( "PREVIOUS:" ) ;
180
+ Debug . WriteLine ( prev . ToString ( ) ) ;
181
+
182
+ Debug . WriteLine ( e . GetHashCode ( ) ) ;
183
+ Debug . WriteLine ( prev . GetHashCode ( ) ) ;
184
+
185
+ Debug . WriteLine ( $ "DIFFERENT?: { ( e . Equals ( prev ) ? "no" : "yes" ) } ") ;
186
+
187
+ /*
188
+ Debug.WriteLine(e.EnableFileSwapper.GetHashCode());
189
+ Debug.WriteLine(prev.EnableFileSwapper.GetHashCode());
190
+ */
191
+
192
+ /*
193
+ Debug.WriteLine(e.AppPath.GetHashCode());
194
+ Debug.WriteLine(prev.AppPath.GetHashCode());
195
+ */
196
+
197
+ /*
198
+ Debug.WriteLine(e.FileSwapperPaths.GetHashCode());
199
+ Debug.WriteLine(prev.FileSwapperPaths.GetHashCode());
200
+ Debug.WriteLine(e.SwapperStates.GetHashCode());
201
+ Debug.WriteLine(prev.SwapperStates.GetHashCode());
202
+ */
203
+
204
+ /*
205
+ Debug.WriteLine(e.getStringArrHash(e.FileSwapperPaths));
206
+ Debug.WriteLine(prev.getStringArrHash(prev.FileSwapperPaths));
207
+ Debug.WriteLine(e.getStringArrHash(e.SwapperStates));
208
+ Debug.WriteLine(prev.getStringArrHash(prev.SwapperStates));
209
+ */
210
+ }
211
+ }
212
+ } finally
213
+ {
214
+ semaphoreSlim . Release ( ) ;
183
215
}
184
216
}
185
217
0 commit comments