Hi, Im using
public void threadcontrol()
{
Thread scan = new Thread (new ThreadStart(GethtmlStream));
scan.Start();
name = "Scanning";
}
in a class, and calling it from button1 with
BJresults sr = new BJresults();
sr.threadcontrol();
np problems here, thread starts and does what it supposed to do. I want
to be able to stop the thread from button2 and I cant figure out how
to. Any ideas.
Regars Robert 8 1783
Not sure but maybe try making the thread a member variable instead of a
local variable. That way you can call it outside the method to stop it.
Use a manualresetevent (or auto) to signal the thread that it's time to
quit. http://msdn2.microsoft.com/en-us/lib...esetevent.aspx
You'll need to loop in your thread and repeatedly call the waitone
method to check if it's been signalled.
Also, I'm curious what a class called "BJResults" does. Ro********@yahoo.co.uk wrote: Hi, Im using public void threadcontrol() {
Thread scan = new Thread (new ThreadStart(GethtmlStream)); scan.Start(); name = "Scanning"; } in a class, and calling it from button1 with BJresults sr = new BJresults(); sr.threadcontrol();
np problems here, thread starts and does what it supposed to do. I want to be able to stop the thread from button2 and I cant figure out how to. Any ideas. Regars Robert
Use a bool flag to signal when it's time for the thread to shutdown
gracefully. The following article explains the pattern in detail.
<http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml>
If your worker thread spends a lot of time blocked on a WaitHandle then
you'd probably want to signal that WaitHandle in conjunction with
setting a flag. I suppose you could also use a separate WaitHandle in
addition to the existing WaitHandle that would signal the thread to
shutdown. In that case the thread could block for either signal.
You'd use WaitHandle.WaitAny to do that.
Brian Ro********@yahoo.co.uk wrote: Hi, Im using public void threadcontrol() {
Thread scan = new Thread (new ThreadStart(GethtmlStream)); scan.Start(); name = "Scanning"; } in a class, and calling it from button1 with BJresults sr = new BJresults(); sr.threadcontrol();
np problems here, thread starts and does what it supposed to do. I want to be able to stop the thread from button2 and I cant figure out how to. Any ideas. Regars Robert
Thankyou both for your replys. Ill read through the link given,
thankyou for this. I will have to read up on what a member variable is
as im still a begginer. Basically the class Ive written is to read data
from a text file, go to a web site(relevent to data in text file) read
the html into stringbuilder and parse the relevent data to a text file.
Theres something like 75000 records to gather.
Regards Robert Ro********@yahoo.co.uk wrote: Thankyou both for your replys. Ill read through the link given, thankyou for this. I will have to read up on what a member variable is as im still a begginer. Basically the class Ive written is to read data from a text file, go to a web site(relevent to data in text file) read the html into stringbuilder and parse the relevent data to a text file. Theres something like 75000 records to gather. Regards Robert
Member variable = variable that can be accessed anywhere in your class.
local variable = variable can only be accessed within a method.
Sounds like you just need to write a console app that will exit when
complete. If this is the case you wouldn't even need to create a
thread unless you need to do multiple things at once.
Thanks again for your replys. I originally wrote this as a console
application, but i needed to get feed back on how many records have
been processed. Because Im looking at proccessing over 75000 records, I
need to stop the thread and continue another day. In other posts I was
advised to take the threading route. So far Ive learnt how to write a
thread and start the thread, which Im really pleased with getting this
far. I have to say for a begginer its a big topic, and Ive along way to
go. Once Im able to stop the thread, I need to add a errorhandler as
well, then I need to see how to get feed back about records processed.
Ive been reading up on this and that doesnt look to bad. In fact the
only reason I realised I needed to stop the thread is I clossed do
VS2005, and saw the thread was still running and had to end it in
procceses. I would have struglled more without these replys. Thanks
again.
Regards Robert
Just out of curiousity, why would you use the bool flag in conjunction
with the waithandle? Why isn't the wait handle enough? Access is
synchronized already and you also have the added benefit that you can
use the event globally to shutdown all worker threads.
I usually use a pattern similar to this:
//thread work routine:
public void Start()
{
//the time can be increased if the thread is more of a
"polling" object
while(!_manualEvent.WaitOne(1,true))
{
//do work here, make sure it's broken up into small
enough chunks that
//the event is checked continually
}
}
Brian Gideon wrote: Use a bool flag to signal when it's time for the thread to shutdown gracefully. The following article explains the pattern in detail.
<http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml>
If your worker thread spends a lot of time blocked on a WaitHandle then you'd probably want to signal that WaitHandle in conjunction with setting a flag. I suppose you could also use a separate WaitHandle in addition to the existing WaitHandle that would signal the thread to shutdown. In that case the thread could block for either signal. You'd use WaitHandle.WaitAny to do that.
Brian
Ro********@yahoo.co.uk wrote: Hi, Im using public void threadcontrol() {
Thread scan = new Thread (new ThreadStart(GethtmlStream)); scan.Start(); name = "Scanning"; } in a class, and calling it from button1 with BJresults sr = new BJresults(); sr.threadcontrol();
np problems here, thread starts and does what it supposed to do. I want to be able to stop the thread from button2 and I cant figure out how to. Any ideas. Regars Robert
I was referring to scenarios where a thread may already be using a
WaitHandle for purposes other than shutting down. If you just used the
existing WaitHandle then how would the thread know that it was suppose
to shut down instead of doing the intended operation?
In your example I would just use a bool flag instead of the
ManualResetEvent especially if there is no reason for the thread to
sleep. But, you bring up a good point in the comments about threads
that wake up on fixed intervals to do work. In that case I think your
example is fine. In fact, that's what I do as well. I didn't even
think to mention that common scenario.
Brian sd********@gmail.com wrote: Just out of curiousity, why would you use the bool flag in conjunction with the waithandle? Why isn't the wait handle enough? Access is synchronized already and you also have the added benefit that you can use the event globally to shutdown all worker threads.
I usually use a pattern similar to this:
//thread work routine: public void Start() {
//the time can be increased if the thread is more of a "polling" object while(!_manualEvent.WaitOne(1,true)) { //do work here, make sure it's broken up into small enough chunks that //the event is checked continually
} } Brian Gideon wrote: Use a bool flag to signal when it's time for the thread to shutdown gracefully. The following article explains the pattern in detail.
<http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml>
If your worker thread spends a lot of time blocked on a WaitHandle then you'd probably want to signal that WaitHandle in conjunction with setting a flag. I suppose you could also use a separate WaitHandle in addition to the existing WaitHandle that would signal the thread to shutdown. In that case the thread could block for either signal. You'd use WaitHandle.WaitAny to do that.
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jim Hubbard |
last post by:
I am reposting a portion of a thread that I am involved in under a new topic
because it seems that there are still people that believe the whole "DLL
Hell" myth.
I hope I can shed some light on...
|
by: roger beniot |
last post by:
I have a program that launches multiple threads with a ThreadStart
method like the following (using System.Net.Sockets.Socket for UDP
packet transfers to a server):
ThreadStart pseudo code:
...
|
by: Shanli RICHEZ |
last post by:
I'm using ASP.NET and quite a lot of object in memory instead of a database.
The question is that I tried to save the data, serialize them in a XML file
to save them when the application is...
|
by: Ricky |
last post by:
I'm trying to run a program directly from the web.
It's working perfectly, but a simple "End" statement
or "Application.Exit" do not work for me and I get
permission errors.
How can I end a...
|
by: cj |
last post by:
I run this program and to exit click the X in the upper right corner.
But apparently it isn't really ending the program. If I return to VB
and make changes then try to rebuild the app it says the...
|
by: Carl Klouda |
last post by:
I'm building a VB 2005 app (.Net 2.0) that performs searches against our
active directory.
It's pretty basic. I have a UI form that takes search parameters and feeds
them to a class I created. ...
|
by: ManuStone |
last post by:
Hi All
I'm newbe to multithreading. Readings info on MSDN for MFC classes it seems that doesn't exist a function, like the pThread libraries, to wait that a Thread started with AfxBeginThread to its...
|
by: morangolds |
last post by:
Hi,
I've been having a problem with C++ Windows Forms apps not "ending"
when you close the form window. I've searched about this problem all
over the place and most searches have lead me to...
|
by: Jon Slaughter |
last post by:
Since a thread doesn't have a Stop feature and I'm not supose to use abort,
I'm wondering how I stop a thread?
My problem is that I simply want to excute a function in the background and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
| |