@@ -85,78 +85,80 @@ public void StopTask()
85
85
{
86
86
try
87
87
{
88
- Process . GetProcessById ( processIdArr [ i ] ) . Kill ( ) ;
89
- processIdArr [ i ] = 0 ;
88
+ if ( processIdArr [ i ] != - 1 )
89
+ {
90
+ Process . GetProcessById ( processIdArr [ i ] ) . Kill ( ) ;
91
+ }
92
+ processIdArr [ i ] = - 1 ;
90
93
}
91
94
catch ( System . ArgumentException e )
92
95
{
93
- Debug . WriteLine ( "Maybe the process [" + processIdArr [ i ] + "] isn't running. Exception message: {0}" , e . Message ) ;
96
+ Debug . WriteLine ( "Maybe the process [" + processIdArr [ i ] + "] isn't running. Exception message: " + e . Message ) ;
94
97
}
95
98
catch ( System . ComponentModel . Win32Exception e )
96
99
{
97
- MessageBox . Show ( "Can't kill the process [" + processIdArr [ i ] + "] . You can try again. Exception message: {0}" , e . Message ) ;
100
+ MessageBox . Show ( "Can't kill the process [" + processIdArr [ i ] + "] . You can try again. Exception message: " + e . Message ) ;
98
101
}
99
102
}
100
103
} ) ;
101
104
}
102
105
103
- public async Task NewThreadAsync ( int processNumber )
106
+ public async Task NewThreadAsync ( int processIDIndex )
104
107
{
105
108
while ( true )
106
109
{
107
- string inputFile = Dispatcher . Invoke ( ( ) =>
110
+ string inputFileName = Dispatcher . Invoke ( ( ) =>
108
111
{
109
112
if ( config . FilesList . Count > 0 )
110
113
{
111
- string firstFile = config . FilesList . First . Value ;
114
+ string firstFileName = config . FilesList . First . Value ;
112
115
config . FilesList . RemoveFirst ( ) ;
113
- return firstFile ;
116
+ return firstFileName ;
114
117
}
115
118
else
116
119
{
117
120
return null ;
118
121
}
119
122
} ) ;
120
123
121
- if ( inputFile == null )
124
+ if ( inputFileName == null )
122
125
{
123
- break ;
126
+ return ;
124
127
}
125
- else
126
- {
127
- string appArgs = SumAppArgs (
128
- argsTemplet : config . ArgsTemplet ,
129
- inputFile : inputFile ,
130
- userArgs : config . UserArgs ,
131
- outputSuffix : config . OutputSuffix ,
132
- outputExtension : config . OutputExtension ,
133
- outputFloder : config . OutputFloder ) ;
134
-
135
- await Task . Run ( ( ) =>
136
- {
137
- NewProcess (
138
- appPath : config . AppPath ,
139
- appArgs : appArgs ,
140
- windowStyle : config . WindowStyle ,
141
- priority : config . Priority ,
142
- simulateCmd : config . SimulateCmd ,
143
- processIndex : processNumber ) ;
144
- } ) ;
145
128
146
- config . CompletedFileNum ++ ;
129
+ string appArgs = SumAppArgs (
130
+ argsTemplet : config . ArgsTemplet ,
131
+ inputFileName : inputFileName ,
132
+ userArgs : config . UserArgs ,
133
+ outputSuffix : config . OutputSuffix ,
134
+ outputExtension : config . OutputExtension ,
135
+ outputFloder : config . OutputFloder ) ;
147
136
148
- Dispatcher . Invoke ( ( ) =>
149
- {
150
- SetProgress ( ( double ) config . CompletedFileNum / config . FilesSum ) ;
151
- } ) ;
152
- }
137
+
138
+ Process process = NewProcess (
139
+ appPath : config . AppPath ,
140
+ appArgs : appArgs ,
141
+ windowStyle : config . WindowStyle ,
142
+ priority : config . Priority ,
143
+ simulateCmd : config . SimulateCmd ) ;
144
+
145
+ processIdArr [ processIDIndex ] = process . Id ;
146
+
147
+ await Task . Run ( ( ) => process . WaitForExit ( ) ) ;
148
+
149
+ config . CompletedFileNumber ++ ;
150
+
151
+ Dispatcher . Invoke ( ( ) =>
152
+ {
153
+ SetProgress ( ( double ) config . CompletedFileNumber / config . FilesSum ) ;
154
+ } ) ;
153
155
}
154
156
}
155
157
156
- private string SumAppArgs ( string argsTemplet , string inputFile , string userArgs , string outputSuffix , string outputExtension , string outputFloder )
158
+ private string SumAppArgs ( string argsTemplet , string inputFileName , string userArgs , string outputSuffix , string outputExtension , string outputFloder )
157
159
{
158
160
//去前后引号
159
- inputFile = new Regex ( "(^\" )|(\" $)" ) . Replace ( inputFile , "" ) ;
161
+ inputFileName = new Regex ( "(^\" )|(\" $)" ) . Replace ( inputFileName , "" ) ;
160
162
argsTemplet = new Regex ( "(^\" )|(\" $)" ) . Replace ( argsTemplet , "" ) ;
161
163
outputSuffix = new Regex ( "(^\" )|(\" $)" ) . Replace ( outputSuffix , "" ) ;
162
164
outputExtension = new Regex ( "(^\" )|(\" $)" ) . Replace ( outputExtension , "" ) ;
@@ -173,7 +175,7 @@ private string SumAppArgs(string argsTemplet, string inputFile, string userArgs,
173
175
//替换 {InputFile}
174
176
{
175
177
//加前后引号
176
- string inputFile2 = "\" " + inputFile + "\" " ;
178
+ string inputFile2 = "\" " + inputFileName + "\" " ;
177
179
//替换模板中的标记
178
180
args = new Regex ( @"\{InputFile\}" ) . Replace ( args , inputFile2 ) ;
179
181
}
@@ -182,7 +184,7 @@ private string SumAppArgs(string argsTemplet, string inputFile, string userArgs,
182
184
{
183
185
string outputFile ;
184
186
//获得主文件名
185
- string mainName = new Regex ( @"\..[^.]+?$" ) . Replace ( inputFile , "" ) ;
187
+ string mainName = new Regex ( @"\..[^.]+?$" ) . Replace ( inputFileName , "" ) ;
186
188
187
189
//后缀
188
190
if ( outputSuffix != "" )
@@ -200,7 +202,7 @@ private string SumAppArgs(string argsTemplet, string inputFile, string userArgs,
200
202
else
201
203
{
202
204
//原拓展名
203
- var sourceExtension = new Regex ( @"\..[^.]+?$" ) . Match ( inputFile ) ;
205
+ var sourceExtension = new Regex ( @"\..[^.]+?$" ) . Match ( inputFileName ) ;
204
206
extension = Convert . ToString ( sourceExtension ) ;
205
207
}
206
208
//去除拓展名前的点
@@ -228,7 +230,7 @@ private string SumAppArgs(string argsTemplet, string inputFile, string userArgs,
228
230
return args ;
229
231
}
230
232
231
- private void NewProcess ( string appPath , string appArgs , uint windowStyle , uint priority , bool simulateCmd , int processIndex )
233
+ private Process NewProcess ( string appPath , string appArgs , uint windowStyle , uint priority , bool simulateCmd )
232
234
{
233
235
var process = new Process ( ) ;
234
236
if ( simulateCmd == false )
@@ -261,12 +263,11 @@ private void NewProcess(string appPath, string appArgs, uint windowStyle, uint p
261
263
try
262
264
{
263
265
process . Start ( ) ;
264
- processIdArr [ processIndex ] = process . Id ;
265
266
}
266
267
catch ( System . ComponentModel . Win32Exception e )
267
268
{
268
269
Debug . WriteLine ( "The process can not be started. Exception message: {0}" , e . Message ) ;
269
- return ;
270
+ return null ;
270
271
}
271
272
272
273
process . PriorityBoostEnabled = false ;
@@ -291,7 +292,8 @@ private void NewProcess(string appPath, string appArgs, uint windowStyle, uint p
291
292
process . PriorityClass = ProcessPriorityClass . RealTime ;
292
293
break ;
293
294
}
294
- process . WaitForExit ( ) ;
295
+
296
+ return process ;
295
297
}
296
298
297
299
private void SumConfig ( )
@@ -326,7 +328,7 @@ private void SumConfig()
326
328
}
327
329
}
328
330
329
- //Start and Exit
331
+ //On Start and Exit
330
332
public partial class MainWindow : Window
331
333
{
332
334
public MainWindow ( )
@@ -443,33 +445,32 @@ await Task.Run(() =>
443
445
cpuUseRatio = Math . Round ( cpuPerformanceCounter . NextValue ( ) ) ;
444
446
usedMem = ( double ) computerInfo . TotalPhysicalMemory - computerInfo . AvailablePhysicalMemory ;
445
447
memUseRatio = Math . Round ( ( double ) usedMem / computerInfo . TotalPhysicalMemory * 100 ) ;
446
-
447
448
//按需切换占用内存大小的显示单位及精度
448
- if ( usedMem >= 1099511627776 ) //占用内存>= 1TB
449
+ if ( usedMem >= 1099511627776 ) //1TB
449
450
{
450
451
memUnit = "TB" ;
451
- usedMem /= 1099511627776 ; //1024^4(4次方)
452
+ usedMem /= 1099511627776 ; //1024^4
452
453
usedMem = Math . Round ( usedMem , 3 ) ;
453
454
}
454
- else if ( usedMem >= 107374182400 ) //>= 100GB
455
+ else if ( usedMem >= 107374182400 ) //100GB
455
456
{
456
457
memUnit = "GB" ;
457
458
usedMem /= 1073741824 ; //^3
458
459
usedMem = Math . Round ( usedMem , 1 ) ;
459
460
}
460
- else if ( usedMem >= 10737418240 ) //>= 10GB
461
+ else if ( usedMem >= 10737418240 ) //10GB
461
462
{
462
463
memUnit = "GB" ;
463
464
usedMem /= 1073741824 ; //^3
464
465
usedMem = Math . Round ( usedMem , 2 ) ;
465
466
}
466
- else if ( usedMem >= 1073741824 ) //>= 1GB
467
+ else if ( usedMem >= 1073741824 ) //1GB
467
468
{
468
469
memUnit = "GB" ;
469
470
usedMem /= 1073741824 ; //^3
470
471
usedMem = Math . Round ( usedMem , 3 ) ;
471
472
}
472
- else if ( usedMem >= 104857600 ) //>= 100MB
473
+ else if ( usedMem >= 104857600 ) //100MB
473
474
{
474
475
memUnit = "MB" ;
475
476
usedMem /= 1048576 ; //^2
@@ -842,7 +843,7 @@ public class Config
842
843
{
843
844
public LinkedList < string > FilesList = new LinkedList < string > ( ) ;
844
845
public int FilesSum = 0 ;
845
- public int CompletedFileNum = 0 ;
846
+ public int CompletedFileNumber = 0 ;
846
847
847
848
public string AppPath ;
848
849
public string ArgsTemplet ;
0 commit comments