Killing Child Async Process (Non-Webservice)

  • Thread starter Thread starter Cam Drab
  • Start date Start date
C

Cam Drab

If this async call is running when I close the form, it appears to still be
running (through the while-statement loop) even after the form.closed. In
the FormClosing event I set the killedApp to true.

All searches on the subject seems to be asking how to do this and no
solution. There has to be a better way. Firstly, this just seems too
hostile and doesn't allow for graceful exits within the called delegate.
Secondly I'm not really confident it doesn't orphan the process until it
finishes anyway.

Any ideas?


delBuildTempTable = new BuildTempTableDelegate(BuildTempTable);
ar = delBuildTempTable.BeginInvoke(SourceConnection,
DestinationConnection, null, null);
try
{
while (!ar.IsCompleted)
{
Application.DoEvents();
// Sleep for 1/10th of a second
Thread.Sleep(100);

// Can't find a graceful way to abort the async process
w/ a non-webservice call
if (killedApp)
{
System.Diagnostics.Process thisProc = new
System.Diagnostics.Process();
thisProc = Process.GetCurrentProcess();
thisProc.Kill();
}
}

if (!delBuildTempTable.EndInvoke(ar))
{
return false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
 
Cam,

The graceful way would be to have an event handler for when the
asynchronous operation completes. When it does, you would finish your work,
checking the variable (which you should synchronize access to) at
intermittent times to determine if you should exit the callback thread.
 
Back
Top