473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Exiting app while async operation in progress?

What's the best way to gracefully exit an app while an asynchronous
operation is in progress?

Let's say I have spawned a worker thread using the BackgroundWorker
component. If I exit the application while the worker thread is running, the
app crashes. It doesn't do any good to call CancelAsync() on the worker
thread from a FormClosing event handler--Since the worker thread is running
asynchronously, the main thread will continue the shutdown without waiting
for the cancel to complete.

I could work up some kludge, like cancelling the FormClose, putting a flag
in as a member variable, then reading that in the RunWorkerCompleted()
handler and reinitiating the application shutdown from there. But that seems
awfully clumsy--There must be a more elegant way to pause the application
shutdown until the worker thread ends.

Any ideas? Thanks.

--
David Veeneman
Foresight Systems
Aug 3 '06 #1
1 5121
I never did find a great answer to this problem. Here is what I ended up
doing:

(1) I did create a FormClosing method to exit the application. In it, I test
the BackgroundWorker to see if it is busy. If it isn't, the FormClose
proceeds normally. If the BackgroundWorker is busy, the FormClose is
cancelled, the background operation is cancelled, and a flag is set that
tells the RunWorkerCompleted() event handler that we are in the middle of a
form close. Here is the code:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Defer closing if background operation in process
if (m_BackgroundWorker.IsBusy)
{
// Cancel here
e.Cancel = true;

// Cancel background process
m_BackgroundWorker.CancelAsync();

// Set member variable flag to trigger exit in RunWorkerCompleted()
m_ExitAppCalled = true;
}
}

And here is the line I add at the end of the RunWorkerCompleted() event
handler:

// Exit if we're in the middle of an 'exit app' call
if (m_ExitAppCalled) Application.Exit();

Still not quite as elegant as I'd like, but it works!

--
David Veeneman
Foresight Systems
Aug 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Vanessa | last post by:
I have a question regarding async mode for calling Microsoft.XMLHTTP object. Microsoft.XMLHTTP hangs the IE once in a while suddenly, but it will work again after half an hour or so without doing...
10
by: Shawn Meyer | last post by:
Hello - I am trying to write a class that has an async BeginX and EndX, plus the regular X syncronous method. Delegates seemed like the way to go, however, I still am having problems getting...
5
by: Homa | last post by:
Hi all, Can anyone give me some links about how to do an async web service call from aspx and display a temperary page before the web service returns? Thanks, Homa Wong
5
by: Paul Hasell | last post by:
Hi, I'm trying to invoke a web method asynchronously but just can't seem to get it to tell me when it has finished! Below is the code I am (currently) using: private void...
0
by: whizpop | last post by:
Hi, First of all, thanks for a great starter kit, now If I could just get it to work (fully). I am trying to compile and run the solution/services all on a local dev box. I am able to...
7
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
2
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I'm using an async page to kick off a couple of asynchronous web services using the PageAsyncTask class. I'm also testing a progress bar that uses an Ajax UpdatePanel and Timer control to...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.