<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jeff Slutter</title>
	<atom:link href="http://gigglenuts.com/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://gigglenuts.com/blog</link>
	<description>Oh my word</description>
	<lastBuildDate>Mon, 29 Aug 2011 23:26:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Robust Process Spawning and IO Redirection in .NET</title>
		<link>http://gigglenuts.com/blog/?p=10</link>
		<comments>http://gigglenuts.com/blog/?p=10#comments</comments>
		<pubDate>Sun, 28 Aug 2011 00:22:26 +0000</pubDate>
		<dc:creator>midnite</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gigglenuts.com/blog/?p=10</guid>
		<description><![CDATA[Many times something seems simple on the surface, but once you start you find it is a pain in the neck. Then, once in awhile, you find it is beyond a pain in the neck. For example, spawning a process and capturing stdout/stderr in .NET. Sure, it doesn't seem hard, create an instance of the [...]]]></description>
			<content:encoded><![CDATA[<p>Many times something seems simple on the surface, but once you start you find it is a pain in the neck. Then, once in awhile, you find it is beyond a pain in the neck. For example, spawning a process and capturing stdout/stderr in .NET. Sure, it doesn't seem hard, create an instance of the Process class, fill in some members, kick it off and wait until it finishes. Works great! On to the next problem...</p>
<p>Until the day it locks up on you unexpectedly. Hmm.</p>
<p>The next step is then to ask the modern Oracle - Google, to see if others have this problem. Sure enough, you find out that if the child process' buffer becomes full, it blocks, and that is causing your deadlock. Even better, there are tons of helpful people on the Internet willing to share the solution to this problem, along with source code snippets. Score! Asynchronous reads for the win. Hook it up, and on to the next problem...</p>
<p>Until the day it locks up on you unexpectedly. Hmm.</p>
<p>I got to this point, and Google began to fail me. It seems either no one ran into this problem, or, the only solution people talk about is the method I'm already using. We write all our crunching tools using C/C++ as  command line tools, and then a GUI in C# that spawns these tools. Most of the time, things ran fine for me - but, under heavy stress conditions, such as the child writing a lot of output, the whole thing would lock up. I even had a reproducible case, where it would lock up 100% of the time for a given command line tool with a certain set of arguments. We debugged our hearts out, trying every which way we could to keep things moving on, but we kept coming up short.</p>
<p>I can't say with 100% confidence, only 99%, that the issue was not mine, but Microsoft's. If you try to spawn processes that [may] write to both stdout and stderr (we'll ignore reading from stdin for now), you may find yourself in a world of hurt if one of those buffers fills up. This was a few months ago, and I'm having a hard time remembering the exact details, but it may have been related to the asynchronous call backs for the redirected stdout/stderr being called on the main thread, and, in my case, I was using threadpools for tasks that spawned the processes. My main thread of my application would wait until it got some sort of event from a worker thread that it was done, or there was an error. But, with Microsoft trying to invoke the callback on the main thread for the IO redirection, it never would call to process the buffer, which left the child process waiting for the buffer to clear, and my application's main thread waiting on the worker thread to finish...deadlock.</p>
<p>All I wanted to do was spawn a process and capture all of its stdout and all of its stderr. I did not have to interact with it via stdin at all. You would think this is a simple request. If I only wanted one or the other, I could have just used <span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;">p.StandardOutput.ReadToEnd(); </span>and just executed the process synchronously. You do not have that luxury if you want both stdout and stderr - you must use asynchronous reads, with the callbacks coming only on the main thread. Throw my need for threadpools into the mix and you find things don't work out so well.</p>
<p>All is not lost, I found a solution. The solution is to not use System.Diagnostics.Process and use raw Win32 API to get the same results. I could of  just wrote a C/C++ DLL, or a C++/CLI assembly, but I hate having a mess of DLLs laying around. I wanted a solution that stayed in C#, and so P/Invoke to the rescue. First I got it all working using straight C/C++ and then moved it all to C# using P/Invoke calls to the Win32 API. I'll save you the work, you can find it here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> ProcessAPI
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080;">#region WIN32 API</span>
    <span style="color: #6666cc; font-weight: bold;">class</span> Win32
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008000;">&#91;</span>StructLayout<span style="color: #008000;">&#40;</span>LayoutKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Sequential</span>, CharSet <span style="color: #008000;">=</span> CharSet<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Auto</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">struct</span> SECURITY_ATTRIBUTES
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 nLength<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> IntPtr lpSecurityDescriptor<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> bInheritHandle<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>StructLayout<span style="color: #008000;">&#40;</span>LayoutKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Sequential</span>, CharSet <span style="color: #008000;">=</span> CharSet<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Auto</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">struct</span> STARTUPINFO
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 cb<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> lpReserved<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> lpDesktop<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> lpTitle<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwX<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwY<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwXSize<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwYSize<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwXCountChars<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwYCountChars<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwFillAttribute<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwFlags<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int16 wShowWindow<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int16 cbReserved2<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> IntPtr lpReserved2<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> IntPtr hStdInput<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> IntPtr hStdOutput<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> IntPtr hStdError<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>StructLayout<span style="color: #008000;">&#40;</span>LayoutKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Sequential</span>, CharSet <span style="color: #008000;">=</span> CharSet<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Auto</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">struct</span> PROCESS_INFORMATION
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> IntPtr hProcess<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> IntPtr hThread<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwProcessId<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">public</span> Int32 dwThreadId<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>Flags<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">enum</span> DuplicateOptions <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">uint</span>
        <span style="color: #008000;">&#123;</span>
            DUPLICATE_CLOSE_SOURCE <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>0x00000001<span style="color: #008000;">&#41;</span>,<span style="color: #008080; font-style: italic;">// Closes the source handle. This occurs regardless of any error status returned.</span>
            DUPLICATE_SAME_ACCESS <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>0x00000002<span style="color: #008000;">&#41;</span>, <span style="color: #008080; font-style: italic;">//Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> Boolean CreatePipe<span style="color: #008000;">&#40;</span>
            <span style="color: #0600FF; font-weight: bold;">out</span> IntPtr hReadPipe,
            <span style="color: #0600FF; font-weight: bold;">out</span> IntPtr hWritePipe,
            <span style="color: #0600FF; font-weight: bold;">ref</span> SECURITY_ATTRIBUTES lpPipeAttributes,
            <span style="color: #6666cc; font-weight: bold;">uint</span> nSize<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> IntPtr GetCurrentProcess<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span>UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> Boolean DuplicateHandle<span style="color: #008000;">&#40;</span>
            IntPtr hSourceProcessHandle,
            IntPtr hSourceHandle,
            IntPtr hTargetProcessHandle,
            <span style="color: #0600FF; font-weight: bold;">out</span> IntPtr lpTargetHandle,
            UInt32 dwDesiredAccess,
            <span style="color: #008000;">&#91;</span>MarshalAs<span style="color: #008000;">&#40;</span>UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> <span style="color: #6666cc; font-weight: bold;">bool</span> bInheritHandle,
            UInt32 dwOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>, CallingConvention <span style="color: #008000;">=</span> CallingConvention<span style="color: #008000;">.</span><span style="color: #0000FF;">Winapi</span>, CharSet <span style="color: #008000;">=</span> CharSet<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Auto</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> IntPtr CreateEvent<span style="color: #008000;">&#40;</span>IntPtr lpEventAttributes,
            <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">In</span>, MarshalAs<span style="color: #008000;">&#40;</span>UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> Boolean bManualReset,
            <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">In</span>, MarshalAs<span style="color: #008000;">&#40;</span>UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> Boolean bIntialState,
            <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">In</span>, MarshalAs<span style="color: #008000;">&#40;</span>UnmanagedType<span style="color: #008000;">.</span><span style="color: #0000FF;">BStr</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> <span style="color: #6666cc; font-weight: bold;">string</span> lpName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetLastError<span style="color: #008000;">&#40;</span>Int32 dwErrCode<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> SetEvent<span style="color: #008000;">&#40;</span>IntPtr hEvent<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> Boolean WriteFile<span style="color: #008000;">&#40;</span>
            IntPtr hFile,
            <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> lpBuffer,
            UInt32 nNumberOfBytesToWrite,
            <span style="color: #0600FF; font-weight: bold;">out</span> UInt32 lpNumberOfBytesWritten,
            IntPtr lpOverlapped<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> ReadFile<span style="color: #008000;">&#40;</span>
            IntPtr hFile,
            <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">Out</span><span style="color: #008000;">&#93;</span> <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> lpBuffer,
            UInt32 nNumberOfBytesToRead,
            <span style="color: #0600FF; font-weight: bold;">out</span> UInt32 lpNumberOfBytesRead,
            IntPtr lpOverlapped<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CreateProcess<span style="color: #008000;">&#40;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> lpApplicationName,
            <span style="color: #6666cc; font-weight: bold;">string</span> lpCommandLine,
            IntPtr lpProcessAttributes,
            IntPtr lpThreadAttributes,
            Boolean bInheritHandles,
            UInt32 dwCreationFlags,
            IntPtr lpEnvironment,
            <span style="color: #6666cc; font-weight: bold;">string</span> lpCurrentDirectory,
            <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">In</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">ref</span> STARTUPINFO lpStartupInfo,
            <span style="color: #0600FF; font-weight: bold;">out</span> PROCESS_INFORMATION lpProcessInformation<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span>UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> TerminateProcess<span style="color: #008000;">&#40;</span>IntPtr hProcess, UInt32 uExitCode<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>, CallingConvention <span style="color: #008000;">=</span> CallingConvention<span style="color: #008000;">.</span><span style="color: #0000FF;">Winapi</span>, CharSet <span style="color: #008000;">=</span> CharSet<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Auto</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span>UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CloseHandle<span style="color: #008000;">&#40;</span>IntPtr hObject<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, EntryPoint <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;PeekNamedPipe&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> PeekNamedPipe<span style="color: #008000;">&#40;</span>
            IntPtr handle,
            <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buffer,
            UInt32 nBufferSize,
            <span style="color: #0600FF; font-weight: bold;">ref</span> UInt32 bytesRead,
            <span style="color: #0600FF; font-weight: bold;">ref</span> UInt32 bytesAvail,
            <span style="color: #0600FF; font-weight: bold;">ref</span> UInt32 BytesLeftThisMessage<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, EntryPoint <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;WaitForMultipleObjects&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">int</span> WaitForMultipleObjects<span style="color: #008000;">&#40;</span>
            UInt32 nCount,
            IntPtr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> lpHandles,
            Boolean fWaitAll,
            UInt32 dwMilliseconds<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span>UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> Boolean GetExitCodeProcess<span style="color: #008000;">&#40;</span>IntPtr hProcess, <span style="color: #0600FF; font-weight: bold;">out</span> Int32 lpExitCode<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ProcessRedirector <span style="color: #008000;">:</span> IDisposable
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080;">#region Internal Members</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Thread</span> m_hThread<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// thread to receive the output of the child process</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> IntPtr m_hEvtStop<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// event to notify the redir thread to exit</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> Int32 m_dwThreadId<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// id of the redir thread</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> Int32 m_resultCode<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// returned result code of the process</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> UInt32 m_dwWaitTime<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// wait time to check the status of the child process</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> IntPtr m_hStdinWrite<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// write end of child's stdin pipe</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> IntPtr m_hStdoutRead<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// read end of child's stdout pipe</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> IntPtr m_hStderrRead<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// read end of child's stderr pipe</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> IntPtr m_hChildProcess<span style="color: #008000;">;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Constructor</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> ProcessRedirector<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            m_hStdinWrite <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
            m_hStdoutRead <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
            m_hStderrRead <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
            m_hChildProcess <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
            m_hThread <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
            m_hEvtStop <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
            m_dwThreadId <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            m_dwWaitTime <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span>
            m_resultCode <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Finalizer</span>
        <span style="color: #008080; font-style: italic;">// NOTE: Leave out the finalizer altogether if this class doesn't </span>
        <span style="color: #008080; font-style: italic;">// own unmanaged resources itself, but leave the other methods</span>
        <span style="color: #008080; font-style: italic;">// exactly as they are. </span>
        ~ProcessRedirector<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Finalizer calls Dispose(false)</span>
            Dispose<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Dispose</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Dispose<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            GC<span style="color: #008000;">.</span><span style="color: #0000FF;">SuppressFinalize</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// The bulk of the clean-up code is implemented in Dispose(bool)</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> disposing<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>disposing<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// free managed resources</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// free native resources if there are any.</span>
            Close<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Property: ResultCode</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Access the return code from the spawned process</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Int32 ResultCode
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> m_resultCode<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region protected LaunchChild</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">bool</span> LaunchChild<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> pathToApp, <span style="color: #6666cc; font-weight: bold;">string</span> workingDirectory, <span style="color: #6666cc; font-weight: bold;">string</span> args, IntPtr hStdOut, IntPtr hStdIn, IntPtr hStdErr<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">const</span> Int16 SW_HIDE <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">const</span> Int32 STARTF_USESTDHANDLES <span style="color: #008000;">=</span> 0x100<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">const</span> Int32 STARTF_USESHOWWINDOW <span style="color: #008000;">=</span> 0x1<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">uint</span> CREATE_NEW_CONSOLE <span style="color: #008000;">=</span> 0x00000010<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Set up the start up info struct.</span>
            Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">STARTUPINFO</span> si <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">STARTUPINFO</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            si<span style="color: #008000;">.</span><span style="color: #0000FF;">cb</span> <span style="color: #008000;">=</span> Marshal<span style="color: #008000;">.</span><span style="color: #008000;">SizeOf</span><span style="color: #008000;">&#40;</span>si<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            si<span style="color: #008000;">.</span><span style="color: #0000FF;">hStdOutput</span> <span style="color: #008000;">=</span> hStdOut<span style="color: #008000;">;</span>
            si<span style="color: #008000;">.</span><span style="color: #0000FF;">hStdInput</span> <span style="color: #008000;">=</span> hStdIn<span style="color: #008000;">;</span>
            si<span style="color: #008000;">.</span><span style="color: #0000FF;">hStdError</span> <span style="color: #008000;">=</span> hStdErr<span style="color: #008000;">;</span>
            si<span style="color: #008000;">.</span><span style="color: #0000FF;">wShowWindow</span> <span style="color: #008000;">=</span> SW_HIDE<span style="color: #008000;">;</span>
            si<span style="color: #008000;">.</span><span style="color: #0000FF;">dwFlags</span> <span style="color: #008000;">=</span> STARTF_USESTDHANDLES <span style="color: #008000;">|</span> STARTF_USESHOWWINDOW<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Note that dwFlags must include STARTF_USESHOWWINDOW if we</span>
            <span style="color: #008080; font-style: italic;">// use the wShowWindow flags. This also assumes that the</span>
            <span style="color: #008080; font-style: italic;">// CreateProcess() call will use CREATE_NEW_CONSOLE.</span>
            Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">PROCESS_INFORMATION</span> pi <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">PROCESS_INFORMATION</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Launch the child process.</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateProcess</span><span style="color: #008000;">&#40;</span>pathToApp, args, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, <span style="color: #0600FF; font-weight: bold;">true</span>, CREATE_NEW_CONSOLE, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, workingDirectory, <span style="color: #0600FF; font-weight: bold;">ref</span> si, <span style="color: #0600FF; font-weight: bold;">out</span> pi<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
            m_hChildProcess <span style="color: #008000;">=</span> pi<span style="color: #008000;">.</span><span style="color: #0000FF;">hProcess</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Close any non-useful handles</span>
            Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">CloseHandle</span><span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">hThread</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region protected DestroyHandle</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> IntPtr rhObject<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>rhObject <span style="color: #008000;">==</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
            Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">CloseHandle</span><span style="color: #008000;">&#40;</span>rhObject<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            rhObject <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region private InternalClose</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> InternalClose<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> getExitCode<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>getExitCode <span style="color: #008000;">&amp;&amp;</span> m_hChildProcess <span style="color: #008000;">!=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// Snag the exit code before it's gone</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">GetExitCodeProcess</span><span style="color: #008000;">&#40;</span>m_hChildProcess, <span style="color: #0600FF; font-weight: bold;">out</span> m_resultCode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    m_resultCode <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> m_hEvtStop<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> m_hChildProcess<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> m_hStdinWrite<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> m_hStdoutRead<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> m_hStderrRead<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            m_dwThreadId <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Stdout/Stderr redirection processing</span>
        <span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #6666cc; font-weight: bold;">void</span> RedirectDelegate<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">int</span> InternalRedirect<span style="color: #008000;">&#40;</span>IntPtr hPipeRead, RedirectDelegate del<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span> ERROR_BROKEN_PIPE <span style="color: #008000;">=</span> <span style="color: #FF0000;">109</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span> ERROR_NO_DATA <span style="color: #008000;">=</span> <span style="color: #FF0000;">232</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">;</span> <span style="color: #008000;">;</span> <span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #6666cc; font-weight: bold;">uint</span> bytesRead <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">uint</span> dwAvail <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">uint</span> bytesLeft <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">PeekNamedPipe</span><span style="color: #008000;">&#40;</span>hPipeRead, <span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">ref</span> bytesRead, <span style="color: #0600FF; font-weight: bold;">ref</span> dwAvail, <span style="color: #0600FF; font-weight: bold;">ref</span> bytesLeft<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// error</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>dwAvail <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">// no data available</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> szOutput <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span>dwAvail<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadFile</span><span style="color: #008000;">&#40;</span>hPipeRead, szOutput, dwAvail, <span style="color: #0600FF; font-weight: bold;">out</span> bytesRead, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> bytesRead <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// error, the child might have ended</span>
&nbsp;
                del<span style="color: #008000;">&#40;</span>ASCIIEncoding<span style="color: #008000;">.</span><span style="color: #0000FF;">ASCII</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetString</span><span style="color: #008000;">&#40;</span>szOutput, <span style="color: #FF0000;">0</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>bytesRead<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">int</span> dwError <span style="color: #008000;">=</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>dwError <span style="color: #008000;">==</span> ERROR_BROKEN_PIPE <span style="color: #008000;">||</span>	<span style="color: #008080; font-style: italic;">// pipe has been ended</span>
                dwError <span style="color: #008000;">==</span> ERROR_NO_DATA<span style="color: #008000;">&#41;</span>		<span style="color: #008080; font-style: italic;">// pipe closing in progress</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>	<span style="color: #008080; font-style: italic;">// child process ended</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            WriteStdError<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Read stdout pipe error<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>		<span style="color: #008080; font-style: italic;">// os error</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// redirect the child process's stdout:</span>
        <span style="color: #008080; font-style: italic;">// return: 1: no more data, 0: child terminated, -1: os error</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">int</span> RedirectStdout<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> InternalRedirect<span style="color: #008000;">&#40;</span>m_hStdoutRead, <span style="color: #008000;">new</span> RedirectDelegate<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> msg<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> WriteStdOut<span style="color: #008000;">&#40;</span>msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// redirect the child process's stderr:</span>
        <span style="color: #008080; font-style: italic;">// return: 1: no more data, 0: child terminated, -1: os error</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">int</span> RedirectStderr<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> InternalRedirect<span style="color: #008000;">&#40;</span>m_hStderrRead, <span style="color: #008000;">new</span> RedirectDelegate<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> msg<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> WriteStdError<span style="color: #008000;">&#40;</span>msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> OutputThread<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span> WAIT_OBJECT_0 <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
            IntPtr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> aHandles <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
            aHandles<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> m_hChildProcess<span style="color: #008000;">;</span>
            aHandles<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> m_hEvtStop<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">bool</span> exitNormally <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">;</span> <span style="color: #008000;">;</span> <span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// redirect stdout till there's no more data.</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> nRet<span style="color: #008000;">;</span>
&nbsp;
                nRet <span style="color: #008000;">=</span> RedirectStdout<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>nRet <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
&nbsp;
                nRet <span style="color: #008000;">=</span> RedirectStderr<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>nRet <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// check if the child process has terminated.</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> dwRc <span style="color: #008000;">=</span> Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">WaitForMultipleObjects</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, aHandles, <span style="color: #0600FF; font-weight: bold;">false</span>, m_dwWaitTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>WAIT_OBJECT_0 <span style="color: #008000;">==</span> dwRc<span style="color: #008000;">&#41;</span>		<span style="color: #008080; font-style: italic;">// the child process ended</span>
                <span style="color: #008000;">&#123;</span>
                    RedirectStdout<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    RedirectStderr<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    exitNormally <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>WAIT_OBJECT_0 <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">==</span> dwRc<span style="color: #008000;">&#41;</span>	<span style="color: #008080; font-style: italic;">// m_hEvtStop was signalled</span>
                <span style="color: #008000;">&#123;</span>
                    Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">TerminateProcess</span><span style="color: #008000;">&#40;</span>m_hChildProcess, 0xFFFFFFFF<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// close handles</span>
            InternalClose<span style="color: #008000;">&#40;</span>exitNormally<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region virtual WriteStdOut</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Override to handle processing data written to stdout by the process</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;outputStr&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteStdOut<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> outputStr<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Out</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>outputStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region virtual WriteStdError</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Override to handle processing data written to stderr by the process</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;errorStr&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteStdError<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> errorStr<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>errorStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Open</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Initialize and spawn a process</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;pathToApp&quot;&gt;Absolute path to the executable to spawn&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;workingDirectory&quot;&gt;The working directory for the process&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;args&quot;&gt;The full set of command line arguments&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;True if everything went okay&lt;/returns&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Open<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> pathToApp, <span style="color: #6666cc; font-weight: bold;">string</span> workingDirectory, <span style="color: #6666cc; font-weight: bold;">string</span> args<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Close<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            IntPtr hStdoutReadTmp <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// parent stdout read handle</span>
            IntPtr hStderrReadTmp <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// parent stderr read handle</span>
            IntPtr hStdoutWrite <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// child stdout write handle</span>
            IntPtr hStderrWrite <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
            IntPtr hStdinWriteTmp <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// parent stdin write handle</span>
            IntPtr hStdinRead <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// child stdin read handle</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Set up the security attributes struct.</span>
            Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">SECURITY_ATTRIBUTES</span> sa <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">SECURITY_ATTRIBUTES</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            sa<span style="color: #008000;">.</span><span style="color: #0000FF;">nLength</span> <span style="color: #008000;">=</span> Marshal<span style="color: #008000;">.</span><span style="color: #008000;">SizeOf</span><span style="color: #008000;">&#40;</span>sa<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            sa<span style="color: #008000;">.</span><span style="color: #0000FF;">lpSecurityDescriptor</span> <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
            sa<span style="color: #008000;">.</span><span style="color: #0000FF;">bInheritHandle</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
            m_resultCode <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">bool</span> setupOk <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">try</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// Create a child stdout pipe.</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">CreatePipe</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> hStdoutReadTmp, <span style="color: #0600FF; font-weight: bold;">out</span> hStdoutWrite, <span style="color: #0600FF; font-weight: bold;">ref</span> sa, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Create a child stderr pipe.</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">CreatePipe</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> hStderrReadTmp, <span style="color: #0600FF; font-weight: bold;">out</span> hStderrWrite, <span style="color: #0600FF; font-weight: bold;">ref</span> sa, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Create a child stdin pipe.</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">CreatePipe</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> hStdinRead, <span style="color: #0600FF; font-weight: bold;">out</span> hStdinWriteTmp, <span style="color: #0600FF; font-weight: bold;">ref</span> sa, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Create new stdout read handle, stderr read handle and the stdin write handle.</span>
                <span style="color: #008080; font-style: italic;">// Set the inheritance properties to FALSE. Otherwise, the child</span>
                <span style="color: #008080; font-style: italic;">// inherits the these handles; resulting in non-closeable</span>
                <span style="color: #008080; font-style: italic;">// handles to the pipes being created.</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">DuplicateHandle</span><span style="color: #008000;">&#40;</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCurrentProcess</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, hStdoutReadTmp, Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCurrentProcess</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> m_hStdoutRead, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">uint</span><span style="color: #008000;">&#41;</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">DuplicateOptions</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DUPLICATE_SAME_ACCESS</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">DuplicateHandle</span><span style="color: #008000;">&#40;</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCurrentProcess</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, hStderrReadTmp, Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCurrentProcess</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> m_hStderrRead, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">uint</span><span style="color: #008000;">&#41;</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">DuplicateOptions</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DUPLICATE_SAME_ACCESS</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">DuplicateHandle</span><span style="color: #008000;">&#40;</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCurrentProcess</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, hStdinWriteTmp, Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCurrentProcess</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> m_hStdinWrite, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">uint</span><span style="color: #008000;">&#41;</span>Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">DuplicateOptions</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DUPLICATE_SAME_ACCESS</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Close inheritable copies of the handles we do not want to be inherited.</span>
                DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStdoutReadTmp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStderrReadTmp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStdinWriteTmp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// launch the child process</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> appFilename <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetFileName</span><span style="color: #008000;">&#40;</span>pathToApp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> cmdArgs <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;">+</span> appFilename <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span> &quot;</span> <span style="color: #008000;">+</span> args<span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>LaunchChild<span style="color: #008000;">&#40;</span>pathToApp, workingDirectory, cmdArgs, hStdoutWrite, hStdinRead, hStderrWrite<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Child is launched. Close the parents copy of those pipe</span>
                <span style="color: #008080; font-style: italic;">// handles that only the child should have open.</span>
                <span style="color: #008080; font-style: italic;">// Make sure that no handles to the write end of the stdout pipe</span>
                <span style="color: #008080; font-style: italic;">// are maintained in this process or else the pipe will not</span>
                <span style="color: #008080; font-style: italic;">// close when the child process exits and ReadFile will hang.</span>
                DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStdoutWrite<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStdinRead<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStderrWrite<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// Launch a thread to receive output from the child process.</span>
                m_hEvtStop <span style="color: #008000;">=</span> Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateEvent</span><span style="color: #008000;">&#40;</span>IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, <span style="color: #0600FF; font-weight: bold;">true</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">try</span>
                <span style="color: #008000;">&#123;</span>
                    m_hThread <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Thread</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">ThreadStart</span><span style="color: #008000;">&#40;</span>OutputThread<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    m_hThread<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;StdOutErr Processor &quot;</span> <span style="color: #008000;">+</span> appFilename<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                <span style="color: #0600FF; font-weight: bold;">catch</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                m_hThread<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                m_dwThreadId <span style="color: #008000;">=</span> m_hThread<span style="color: #008000;">.</span><span style="color: #0000FF;">ManagedThreadId</span><span style="color: #008000;">;</span>
                setupOk <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">finally</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>setupOk<span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    Int32 dwOsErr <span style="color: #008000;">=</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>dwOsErr <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #008000;">&#123;</span>
                        WriteStdError<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Redirect console error: &quot;</span> <span style="color: #008000;">+</span> dwOsErr<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;x8&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #008000;">&#125;</span>
&nbsp;
                    DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStdoutReadTmp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStderrReadTmp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStdoutWrite<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStderrWrite<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStdinWriteTmp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    DestroyHandle<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> hStdinRead<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    Close<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">SetLastError</span><span style="color: #008000;">&#40;</span>dwOsErr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Close</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Close a process</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;abort&quot;&gt;If true, abort the processing (waiting up to</span>
        <span style="color: #008080; font-style: italic;">/// 5 seconds before terminating), otherwise just wait until it exits&lt;/param&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> Close<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> abort<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>m_hThread <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>abort<span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">// tell the thread to bail</span>
                    Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">SetEvent</span><span style="color: #008000;">&#40;</span>m_hEvtStop<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>m_hThread<span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #008000;">&#123;</span>
                        <span style="color: #0600FF; font-weight: bold;">try</span>
                        <span style="color: #008000;">&#123;</span>
                            m_hThread<span style="color: #008000;">.</span><span style="color: #0000FF;">Abort</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                        <span style="color: #008000;">&#125;</span>
                        <span style="color: #0600FF; font-weight: bold;">catch</span>
                        <span style="color: #008000;">&#123;</span>
                        <span style="color: #008000;">&#125;</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
                <span style="color: #0600FF; font-weight: bold;">else</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">// wait until the thread exits</span>
                    m_hThread<span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                m_hThread <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            InternalClose<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region SendToStdIn</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Send the given string of data to the stdin of the spawned process</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;str&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> SendToStdIn<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> str<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>m_hStdinWrite <span style="color: #008000;">==</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> strData <span style="color: #008000;">=</span> ASCIIEncoding<span style="color: #008000;">.</span><span style="color: #0000FF;">ASCII</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">uint</span> dwWritten<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> Win32<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteFile</span><span style="color: #008000;">&#40;</span>m_hStdinWrite, strData, <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">uint</span><span style="color: #008000;">&#41;</span>strData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, <span style="color: #0600FF; font-weight: bold;">out</span> dwWritten, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region SetWaitTime</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Adjust the waiting time between checks to see if the process</span>
        <span style="color: #008080; font-style: italic;">/// has exited, this is also the time between checks to stdout and</span>
        <span style="color: #008080; font-style: italic;">/// stderr processing.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;waitTime&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetWaitTime<span style="color: #008000;">&#40;</span>UInt32 waitTime<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            m_dwWaitTime <span style="color: #008000;">=</span> waitTime<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://gigglenuts.com/blog/?feed=rss2&#038;p=10</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Game Loops and Frametime</title>
		<link>http://gigglenuts.com/blog/?p=5</link>
		<comments>http://gigglenuts.com/blog/?p=5#comments</comments>
		<pubDate>Mon, 04 Jul 2011 20:05:31 +0000</pubDate>
		<dc:creator>midnite</dc:creator>
				<category><![CDATA[Game Loops]]></category>

		<guid isPermaLink="false">http://gigglenuts.com/blog/?p=5</guid>
		<description><![CDATA[Kwasi Mensah wrote a blog on #altdevblogaday entitled Game Loops on IOS that I skimmed through. I'm currently not dealing with game loops, frametime, nor IOS right now, so this post is just a little reminder for my future self should I find myself tumbling down that wormhole. Glenn Fiedler's article Fix Your Timestep is the [...]]]></description>
			<content:encoded><![CDATA[<p>Kwasi Mensah wrote a blog on #altdevblogaday entitled <a title="Game Loops on IOS" href="http://altdevblogaday.com/2011/06/27/game-loops-on-ios/" target="_blank">Game Loops on IOS</a> that I skimmed through. I'm currently not dealing with game loops, frametime, nor IOS right now, so this post is just a little reminder for my future self should I find myself tumbling down that wormhole.</p>
<p>Glenn Fiedler's article <a title="Fix Your Timestep" href="http://gafferongames.com/game-physics/fix-your-timestep/" target="_blank">Fix Your Timestep</a> is the oft-referenced article on the topic for good reason, and is always worth another read through.</p>
<p>The summary for Kwasi's article:</p>
<ul>
<li>He has a function that gets called when there was a vsync</li>
<li>If a vsync happens while he is in the function, the function will get called again immediately after it exits</li>
<li>Most of the time you should be hitting 60 fps (or 30 fps) and occasionally your frametime might go over</li>
<li>What you don't want is to spiral out of control if you occasionally miss a frame. If you are consistently missing frames than you should drop your goal frametime to the next level (60 -&gt; 30)</li>
<li>Start by timing how long it took to run the "DoFrame" function, call this time <em>elapsed</em>.</li>
<li>If <em>elapsed</em> took longer than your goal frametime, then change the value of <em>elapsed</em> to your goal frametime plus how much you overran the vsync: <strong><em>fmod(elapsed,frametime)</em></strong></li>
<li>Store <em>elapsed</em> in a persistent variable, called <em>bank</em>, for the next call to "DoFrame".</li>
<li>At the start of "DoFrame" subtract your goal frametime from the persistent <em>bank</em> variable.</li>
<li>If <em>bank</em> is higher than zero, that means your last processing through "DoFrame" lasted longer than your goal frametime, and therefore, you missed a vsync (or more). In this case he suggests just forgoing the processing of "DoFrame" for this frame and just wait until the next vsync (the article explains why). So exit out of "DoFrame" until next time.</li>
<li>Otherwise <em>bank</em> is less than, or equal to, zero. Which means that the last processing through "DoFrame" was quicker than your goal frametime (or, just as long). Reset <em>bank</em> to zero because we are starting a new, and do the processing of "DoFrame".</li>
</ul>
<p>So, occasionally you'll have to eat some time to wait for the next vsync should you miss a goal vsync. But the idea is, if you don't, another vsync will come along, and if you miss that (as you are already under a sub-vsync interval of time remaining due to the miss) than you are going to throw off things even more. I guess this will cause a, probably unnoticeable, slow down in time for when you miss a vsync.</p>
<p>I have to give some thought to all of this, but until then...</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://gigglenuts.com/blog/?feed=rss2&#038;p=5</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

