Threads, and refernced class that run Threads

  • Thread starter Thread starter want 2 Learn
  • Start date Start date
W

want 2 Learn

Guest
I have a service which create a class A.

this class A start a thread :


public class A{
private Thread thread;
public A(){
thread = new Thread(Send);
thread.Name = "Connector_Send";
thread.Start();
}
~A()
{
if (thread.IsAlive) thread.Abort();
}
public void Stop() //called when service stopped
{
if (thread.IsAlive) thread.Abort();
}
private void Send(){



B b=new B();


while(true){

……..
}

}


class B :

public class B{
private Thread thread;
public B(){
thread = new Thread(Send2);
thread.Name = "Connector_Send2";
thread.Start();
}
~B()
{
if (thread.IsAlive) thread.Abort();
}
public void Stop() //called when service stopped
{
if (thread.IsAlive) thread.Abort();
}
private void Send2(){

while(true){
……..
}

}


in class B, I have a WCF that send message all the time, and a ping every 10 seconds.

it seems that when class A get stop() event, class B not terminated immediacy as well (I see that the WCF connection is up and for some time).

maybe my approach not correct?

maybe the B should be declared not in the thread but rather in the class?

Continue reading...
 
Back
Top