Handling Worker Thread Shutdown

G

Guest

Hi,

I create a worker thread to create a local CE database then connect to a
webservice to bring back data. This process runs in the business logic tier
which communicates to the client UI thread via a passed parameter. It
communicates with the UI thread to see if a cancel request has occured and
also to update the status (progress bar etc).

What I would like to know is how can I make the UI thread wait until the
worker thread has returned from the call to the web service in order to
perorm some further processing like deleting the CE database before control
is returned to the calling UI program.

I know it sounds confusing. Here is a snip of the UI cancel method:

private void cancel_push_Click(object sender, System.EventArgs e)
{
//Check if we are running any worker threads or not.
if (worker_thread!=null)
{
if (worker_thread.IsAlive)
{
worker_thread.Suspend();
DialogResult _result =
MessageBox.Show(MyResources.Strings["middleofcreatingalogbook_msg"],
this.Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (_result==DialogResult.Yes)
{
//Then cancel the current operation.
Cache.ThreadStatus=WorkerThreadStatus.Exception;
//Resume the thread to let it finish doing its job.
worker_thread.Resume();

}
else
{
//Then continue.
worker_thread.Resume();
return;
}
}
}
//Ensure we disconnect from the Connection Manager.
if (connection_cm!=null)
{
connection_cm.OnConnectionStateChanged-=new
EventHandler(connection_cm_OnConnectionStateChanged);
connection_cm.Disconnect();
}
Close();
}

Formating of the above look real ugly!

Settting ThreadStatus enum tells the business logic we want to quit. At this
point the thread is resumed which does clean up like deleting the CE database.

I don't want control to return until this thread has finished. I tried a
while(worker_thread.IsAlive)
{
//wait
}

But this seems to use up so much processing power it takes forever for
control to return.

Regards
Simon.
 
G

Guest

Use a synchronization object or an event from the worker thread to let the
main thread know it's done.

-Chris
 
G

Guest

I have figured it out. Use OpenNETCF.Threading.ThreadEx class, ThreadEx has a
Join method which blocks the calling thread until the worker has finished.
Neat!

Of course there are other ugly ways of doing the above like async web
service calls but then you get exceptions when terminating an async call
early which gets messy.

Cheers
Simon.
 
G

Guest

Yep, exactly why I wrote it.

-Chris


Simon Hart said:
I have figured it out. Use OpenNETCF.Threading.ThreadEx class, ThreadEx has
a
Join method which blocks the calling thread until the worker has finished.
Neat!

Of course there are other ugly ways of doing the above like async web
service calls but then you get exceptions when terminating an async call
early which gets messy.

Cheers
Simon.



Simon Hart said:
Hi,

I create a worker thread to create a local CE database then connect to a
webservice to bring back data. This process runs in the business logic
tier
which communicates to the client UI thread via a passed parameter. It
communicates with the UI thread to see if a cancel request has occured
and
also to update the status (progress bar etc).

What I would like to know is how can I make the UI thread wait until the
worker thread has returned from the call to the web service in order to
perorm some further processing like deleting the CE database before
control
is returned to the calling UI program.

I know it sounds confusing. Here is a snip of the UI cancel method:

private void cancel_push_Click(object sender, System.EventArgs e)
{
//Check if we are running any worker threads or not.
if (worker_thread!=null)
{
if (worker_thread.IsAlive)
{
worker_thread.Suspend();
DialogResult _result =
MessageBox.Show(MyResources.Strings["middleofcreatingalogbook_msg"],
this.Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (_result==DialogResult.Yes)
{
//Then cancel the current operation.
Cache.ThreadStatus=WorkerThreadStatus.Exception;
//Resume the thread to let it finish doing its job.
worker_thread.Resume();

}
else
{
//Then continue.
worker_thread.Resume();
return;
}
}
}
//Ensure we disconnect from the Connection Manager.
if (connection_cm!=null)
{
connection_cm.OnConnectionStateChanged-=new
EventHandler(connection_cm_OnConnectionStateChanged);
connection_cm.Disconnect();
}
Close();
}

Formating of the above look real ugly!

Settting ThreadStatus enum tells the business logic we want to quit. At
this
point the thread is resumed which does clean up like deleting the CE
database.

I don't want control to return until this thread has finished. I tried a
while(worker_thread.IsAlive)
{
//wait
}

But this seems to use up so much processing power it takes forever for
control to return.

Regards
Simon.
 

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