//Single bot has finished private void onBotComplete(IAsyncResult ar) { try { launchDelegate ld = (launchDelegate)ar.AsyncState; ld.EndInvoke(ar); } catch (Exception x) { el.WriteEntry($"Method onBotComplete failed with error: {x.Message}"); } }
// When a file arrives, launch a single bot asynchronously, so that monitoring may continue. private void OnNewFileReceived(object source, FileSystemEventArgs e) { try { string triggerPath = e.FullPath; //This is the path to the {guid}.input file el.WriteEntry($"Received new file {triggerPath}."); FileInfo fi = new FileInfo(triggerPath); string[] fileParts = fi.Name.Split('.'); Guid candidate = Guid.Empty; try { candidate = Guid.Parse(fileParts[0]); } catch { el.WriteEntry($"Skipping Invalid file \"{triggerPath}\".", EventLogEntryType.Warning); return; } //So now you have a file names {guid}.input. Read it and make sure it's valid //The following is how to asynchronously make a C# method call. AsyncCallback cb = new AsyncCallback(onBotComplete); //The callback fires when the bot is complete. //With everything set up, the following calls createBot and returns immediately to monitoring //1. botPath is the full path of the {guid}.input file //2. out fileName is just the filename portion. This is done so that the callback can correlate the input with the output //3. cb is the callback method (onBotsComplete) //4. ld is the launch delegate object again. //IAsyncResult ar = ld.BeginInvoke(botPath, out fileName, cb, ld); launchDelegate ld = new launchDelegate(launchIntermediate); //The delegate, declared above, can just take a method name IAsyncResult ar = ld.BeginInvoke(triggerPath, candidate, cb, ld); } catch (Exception x) //Log and continue so that no particular input file can gum up the works { el.WriteEntry($"Unexpected error in method \"OnNewFileReceived\": {x.Message}", EventLogEntryType.Error); } }