Multi Thread / Prevent close windows form

R

RockDale

Hi, there

How can I prevent the user close my form when my backend thread have
not finished the process? Well, I guess using source code is more clear
to describe my question

I have a form and I am using a work thread to get street list from sql
ce database.

private void Form_Load(object sender, System.EventArgs e)
{
DoThreading();
}

public void DoThreading()
{
Thread thread = new Thread(new ThreadStart this.GetStreetData));
thread.Start();
Application.DoEvents();
}

public void GetStreetData()
{
//doing database stuff
this.lblDataCache.Invoke (new EventHandler(WorkerUpdate));
}
public void WorkerUpdate(object sender, EventArgs e)
{
this.lblStatus.Text = "get street data successful!";
}

My question is, I want to prevent the user close this form before the
thread execution finished. Because I will open second form after this
form closed. And in the second form I will open sql ce connection to do
some data retrieve. If the thread still runing, I will get error as the
sql ce does not support multiple connections open at a same time.

Is there a solution or I am on the wrong direction?
Thanks a lot
-Rockdale
 
D

Daniel Moth

Handle the Closing event from your form and set e.Cancel=true if your thread
is still running. To check if your thread is still running, have a flag
which you toggle in GetStreetData and WorkerUpdate.

Of course you should make sure that your "doing database stuff" is not too
long... if it is, then you need to have a way to cancel that operation...

Cheers
Daniel
 

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