manzoor10
January 12th, 2009, 06:49 AM
Hey when I abort a thread it throws a ThreadAbortException right?
I catch it, it is re thrown again but the first time I catch it I call the function ResetAbort(), but the thread still aborts :(?
Help please....
Here's my code
// Threading.cpp : main project file.
#include "stdafx.h"
using namespace System;
using namespace System::Threading;
ref class MyThread
{
public:
static void ThreadFunc(Object ^Name);
};
void MyThread::ThreadFunc(Object ^Name)
{
Thread ^thr = Thread::CurrentThread;
try
{
for (int i = 0; i < 100; i++)
{
Console::WriteLine("{0} {1}", Name, i.ToString());
Thread::Sleep(10);
}
return;
}
catch (ThreadAbortException^)
{
Console::WriteLine("{0} Aborted", Name);
// Reset the abort so that the meoth will continue processing
thr->ResetAbort();
}
}
void main()
{
Console::WriteLine("Main Program Starts");
Thread ^thr1 = gcnew Thread(gcnew ParameterizedThreadStart(&MyThread::ThreadFunc));
Thread ^thr2 = gcnew Thread(gcnew ParameterizedThreadStart(&MyThread::ThreadFunc));
thr1->Start("Thread1");
thr2->Start("Thread2");
Thread::Sleep(20);
thr1->Abort();
Thread::Sleep(40);
thr2->Abort();
Console::WriteLine("Main Program Ends");
}
I catch it, it is re thrown again but the first time I catch it I call the function ResetAbort(), but the thread still aborts :(?
Help please....
Here's my code
// Threading.cpp : main project file.
#include "stdafx.h"
using namespace System;
using namespace System::Threading;
ref class MyThread
{
public:
static void ThreadFunc(Object ^Name);
};
void MyThread::ThreadFunc(Object ^Name)
{
Thread ^thr = Thread::CurrentThread;
try
{
for (int i = 0; i < 100; i++)
{
Console::WriteLine("{0} {1}", Name, i.ToString());
Thread::Sleep(10);
}
return;
}
catch (ThreadAbortException^)
{
Console::WriteLine("{0} Aborted", Name);
// Reset the abort so that the meoth will continue processing
thr->ResetAbort();
}
}
void main()
{
Console::WriteLine("Main Program Starts");
Thread ^thr1 = gcnew Thread(gcnew ParameterizedThreadStart(&MyThread::ThreadFunc));
Thread ^thr2 = gcnew Thread(gcnew ParameterizedThreadStart(&MyThread::ThreadFunc));
thr1->Start("Thread1");
thr2->Start("Thread2");
Thread::Sleep(20);
thr1->Abort();
Thread::Sleep(40);
thr2->Abort();
Console::WriteLine("Main Program Ends");
}