Threads and winform

  • Thread starter Thread starter Sunny
  • Start date Start date
S

Sunny

Hi,
If I have (hmm, at least I want to have :) ) a winform, displaying the
progress of a long duration method, which is running in a separate
thread, how do I implement canceling the thread, if I click a button on
the form.
Any directions, examples, etc.

Thanks
Sunny
 
Hi Sunny,

I would suggest using ManualResetEvent object as a flag for canceling the
worker thread.
When you want to cancel the worker thread you can set the event
(event.Set()) then the worker thread has to periodically check the state of
the event

if(event.WaitOne(0,false))
{
//stop the work and exit
}

When the thread starts it should reset the event (event.Reset())

HTH
B\rgds
100
 
Hi 100,
I have thought about such a thing, but I needed some working example.
And the things are a little bit more complicated, 'because I forgot to
mention, that actually this thread is invoking a remoting method, which
takes a long time to execute ...

But, if I see some good example for a non remoting program, I most
probably will figure out hot to marshal the events, etc.

Thanks
Sunny
 
Hi Sunny,

I think 100 has already provided you a clear way.
You only need follow his way to write a sample:

static AutoResetEvent myResetEvent = new AutoResetEvent(false);
private void button2_Click(object sender, System.EventArgs e)
{
myResetEvent.Set();
}

private void button1_Click(object sender, System.EventArgs e)
{
Thread t=new Thread(new ThreadStart(longwork));
t.Start();
}

void longwork()
{
while(true)
{
if(myResetEvent.WaitOne(0,false))
{
Console.WriteLine("quit");
return;
}
Console.WriteLine("running!");
}
}

In project setting, change the project running as console application, then
you can see the console output of this application.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: Sunny <[email protected]>
| Subject: Re: Threads and winform
| Date: Thu, 6 Nov 2003 14:11:38 -0600
| References: <[email protected]>
<[email protected]>
| Organization: Iceberg Wireless LLC
| MIME-Version: 1.0
| Content-Type: text/plain; charset="iso-8859-15"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: MicroPlanet Gravity v2.60
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 216.17.90.91
| Lines: 1
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:197289
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi 100,
| I have thought about such a thing, but I needed some working example.
| And the things are a little bit more complicated, 'because I forgot to
| mention, that actually this thread is invoking a remoting method, which
| takes a long time to execute ...
|
| But, if I see some good example for a non remoting program, I most
| probably will figure out hot to marshal the events, etc.
|
| Thanks
| Sunny
|
| In article <[email protected]>, (e-mail address removed) says...
| > Hi Sunny,
| >
| > I would suggest using ManualResetEvent object as a flag for canceling
the
| > worker thread.
| > When you want to cancel the worker thread you can set the event
| > (event.Set()) then the worker thread has to periodically check the
state of
| > the event
| >
| > if(event.WaitOne(0,false))
| > {
| > //stop the work and exit
| > }
| >
| > When the thread starts it should reset the event (event.Reset())
| >
| > HTH
| > B\rgds
| > 100
| >
|
 
You might use Thread.Abort method and catch ThreadAbortException within
thread.

Miha
 
Back
Top