Thread Aorting
-
hi all, i tried to abort thread but when i make myThread.Abort(); it give me an exception of aborting thread so what can i do to avoid this exception another thing is that Suspend Method is obsolete(help said) what is the alternative third and last: when i declare thread as member data in class it refuses to identify the name of the method i used to initialize like: Thread packetsMovingThread = new Thread(new ThreadStart(threadProcedure)); private threadProcedure() { my method } thanx Generator
-
hi all, i tried to abort thread but when i make myThread.Abort(); it give me an exception of aborting thread so what can i do to avoid this exception another thing is that Suspend Method is obsolete(help said) what is the alternative third and last: when i declare thread as member data in class it refuses to identify the name of the method i used to initialize like: Thread packetsMovingThread = new Thread(new ThreadStart(threadProcedure)); private threadProcedure() { my method } thanx Generator
HexaDeveloper wrote:
i tried to abort thread but when i make myThread.Abort(); it give me an exception of aborting thread so what can i do to avoid this exception
Allow the thread to end naturally. Typically, a thread method has a loop that it runs until it completes its task.
private void ThreadMethod
{
while(notDone)
{
// Do stuff here...
}
}HexaDeveloper wrote:
another thing is that Suspend Method is obsolete(help said) what is the alternative
Hmm, well you could use locking to pause a thread:
private void ThreadMethod
{
lock(lockObject)
{
Monitor.Wait(lockObject);
}while(!done) { // Do stuff here... }
}
And at the point in which you create the thread:
Thread myThread = new Thread(ThreadMethod);
myThread.Start();
lock(lockObject)
{
Monitor.Pulse(lockObject);
}This will create a thread and start it. The thread runs until it reaches the
Monitor.Wait(lockObject)
statement. At that point, it's up to you to wake up the thread and let it do its thing. This is done above with theMonitor.Pulse(lockObject)
statement. Now, often times you want a thread to wait periodically instead of just when it's started up. So we can put a Wait inside the loop:private void ThreadMethod
{
lock(lockObject)
{
while(!done)
{
Monitor.Wait(lockObject);if(done) { break; } // Do stuff here... } }
}
Then it's up to your application to periodically wake up the thread to do its work. Hope this helps.
HexaDeveloper wrote:
third and last: when i declare thread as member data in class it refuses to identify the name of the method i used to initialize like:
This question I don't understand. Can you elaborate?
-
HexaDeveloper wrote:
i tried to abort thread but when i make myThread.Abort(); it give me an exception of aborting thread so what can i do to avoid this exception
Allow the thread to end naturally. Typically, a thread method has a loop that it runs until it completes its task.
private void ThreadMethod
{
while(notDone)
{
// Do stuff here...
}
}HexaDeveloper wrote:
another thing is that Suspend Method is obsolete(help said) what is the alternative
Hmm, well you could use locking to pause a thread:
private void ThreadMethod
{
lock(lockObject)
{
Monitor.Wait(lockObject);
}while(!done) { // Do stuff here... }
}
And at the point in which you create the thread:
Thread myThread = new Thread(ThreadMethod);
myThread.Start();
lock(lockObject)
{
Monitor.Pulse(lockObject);
}This will create a thread and start it. The thread runs until it reaches the
Monitor.Wait(lockObject)
statement. At that point, it's up to you to wake up the thread and let it do its thing. This is done above with theMonitor.Pulse(lockObject)
statement. Now, often times you want a thread to wait periodically instead of just when it's started up. So we can put a Wait inside the loop:private void ThreadMethod
{
lock(lockObject)
{
while(!done)
{
Monitor.Wait(lockObject);if(done) { break; } // Do stuff here... } }
}
Then it's up to your application to periodically wake up the thread to do its work. Hope this helps.
HexaDeveloper wrote:
third and last: when i declare thread as member data in class it refuses to identify the name of the method i used to initialize like:
This question I don't understand. Can you elaborate?
hi, i mean when i write Thread myThread = new Thread(ThreadMethod); it said [Error 1 A field initializer cannot reference the nonstatic field, method, or property 'Interface.mainScreen.threadProcedure()' C:\Documents and Settings\Administrator\Desktop\Well Formed Interface_thread\Interface\mainScreen.cs 38 49 Interface ] and i am actually want to know the reason that when i used abort it makes exception and i want to ask if i can make the lockobject my thread name thanx -- modified at 12:02 Wednesday 11th April, 2007 Generator -- modified at 14:19 Wednesday 11th April, 2007