Killing Child Async Process (Non-Webservice)

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());
}
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top