Click to See Complete Forum and Search --> : Help with theads
radicallycritic
February 18th, 2007, 06:31 PM
Hello Gurus,
I am new to programming. I came across reading with threads.
If i create 5 working threads, now how will i stop a specific thread, for example the 3rd created thread in this:
for(i=1;i<=5;i++){
Thread trd = New Thread(myThreadMethod);
trd.IsBackground = true;
trd.Start();
}
Am i right here, i created 5 threads? How will i stop specific thread, lets say the thread created if i = 3??
Thanks,
Newbie!
jusstujoo
February 19th, 2007, 02:27 AM
Please use CodeTags when you provide code. It helps in easy understanding.
This should solve your problem:
http://www.codeguru.com/forum/showthread.php?t=364785
Following link is a good help regarding threads:
http://www.albahari.com/threading/
Also, to stop the thread, its similar like the way you start it.
trd.Stop();
In your case, I do not see any array or list that could maintain the list of Threads? How would you refer to specific thread to stop it? Otherwise if you were having an array then, you could have stopped it by using:
trd[0].Stop()
trd[1].Stop()
... so on so forth.
Now that you aren't using any list, you may try using some sort of flags? There is a good example in msdn that uses flags to stop the thread. Check the following link at http://msdn2.microsoft.com/en-us/library/7a2f3ay4.aspx
Thanks.
torrud
February 19th, 2007, 04:54 AM
Also, to stop the thread, its similar like the way you start it.
trd.Stop();
The thread class does not contain a Stop() method if I remember correctly.
It is unusual to stop a thread. In most cases you start a thread for doing a task which needs a long time and after doing the work the thread finished by itself. Sometimes you want to cancel the thread and in this case you should use the Thread.Abort() method. This will terminates the thread and throwing an AbortException() to the main thread.
But jusstujoo is right as he said you need a reference to the thread you want to cancel. If you do it in this way you loose the reference of a thread after each loop.
for(i=1;i<=5;i++)
{
Thread trd = New Thread(myThreadMethod);
trd.IsBackground = true;
trd.Start();
}
It is better to modify the code in something like this:
int count = 5;
Thread[] threads = new Thread[count];
for(i = 0; i < count; i++)
{
Thread trd = New Thread(myThreadMethod);
trd.IsBackground = true;
trd.Start();
threads[i] = trd;
}
Now you are able the abort the thread number 3 for instance by calling:
threads[2].Abort(); //keep in mind that you have to catch the exception in your main thread
radicallycritic
February 19th, 2007, 06:54 PM
Thanks for the replies guyz.
This is infact the problem:
for(i = 0; i < 5; i++)
{
Thread trd = New Thread(myThreadMethod);
trd.IsBackground = true;
trd.Start();
}
Threads are created not in an array list but the code above. my question here is can we possibly trap or get a unique identification for each created thread?
Thank you very much guyz!
-Rad (Highschool student)
wildfrog
February 19th, 2007, 07:06 PM
my question here is can we possibly trap or get a unique identification for each created thread?
Well, you hold a reference to the thread object - trd. With that reference you can identify the thread.
Threads are created not in an array list but the code above. Then you should change the code and add the reference to a collection. This way you can access it at a later stage.
- petter
radicallycritic
February 19th, 2007, 11:34 PM
Well, you hold a reference to the thread object - trd. With that reference you can identify the thread.
Then you should change the code and add the reference to a collection. This way you can access it at a later stage.
- petter
petter the code is already given so the problem as pointed above is how to reference it using that EXISTING CODE.
Thanks Guyz!
-Rad (HighSchool Student)
torrud
February 20th, 2007, 03:33 AM
Looks bad for you. I think you do not have a real chance to reference the threads.
wildfrog
February 20th, 2007, 10:25 AM
Maybe you can use Thread.CurrentThread from within the the thread procedure to get a reference, and then add it to some kind of collection (synchronized ofcourse).
With such a solution you cannot know if the 1. thread in the collection corresponds to the thread created when i = 1, due to the nature of non-synchronized threads. But that might not be a problem?
- petter
Fishdawg65
February 20th, 2007, 10:28 AM
It looks and sounds like you want us to do your homework for you. If this is the case and your teacher really thinks that you can specify which thread to end then he must surely be mistaken. The threads should be put into a collection of some sort like others have stated. Also, you should probably be asking your teacher for help rather than getting the answer off of here...
dcell59
February 20th, 2007, 04:48 PM
It seems to me that we're missing most of the problem here. All we have is a few lines of invariant code, and no other information or rules. Can we do anything we want in myThreadMethod and outside of this code? If so, we could have a global data structure that would allow myThreadMethod to "register" each thread as they are created. This would then allow the programmer to find the 3rd thread and kill it.
Of course, this really is a hokey question. It would be interesting to know what the point of it is (other than as a terrible example)...
wildfrog
February 20th, 2007, 05:02 PM
Can we do anything we want in myThreadMethod and outside of this code? If so, we could have a global data structure that would allow myThreadMethod to "register" each thread as they are created. This would then allow the programmer to find the 3rd thread and kill it.This is what is tried to say in my last post, there is only one problem.
If you create 5 threads in a loop you cannot assume that the 1. thread created (or started) is the 1. thread to add itself to the collection. So, the first thread in the collection might actually be the 2. (3., 4. or 5.) thread.
- petter
radicallycritic
February 21st, 2007, 07:11 PM
Guyz thanks for this..
how will i use it in collections rather than in array.
thanks!
dcell59
February 21st, 2007, 07:25 PM
An array is a collection (there are lots of types of collections, and an array is one type).
What we are saying is that you need to save some sort of data about the threads somewhere so you can get at it later.
Note that all of this hinges on the idea that you have to use the code you listed, and that you only have control over myThreadProc and over code before and after the code listed.
It really would help us if you could explain the point of this exercise. Is your teacher trying to get you to dig deep into C# for some kind of extra credit? This just reminds me of an interview question where you are stuck with some dumb code that you can't fix, so you have to work around it.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.