R
russ.haley
Threading and suspend
I have an application that will load x number of "communicators". These
"communicators" start up a communications channel with another
application(of no importance). The initial code loaded the object
model and then initialized the communications on the same thread, which
took a lot of time. It took a minute plus to load four or five
communicators.
I found that if I can move the communications loading to another thread
it greatly improves the overall load time to about 10 seconds for 100
communicators. The problem was when I realeased the thread, the
objects were destroyed too. Most noteably the Asynchronous call back
for a NetworkStream. I found that if I suspend the thread, all the
objects stay around and processing continues fine and performs quite
well, but I am uncertain about leaving a thread just "hanging" there
suspended. In the dispose method of the parent object, I simple resume
the thread if it's suspended and everthing seems to exit cleanly.
Is suspending a thread and using the objects it creates an acceptable
threading methodology? Do the Asynchrounous callbacks use the suspended
thread or the main thread? Any insight would be helpful. A breif
example below...
Thanks in Advance
Russ
Class Parent
{
private Thread _thrMyThread;
private MyTCPClientWrapper _clsMyWrapper;
public Parent()
{ //The object model needs to be loaded before the constructor
exits
LoadObjectModel();
_thrMyThread = new Thread(new ThreadStart(InitCommunications));
_thrMyThread.Start();
}
private void LoadObjectModel()
{
//Load Object here
_clsMyWrapper = new MyTCPClientWrapper("args");
}
private void InitCommunications()
{
//Load _clsMyWrapper Here
_thrMyThread.Suspend();
}
public void Dispose()
{
if(_thrMyThread.ThreadState == ThreadState.Suspended)
{
_thrMyThread.Resume();
}
}
}
I have an application that will load x number of "communicators". These
"communicators" start up a communications channel with another
application(of no importance). The initial code loaded the object
model and then initialized the communications on the same thread, which
took a lot of time. It took a minute plus to load four or five
communicators.
I found that if I can move the communications loading to another thread
it greatly improves the overall load time to about 10 seconds for 100
communicators. The problem was when I realeased the thread, the
objects were destroyed too. Most noteably the Asynchronous call back
for a NetworkStream. I found that if I suspend the thread, all the
objects stay around and processing continues fine and performs quite
well, but I am uncertain about leaving a thread just "hanging" there
suspended. In the dispose method of the parent object, I simple resume
the thread if it's suspended and everthing seems to exit cleanly.
Is suspending a thread and using the objects it creates an acceptable
threading methodology? Do the Asynchrounous callbacks use the suspended
thread or the main thread? Any insight would be helpful. A breif
example below...
Thanks in Advance
Russ
Class Parent
{
private Thread _thrMyThread;
private MyTCPClientWrapper _clsMyWrapper;
public Parent()
{ //The object model needs to be loaded before the constructor
exits
LoadObjectModel();
_thrMyThread = new Thread(new ThreadStart(InitCommunications));
_thrMyThread.Start();
}
private void LoadObjectModel()
{
//Load Object here
_clsMyWrapper = new MyTCPClientWrapper("args");
}
private void InitCommunications()
{
//Load _clsMyWrapper Here
_thrMyThread.Suspend();
}
public void Dispose()
{
if(_thrMyThread.ThreadState == ThreadState.Suspended)
{
_thrMyThread.Resume();
}
}
}