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 <su******@icebergwireless.com>
| Subject: Re: Threads and winform
| Date: Thu, 6 Nov 2003 14:11:38 -0600
| References: <O1**************@TK2MSFTNGP12.phx.gbl>
<u0**************@TK2MSFTNGP11.phx.gbl>
| 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: <eu**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 216.17.90.91
| Lines: 1
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.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 <u0**************@TK2MSFTNGP11.phx.gbl>,
10*@100.com 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
| >
|