Jeff Slutter Oh my word! :-O

27Aug/110

Robust Process Spawning and IO Redirection in .NET

Posted by midnite

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...

Until the day it locks up on you unexpectedly. Hmm.

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...

Until the day it locks up on you unexpectedly. Hmm.

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.

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.

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 p.StandardOutput.ReadToEnd(); 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.

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:

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
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
 
namespace ProcessAPI
{
    #region WIN32 API
    class Win32
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct SECURITY_ATTRIBUTES
        {
            public Int32 nLength;
            public IntPtr lpSecurityDescriptor;
            public bool bInheritHandle;
        }
 
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct STARTUPINFO
        {
            public Int32 cb;
            public string lpReserved;
            public string lpDesktop;
            public string lpTitle;
            public Int32 dwX;
            public Int32 dwY;
            public Int32 dwXSize;
            public Int32 dwYSize;
            public Int32 dwXCountChars;
            public Int32 dwYCountChars;
            public Int32 dwFillAttribute;
            public Int32 dwFlags;
            public Int16 wShowWindow;
            public Int16 cbReserved2;
            public IntPtr lpReserved2;
            public IntPtr hStdInput;
            public IntPtr hStdOutput;
            public IntPtr hStdError;
        }
 
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct PROCESS_INFORMATION
        {
            public IntPtr hProcess;
            public IntPtr hThread;
            public Int32 dwProcessId;
            public Int32 dwThreadId;
        }
 
        [Flags]
        public enum DuplicateOptions : uint
        {
            DUPLICATE_CLOSE_SOURCE = (0x00000001),// Closes the source handle. This occurs regardless of any error status returned.
            DUPLICATE_SAME_ACCESS = (0x00000002), //Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.
        }
 
        [DllImport("kernel32.dll")]
        public static extern Boolean CreatePipe(
            out IntPtr hReadPipe,
            out IntPtr hWritePipe,
            ref SECURITY_ATTRIBUTES lpPipeAttributes,
            uint nSize);
 
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetCurrentProcess();
 
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern Boolean DuplicateHandle(
            IntPtr hSourceProcessHandle,
            IntPtr hSourceHandle,
            IntPtr hTargetProcessHandle,
            out IntPtr lpTargetHandle,
            UInt32 dwDesiredAccess,
            [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
            UInt32 dwOptions);
 
        [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)]
        public static extern IntPtr CreateEvent(IntPtr lpEventAttributes,
            [In, MarshalAs(UnmanagedType.Bool)] Boolean bManualReset,
            [In, MarshalAs(UnmanagedType.Bool)] Boolean bIntialState,
            [In, MarshalAs(UnmanagedType.BStr)] string lpName);
 
        [DllImport("kernel32.dll")]
        public static extern void SetLastError(Int32 dwErrCode);
 
        [DllImport("kernel32.dll")]
        public static extern bool SetEvent(IntPtr hEvent);
 
        [DllImport("kernel32.dll")]
        public static extern Boolean WriteFile(
            IntPtr hFile,
            Byte[] lpBuffer,
            UInt32 nNumberOfBytesToWrite,
            out UInt32 lpNumberOfBytesWritten,
            IntPtr lpOverlapped);
 
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool ReadFile(
            IntPtr hFile,
            [Out] Byte[] lpBuffer,
            UInt32 nNumberOfBytesToRead,
            out UInt32 lpNumberOfBytesRead,
            IntPtr lpOverlapped);
 
        [DllImport("kernel32.dll")]
        public static extern bool CreateProcess(
            string lpApplicationName,
            string lpCommandLine,
            IntPtr lpProcessAttributes,
            IntPtr lpThreadAttributes,
            Boolean bInheritHandles,
            UInt32 dwCreationFlags,
            IntPtr lpEnvironment,
            string lpCurrentDirectory,
            [In] ref STARTUPINFO lpStartupInfo,
            out PROCESS_INFORMATION lpProcessInformation);
 
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool TerminateProcess(IntPtr hProcess, UInt32 uExitCode);
 
        [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseHandle(IntPtr hObject);
 
        [DllImport("kernel32.dll", EntryPoint = "PeekNamedPipe", SetLastError = true)]
        public static extern bool PeekNamedPipe(
            IntPtr handle,
            Byte[] buffer,
            UInt32 nBufferSize,
            ref UInt32 bytesRead,
            ref UInt32 bytesAvail,
            ref UInt32 BytesLeftThisMessage);
 
        [DllImport("kernel32.dll", EntryPoint = "WaitForMultipleObjects", SetLastError = true)]
        public static extern int WaitForMultipleObjects(
            UInt32 nCount,
            IntPtr[] lpHandles,
            Boolean fWaitAll,
            UInt32 dwMilliseconds);
 
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern Boolean GetExitCodeProcess(IntPtr hProcess, out Int32 lpExitCode);
    }
    #endregion
 
    public class ProcessRedirector : IDisposable
    {
        #region Internal Members
        private System.Threading.Thread m_hThread; // thread to receive the output of the child process
        private IntPtr m_hEvtStop; // event to notify the redir thread to exit
        private Int32 m_dwThreadId; // id of the redir thread
        private Int32 m_resultCode; // returned result code of the process
        private UInt32 m_dwWaitTime; // wait time to check the status of the child process
 
        protected IntPtr m_hStdinWrite; // write end of child's stdin pipe
        protected IntPtr m_hStdoutRead; // read end of child's stdout pipe
        protected IntPtr m_hStderrRead; // read end of child's stderr pipe
        protected IntPtr m_hChildProcess;
        #endregion
 
        #region Constructor
        public ProcessRedirector()
        {
            m_hStdinWrite = IntPtr.Zero;
            m_hStdoutRead = IntPtr.Zero;
            m_hStderrRead = IntPtr.Zero;
            m_hChildProcess = IntPtr.Zero;
            m_hThread = null;
            m_hEvtStop = IntPtr.Zero;
            m_dwThreadId = 0;
            m_dwWaitTime = 100;
            m_resultCode = -1;
        }
        #endregion
 
        #region Finalizer
        // NOTE: Leave out the finalizer altogether if this class doesn't 
        // own unmanaged resources itself, but leave the other methods
        // exactly as they are. 
        ~ProcessRedirector()
        {
            // Finalizer calls Dispose(false)
            Dispose(false);
        }
        #endregion
 
        #region Dispose
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
 
        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // free managed resources
            }
 
            // free native resources if there are any.
            Close(true);
        }
        #endregion
 
        #region Property: ResultCode
        /// <summary>
        /// Access the return code from the spawned process
        /// </summary>
        public Int32 ResultCode
        {
            get { return m_resultCode; }
        }
        #endregion
 
        #region protected LaunchChild
        protected bool LaunchChild(string pathToApp, string workingDirectory, string args, IntPtr hStdOut, IntPtr hStdIn, IntPtr hStdErr)
        {
            const Int16 SW_HIDE = 0;
            const Int32 STARTF_USESTDHANDLES = 0x100;
            const Int32 STARTF_USESHOWWINDOW = 0x1;
            const uint CREATE_NEW_CONSOLE = 0x00000010;
 
            // Set up the start up info struct.
            Win32.STARTUPINFO si = new Win32.STARTUPINFO();
            si.cb = Marshal.SizeOf(si);
            si.hStdOutput = hStdOut;
            si.hStdInput = hStdIn;
            si.hStdError = hStdErr;
            si.wShowWindow = SW_HIDE;
            si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
 
            // Note that dwFlags must include STARTF_USESHOWWINDOW if we
            // use the wShowWindow flags. This also assumes that the
            // CreateProcess() call will use CREATE_NEW_CONSOLE.
            Win32.PROCESS_INFORMATION pi = new Win32.PROCESS_INFORMATION();
 
            // Launch the child process.
            if (!Win32.CreateProcess(pathToApp, args, IntPtr.Zero, IntPtr.Zero, true, CREATE_NEW_CONSOLE, IntPtr.Zero, workingDirectory, ref si, out pi))
                return false;
 
            m_hChildProcess = pi.hProcess;
 
            // Close any non-useful handles
            Win32.CloseHandle(pi.hThread);
            return true;
        }
        #endregion
 
        #region protected DestroyHandle
        protected void DestroyHandle(ref IntPtr rhObject)
        {
            if (rhObject == IntPtr.Zero)
                return;
 
            Win32.CloseHandle(rhObject);
            rhObject = IntPtr.Zero;
        }
        #endregion
 
        #region private InternalClose
        private void InternalClose(bool getExitCode)
        {
            if (getExitCode && m_hChildProcess != IntPtr.Zero)
            {
                // Snag the exit code before it's gone
                if (!Win32.GetExitCodeProcess(m_hChildProcess, out m_resultCode))
                {
                    m_resultCode = -1;
                }
            }
 
            DestroyHandle(ref m_hEvtStop);
            DestroyHandle(ref m_hChildProcess);
            DestroyHandle(ref m_hStdinWrite);
            DestroyHandle(ref m_hStdoutRead);
            DestroyHandle(ref m_hStderrRead);
            m_dwThreadId = 0;
        }
        #endregion
 
        #region Stdout/Stderr redirection processing
        delegate void RedirectDelegate(string msg);
        int InternalRedirect(IntPtr hPipeRead, RedirectDelegate del)
        {
            const int ERROR_BROKEN_PIPE = 109;
            const int ERROR_NO_DATA = 232;
 
            for (; ; )
            {
                uint bytesRead = 0;
                uint dwAvail = 0;
                uint bytesLeft = 0;
                if (!Win32.PeekNamedPipe(hPipeRead, null, 0, ref bytesRead, ref dwAvail, ref bytesLeft))
                    break; // error
 
                if (dwAvail == 0)
                {
                    // no data available
                    return 1;
                }
 
                byte[] szOutput = new byte[dwAvail];
                if (!Win32.ReadFile(hPipeRead, szOutput, dwAvail, out bytesRead, IntPtr.Zero) || bytesRead == 0)
                    break; // error, the child might have ended
 
                del(ASCIIEncoding.ASCII.GetString(szOutput, 0, (int)bytesRead));
            }
 
            int dwError = Marshal.GetLastWin32Error();
            if (dwError == ERROR_BROKEN_PIPE ||	// pipe has been ended
                dwError == ERROR_NO_DATA)		// pipe closing in progress
            {
                return 0;	// child process ended
            }
 
            WriteStdError("Read stdout pipe error\r\n");
            return -1;		// os error
        }
 
        // redirect the child process's stdout:
        // return: 1: no more data, 0: child terminated, -1: os error
        protected int RedirectStdout()
        {
            return InternalRedirect(m_hStdoutRead, new RedirectDelegate(delegate(string msg) { WriteStdOut(msg); }));
        }
 
        // redirect the child process's stderr:
        // return: 1: no more data, 0: child terminated, -1: os error
        protected int RedirectStderr()
        {
            return InternalRedirect(m_hStderrRead, new RedirectDelegate(delegate(string msg) { WriteStdError(msg); }));
        }
 
        protected void OutputThread()
        {
            const int WAIT_OBJECT_0 = 0;
 
            IntPtr[] aHandles = new IntPtr[2];
            aHandles[0] = m_hChildProcess;
            aHandles[1] = m_hEvtStop;
 
            bool exitNormally = false;
            for (; ; )
            {
                // redirect stdout till there's no more data.
                int nRet;
 
                nRet = RedirectStdout();
                if (nRet < 0)
                    break;
 
                nRet = RedirectStderr();
                if (nRet < 0)
                    break;
 
                // check if the child process has terminated.
                int dwRc = Win32.WaitForMultipleObjects(2, aHandles, false, m_dwWaitTime);
                if (WAIT_OBJECT_0 == dwRc)		// the child process ended
                {
                    RedirectStdout();
                    RedirectStderr();
                    exitNormally = true;
                    break;
                }
 
                if (WAIT_OBJECT_0 + 1 == dwRc)	// m_hEvtStop was signalled
                {
                    Win32.TerminateProcess(m_hChildProcess, 0xFFFFFFFF);
                    break;
                }
            }
 
            // close handles
            InternalClose(exitNormally);
        }
        #endregion
 
        #region virtual WriteStdOut
        /// <summary>
        /// Override to handle processing data written to stdout by the process
        /// </summary>
        /// <param name="outputStr"></param>
        protected virtual void WriteStdOut(string outputStr)
        {
            Console.Out.Write(outputStr);
        }
        #endregion
 
        #region virtual WriteStdError
        /// <summary>
        /// Override to handle processing data written to stderr by the process
        /// </summary>
        /// <param name="errorStr"></param>
        protected virtual void WriteStdError(string errorStr)
        {
            Console.Error.Write(errorStr);
        }
        #endregion
 
        #region Open
        /// <summary>
        /// Initialize and spawn a process
        /// </summary>
        /// <param name="pathToApp">Absolute path to the executable to spawn</param>
        /// <param name="workingDirectory">The working directory for the process</param>
        /// <param name="args">The full set of command line arguments</param>
        /// <returns>True if everything went okay</returns>
        public bool Open(string pathToApp, string workingDirectory, string args)
        {
            Close(true);
 
            IntPtr hStdoutReadTmp = IntPtr.Zero; // parent stdout read handle
            IntPtr hStderrReadTmp = IntPtr.Zero; // parent stderr read handle
            IntPtr hStdoutWrite = IntPtr.Zero; // child stdout write handle
            IntPtr hStderrWrite = IntPtr.Zero;
            IntPtr hStdinWriteTmp = IntPtr.Zero; // parent stdin write handle
            IntPtr hStdinRead = IntPtr.Zero; // child stdin read handle
 
            // Set up the security attributes struct.
            Win32.SECURITY_ATTRIBUTES sa = new Win32.SECURITY_ATTRIBUTES();
            sa.nLength = Marshal.SizeOf(sa);
            sa.lpSecurityDescriptor = IntPtr.Zero;
            sa.bInheritHandle = true;
            m_resultCode = -1;
 
            bool setupOk = false;
            try
            {
                // Create a child stdout pipe.
                if (!Win32.CreatePipe(out hStdoutReadTmp, out hStdoutWrite, ref sa, 0))
                    return false;
 
                // Create a child stderr pipe.
                if (!Win32.CreatePipe(out hStderrReadTmp, out hStderrWrite, ref sa, 0))
                    return false;
 
                // Create a child stdin pipe.
                if (!Win32.CreatePipe(out hStdinRead, out hStdinWriteTmp, ref sa, 0))
                    return false;
 
                // Create new stdout read handle, stderr read handle and the stdin write handle.
                // Set the inheritance properties to FALSE. Otherwise, the child
                // inherits the these handles; resulting in non-closeable
                // handles to the pipes being created.
                if (!Win32.DuplicateHandle(Win32.GetCurrentProcess(), hStdoutReadTmp, Win32.GetCurrentProcess(), out m_hStdoutRead, 0, false, (uint)Win32.DuplicateOptions.DUPLICATE_SAME_ACCESS))
                    return false;
 
                if (!Win32.DuplicateHandle(Win32.GetCurrentProcess(), hStderrReadTmp, Win32.GetCurrentProcess(), out m_hStderrRead, 0, false, (uint)Win32.DuplicateOptions.DUPLICATE_SAME_ACCESS))
                    return false;
 
                if (!Win32.DuplicateHandle(Win32.GetCurrentProcess(), hStdinWriteTmp, Win32.GetCurrentProcess(), out m_hStdinWrite, 0, false, (uint)Win32.DuplicateOptions.DUPLICATE_SAME_ACCESS))
                    return false;
 
                // Close inheritable copies of the handles we do not want to be inherited.
                DestroyHandle(ref hStdoutReadTmp);
                DestroyHandle(ref hStderrReadTmp);
                DestroyHandle(ref hStdinWriteTmp);
 
                // launch the child process
                string appFilename = System.IO.Path.GetFileName(pathToApp);
                string cmdArgs = "\"" + appFilename + "\" " + args;
                if (!LaunchChild(pathToApp, workingDirectory, cmdArgs, hStdoutWrite, hStdinRead, hStderrWrite))
                    return false;
 
                // Child is launched. Close the parents copy of those pipe
                // handles that only the child should have open.
                // Make sure that no handles to the write end of the stdout pipe
                // are maintained in this process or else the pipe will not
                // close when the child process exits and ReadFile will hang.
                DestroyHandle(ref hStdoutWrite);
                DestroyHandle(ref hStdinRead);
                DestroyHandle(ref hStderrWrite);
 
                // Launch a thread to receive output from the child process.
                m_hEvtStop = Win32.CreateEvent(IntPtr.Zero, true, false, null);
 
                try
                {
                    m_hThread = new System.Threading.Thread(new System.Threading.ThreadStart(OutputThread));
                    m_hThread.Name = "StdOutErr Processor " + appFilename;
                }
                catch
                {
                    return false;
                }
                m_hThread.Start();
                m_dwThreadId = m_hThread.ManagedThreadId;
                setupOk = true;
            }
            finally
            {
                if (!setupOk)
                {
                    Int32 dwOsErr = Marshal.GetLastWin32Error();
                    if (dwOsErr != 0)
                    {
                        WriteStdError("Redirect console error: " + dwOsErr.ToString("x8") + "\r\n");
                    }
 
                    DestroyHandle(ref hStdoutReadTmp);
                    DestroyHandle(ref hStderrReadTmp);
                    DestroyHandle(ref hStdoutWrite);
                    DestroyHandle(ref hStderrWrite);
                    DestroyHandle(ref hStdinWriteTmp);
                    DestroyHandle(ref hStdinRead);
                    Close(true);
 
                    Win32.SetLastError(dwOsErr);
                }
            }
 
            return true;
        }
        #endregion
 
        #region Close
        /// <summary>
        /// Close a process
        /// </summary>
        /// <param name="abort">If true, abort the processing (waiting up to
        /// 5 seconds before terminating), otherwise just wait until it exits</param>
        public virtual void Close(bool abort)
        {
            if (m_hThread != null)
            {
                if (abort)
                {
                    // tell the thread to bail
                    Win32.SetEvent(m_hEvtStop);
                    if (!m_hThread.Join(5000))
                    {
                        try
                        {
                            m_hThread.Abort();
                        }
                        catch
                        {
                        }
                    }
                }
                else
                {
                    // wait until the thread exits
                    m_hThread.Join();
                }
 
                m_hThread = null;
            }
 
            InternalClose(false);
        }
        #endregion
 
        #region SendToStdIn
        /// <summary>
        /// Send the given string of data to the stdin of the spawned process
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool SendToStdIn(string str)
        {
            if (m_hStdinWrite == IntPtr.Zero)
                return false;
 
            byte[] strData = ASCIIEncoding.ASCII.GetBytes(str);
 
            uint dwWritten;
            return Win32.WriteFile(m_hStdinWrite, strData, (uint)strData.Length, out dwWritten, IntPtr.Zero);
        }
        #endregion
 
        #region SetWaitTime
        /// <summary>
        /// Adjust the waiting time between checks to see if the process
        /// has exited, this is also the time between checks to stdout and
        /// stderr processing.
        /// </summary>
        /// <param name="waitTime"></param>
        public void SetWaitTime(UInt32 waitTime)
        {
            m_dwWaitTime = waitTime;
        }
        #endregion
    }
}
Filed under: Uncategorized No Comments
4Jul/110

Game Loops and Frametime

Posted by midnite

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 oft-referenced article on the topic for good reason, and is always worth another read through.

The summary for Kwasi's article:

  • He has a function that gets called when there was a vsync
  • If a vsync happens while he is in the function, the function will get called again immediately after it exits
  • Most of the time you should be hitting 60 fps (or 30 fps) and occasionally your frametime might go over
  • 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 -> 30)
  • Start by timing how long it took to run the "DoFrame" function, call this time elapsed.
  • If elapsed took longer than your goal frametime, then change the value of elapsed to your goal frametime plus how much you overran the vsync: fmod(elapsed,frametime)
  • Store elapsed in a persistent variable, called bank, for the next call to "DoFrame".
  • At the start of "DoFrame" subtract your goal frametime from the persistent bank variable.
  • If bank 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.
  • Otherwise bank 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 bank to zero because we are starting a new, and do the processing of "DoFrame".

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.

I have to give some thought to all of this, but until then...

 

Filed under: Game Loops No Comments