G
Guest
I am trying to execute a blocking call in a thread which times out after a
certain time.
The problem I am having is that the thread does not seem to die after it is
done doing its work. I have a MAIN that never dies, the problem I am having
is that either on timeout or on successfull completion of the "blockingcall"
the process space in which my MAIN runs still shows the created thread IDs as
active with a couple of references and handles. I am using process explorer
from SysInternals to look at the ThreadIDs
Code is below and am using this snippet in a web app.
thanks
Here is my main:
for(int i=1; i<=1; i++)
{
TTEncap TTE = new TTEncap(("Thread" + i.ToString()));
Thread myThread = new Thread(new ThreadStart(TTE.blockingCall));
int dueTime = 10000 + (i * 5000);
Timer timer1 = new Timer(new TimerCallback(TTE.OnTimer), myThread, dueTime,
Timeout.Infinite);
TTE.ThreadTimer = timer1;
myThread.Start();
}
Console.WriteLine("Main runs on Thread: {0}\n",
AppDomain.GetCurrentThreadId());
// Main keeps working on something else forever (MAIN NEVER DIES)
HERE is the TTE Class:
public class TTEncap
{
public string TEName = null;
public Timer ThreadTimer;
public TTEncap(string tName)
{
TEName = tName;
}
public void blockingCall()
{
try
{
Console.WriteLine("BlockingCall runs on Thread: {0}\n",
AppDomain.GetCurrentThreadId());
while(true)
{
}
}
catch (Exception ex)
{
Console.WriteLine("Exception BC {0}: {1}\n",
AppDomain.GetCurrentThreadId(), ex.Message);
}
finally
{
this.ThreadTimer.Dispose();
}
}
public void OnTimer(object obj)
{
try
{
Console.WriteLine("TTEncap Name: {1} Timer runs on Thread:
{0}\n", AppDomain.GetCurrentThreadId(), TEName);
Thread tt = (Thread) obj;
tt.Abort();
}
catch (Exception ex)
{
Console.WriteLine("Exception OnTimer: {0}\n", ex.Message);
}
}
}
certain time.
The problem I am having is that the thread does not seem to die after it is
done doing its work. I have a MAIN that never dies, the problem I am having
is that either on timeout or on successfull completion of the "blockingcall"
the process space in which my MAIN runs still shows the created thread IDs as
active with a couple of references and handles. I am using process explorer
from SysInternals to look at the ThreadIDs
Code is below and am using this snippet in a web app.
thanks
Here is my main:
for(int i=1; i<=1; i++)
{
TTEncap TTE = new TTEncap(("Thread" + i.ToString()));
Thread myThread = new Thread(new ThreadStart(TTE.blockingCall));
int dueTime = 10000 + (i * 5000);
Timer timer1 = new Timer(new TimerCallback(TTE.OnTimer), myThread, dueTime,
Timeout.Infinite);
TTE.ThreadTimer = timer1;
myThread.Start();
}
Console.WriteLine("Main runs on Thread: {0}\n",
AppDomain.GetCurrentThreadId());
// Main keeps working on something else forever (MAIN NEVER DIES)
HERE is the TTE Class:
public class TTEncap
{
public string TEName = null;
public Timer ThreadTimer;
public TTEncap(string tName)
{
TEName = tName;
}
public void blockingCall()
{
try
{
Console.WriteLine("BlockingCall runs on Thread: {0}\n",
AppDomain.GetCurrentThreadId());
while(true)
{
}
}
catch (Exception ex)
{
Console.WriteLine("Exception BC {0}: {1}\n",
AppDomain.GetCurrentThreadId(), ex.Message);
}
finally
{
this.ThreadTimer.Dispose();
}
}
public void OnTimer(object obj)
{
try
{
Console.WriteLine("TTEncap Name: {1} Timer runs on Thread:
{0}\n", AppDomain.GetCurrentThreadId(), TEName);
Thread tt = (Thread) obj;
tt.Abort();
}
catch (Exception ex)
{
Console.WriteLine("Exception OnTimer: {0}\n", ex.Message);
}
}
}