9
9
using OpenSvip . Framework ;
10
10
using System . Threading ;
11
11
using System . Diagnostics ;
12
+ using System . Reflection ;
12
13
using Microsoft . WindowsAPICodePack . Dialogs ;
13
14
using System . Windows . Input ;
14
15
using OpenSvip . GUI . Config ;
@@ -197,13 +198,38 @@ private void ExecuteTasks()
197
198
}
198
199
new Thread ( ( ) =>
199
200
{
201
+ // Prepare for execution
200
202
Model . ExecutionInProgress = true ;
201
- var inputConverter = PluginManager . GetConverter ( Model . SelectedInputPlugin . Identifier ) ;
202
- var outputConverter = PluginManager . GetConverter ( Model . SelectedOutputPlugin . Identifier ) ;
203
203
foreach ( var task in Model . TaskList )
204
204
{
205
205
task . PrepareForExecution ( ) ;
206
206
}
207
+
208
+ // Construct options
209
+ var inputOptionDictionary = new Dictionary < string , string > ( ) ;
210
+ foreach ( var option in Model . SelectedInputOptions )
211
+ {
212
+ inputOptionDictionary [ option . OptionInfo . Name ] = option . OptionValue ;
213
+ }
214
+ var outputOptionDictionary = new Dictionary < string , string > ( ) ;
215
+ foreach ( var option in Model . SelectedOutputOptions )
216
+ {
217
+ outputOptionDictionary [ option . OptionInfo . Name ] = option . OptionValue ;
218
+ }
219
+ var inputOptions = new ConverterOptions ( inputOptionDictionary ) ;
220
+ var outputOptions = new ConverterOptions ( outputOptionDictionary ) ;
221
+
222
+ // Create sandbox for tasks
223
+ var domain = AppDomain . CreateDomain ( "TaskExecution" ) ;
224
+ var container = ( TaskContainer ) domain . CreateInstanceAndUnwrap (
225
+ Assembly . GetAssembly ( typeof ( TaskContainer ) ) . FullName ,
226
+ typeof ( TaskContainer ) . ToString ( ) ) ;
227
+ container . Init (
228
+ Model . SelectedInputPlugin ,
229
+ Model . SelectedOutputPlugin ,
230
+ inputOptions ,
231
+ outputOptions ) ;
232
+
207
233
var skipSameFilename = Model . OverWriteOption == OverwriteOptions . Skip ;
208
234
var askBeforeOverwrite = Model . OverWriteOption == OverwriteOptions . Ask ;
209
235
foreach ( var task in Model . TaskList )
@@ -228,45 +254,34 @@ private void ExecuteTasks()
228
254
continue ;
229
255
}
230
256
}
231
-
232
- var inputOptionDictionary = new Dictionary < string , string > ( ) ;
233
- foreach ( var option in Model . SelectedInputOptions )
234
- {
235
- inputOptionDictionary [ option . OptionInfo . Name ] = option . OptionValue ;
236
- }
237
- var outputOptionDictionary = new Dictionary < string , string > ( ) ;
238
- foreach ( var option in Model . SelectedOutputOptions )
239
- {
240
- outputOptionDictionary [ option . OptionInfo . Name ] = option . OptionValue ;
241
- }
242
- outputConverter . Save (
243
- task . ExportPath ,
244
- inputConverter . Load (
245
- task . ImportPath ,
246
- new ConverterOptions ( inputOptionDictionary ) ) ,
247
- new ConverterOptions ( outputOptionDictionary ) ) ;
257
+ // Run the container
258
+ container . Run ( task . ImportPath , task . ExportPath ) ;
248
259
}
249
260
catch ( Exception e )
250
261
{
251
262
task . Status = TaskStates . Error ;
252
263
task . Error = e . Message ;
253
264
continue ;
254
265
}
255
- var warnings = Warnings . GetWarnings ( ) ;
266
+ var warnings = container . GetWarnings ( ) ;
256
267
if ( warnings . Any ( ) )
257
268
{
258
269
task . Status = TaskStates . Warning ;
259
270
foreach ( var warning in warnings )
260
271
{
261
272
task . Warnings . Add ( warning ) ;
262
273
}
263
- Warnings . ClearWarnings ( ) ;
274
+ container . ClearWarnings ( ) ;
264
275
}
265
276
else
266
277
{
267
278
task . Status = TaskStates . Success ;
268
279
}
269
280
}
281
+ // Unload the domain to release assembly files
282
+ AppDomain . Unload ( domain ) ;
283
+
284
+ // Things after execution
270
285
Model . ExecutionInProgress = false ;
271
286
if ( ! Model . OpenExportFolder )
272
287
{
@@ -279,27 +294,27 @@ private void ExecuteTasks()
279
294
} ) . Start ( ) ;
280
295
}
281
296
282
- public static RelayCommand < MainWindow > ImportCommand = new RelayCommand < MainWindow > (
297
+ public static readonly RelayCommand < MainWindow > ImportCommand = new RelayCommand < MainWindow > (
283
298
p => ! p . Model . ExecutionInProgress ,
284
299
p => p . FileMaskPanel_Click ( null , null ) ) ;
285
300
286
- public static RelayCommand < MainWindow > ExportCommand = new RelayCommand < MainWindow > (
301
+ public static readonly RelayCommand < MainWindow > ExportCommand = new RelayCommand < MainWindow > (
287
302
p => p . StartExecutionButton . IsEnabled ,
288
303
p => p . StartExecutionButton_Click ( null , null ) ) ;
289
304
290
- public static RelayCommand < MainWindow > BrowseAndExportCommand = new RelayCommand < MainWindow > (
305
+ public static readonly RelayCommand < MainWindow > BrowseAndExportCommand = new RelayCommand < MainWindow > (
291
306
p => p . StartExecutionButton . IsEnabled ,
292
307
p => p . BrowseAndExportMenu_Click ( null , null ) ) ;
293
308
294
- public static RelayCommand < AppModel > ResetCommand = new RelayCommand < AppModel > (
309
+ public static readonly RelayCommand < AppModel > ResetCommand = new RelayCommand < AppModel > (
295
310
p => ! p . ExecutionInProgress ,
296
311
p => p . TaskList . Clear ( ) ) ;
297
312
298
- public static RelayCommand < MainWindow > AboutCommand = new RelayCommand < MainWindow > (
313
+ public static readonly RelayCommand < MainWindow > AboutCommand = new RelayCommand < MainWindow > (
299
314
p => true ,
300
315
p => p . AboutMenuItem_Click ( null , null ) ) ;
301
316
302
- public static RelayCommand < System . Windows . Controls . MenuItem > ImportPluginMenuItemCommand = new RelayCommand < System . Windows . Controls . MenuItem > (
317
+ public static readonly RelayCommand < System . Windows . Controls . MenuItem > ImportPluginMenuItemCommand = new RelayCommand < System . Windows . Controls . MenuItem > (
303
318
p =>
304
319
{
305
320
var model = ( AppModel ) p . DataContext ;
@@ -317,7 +332,7 @@ private void ExecuteTasks()
317
332
( ( MainWindow ) App . Current . MainWindow ) . Model . SelectedInputPluginIndex = index ;
318
333
} ) ;
319
334
320
- public static RelayCommand < System . Windows . Controls . MenuItem > ExportPluginMenuItemCommand = new RelayCommand < System . Windows . Controls . MenuItem > (
335
+ public static readonly RelayCommand < System . Windows . Controls . MenuItem > ExportPluginMenuItemCommand = new RelayCommand < System . Windows . Controls . MenuItem > (
321
336
p => true ,
322
337
p =>
323
338
{
@@ -331,7 +346,7 @@ private void ExecuteTasks()
331
346
( ( MainWindow ) App . Current . MainWindow ) . Model . SelectedOutputPluginIndex = index ;
332
347
} ) ;
333
348
334
- public static RelayCommand < AppModel > InstallPluginCommand = new RelayCommand < AppModel > (
349
+ public static readonly RelayCommand < AppModel > InstallPluginCommand = new RelayCommand < AppModel > (
335
350
p => ! p . ExecutionInProgress ,
336
351
p =>
337
352
{
@@ -419,7 +434,7 @@ private void ExecuteTasks()
419
434
} ) . Start ( ) ;
420
435
} ) ;
421
436
422
- public static RelayCommand < AppModel > ManagePathsCommand = new RelayCommand < AppModel > (
437
+ public static readonly RelayCommand < AppModel > ManagePathsCommand = new RelayCommand < AppModel > (
423
438
p => ! p . ExecutionInProgress ,
424
439
p => PathManagerDialog . CreateDialog ( p ) . ShowDialog ( ) ) ;
425
440
0 commit comments