Click here to Skip to main content
16,008,490 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# and COM InterOp problems Pin
leppie20-Aug-02 7:57
leppie20-Aug-02 7:57 
GeneralRe: C# and COM InterOp problems Pin
James T. Johnson20-Aug-02 8:01
James T. Johnson20-Aug-02 8:01 
GeneralRe: C# and COM InterOp problems Pin
leppie20-Aug-02 10:33
leppie20-Aug-02 10:33 
GeneralBuild Error Pin
albean19-Aug-02 13:01
albean19-Aug-02 13:01 
GeneralRe: Build Error - Solved Pin
albean19-Aug-02 14:37
albean19-Aug-02 14:37 
GeneralDesign question Pin
Christian Graus19-Aug-02 12:28
protectorChristian Graus19-Aug-02 12:28 
GeneralRe: Design question Pin
Domenic Denicola19-Aug-02 13:29
Domenic Denicola19-Aug-02 13:29 
GeneralCode Review needed Pin
ke5in19-Aug-02 10:57
ke5in19-Aug-02 10:57 
Consider this C# code:

///////
public class Worker
{

public ManualResetEvent MyEvent;
private Thread t;
private string message = "nothing now";

//Constructor
public Worker(string name)
{
t = new Thread(new ThreadStart(this.Work));
t.Name = "Worker: " + name;
t.Start();
//I don't like the following line:
while(t.ThreadState == ThreadState.Unstarted){}
}


//Property
public string Message
{
get{return message;}
set{message = value;}
}

//Thread function
public void Work()
{
MyEvent = new ManualResetEvent(false);
for(;;)//Loop forever
{
MyEvent.Reset();
MyEvent.WaitOne(); //Wait until "MyEvent" is Set
Console.Write("Message:" + message + ".\r\n");
}
}

//terminate the thread
public void kill()
{
t.Abort();
}
}

//////
Worker w = new Worker("Thread 1");
w.Message="Something";
w.MyEvent.Set();
....
w.Message="Something else";
w.MyEvent.Set();
...
w.kill();

----------------------------------------------------------------

Q1: Is encapsulating the thread in the class like this bad practice?

Q2: Assuming that there is nothing wrong with encapsulating the thread, is there anyway I can terminate it in a destructor? I would like to see this thread die when the instance of the class goes out of scope. I think explicitly calling the kill method is lame. (Or do I just have to get used to the way things are destroyed in C#?)

Q3: What is the “approved” method of waiting for a thread to start?
i.e. is there anything better than this:
while(t.ThreadState == ThreadState.Unstarted){}


-----
Any other comments would be appreciated.

Thanks,
-Kevin



GeneralRe: Code Review needed Pin
Sijin19-Aug-02 19:33
Sijin19-Aug-02 19:33 
GeneralRe: Code Review needed Pin
leppie20-Aug-02 2:22
leppie20-Aug-02 2:22 
GeneralRe: Code Review needed Pin
Paul Riley20-Aug-02 6:49
Paul Riley20-Aug-02 6:49 
GeneralCOM or Remoting or MSMQ or... Need ideas on localhost communication Pin
poodull19-Aug-02 10:02
poodull19-Aug-02 10:02 
GeneralRe: COM or Remoting or MSMQ or... Need ideas on localhost communication Pin
TigerNinja_21-Aug-02 12:49
TigerNinja_21-Aug-02 12:49 
GeneralWizard Dialog Pin
laphijia19-Aug-02 9:03
laphijia19-Aug-02 9:03 
GeneralRe: Wizard Dialog Pin
TigerNinja_21-Aug-02 13:19
TigerNinja_21-Aug-02 13:19 
GeneralStripping HTML tags Pin
Nnamdi Onyeyiri19-Aug-02 7:34
Nnamdi Onyeyiri19-Aug-02 7:34 
GeneralRe: Stripping HTML tags Pin
leppie19-Aug-02 8:51
leppie19-Aug-02 8:51 
GeneralRe: Stripping HTML tags Pin
Nnamdi Onyeyiri19-Aug-02 8:54
Nnamdi Onyeyiri19-Aug-02 8:54 
GeneralRe: Stripping HTML tags Pin
Nick Parker19-Aug-02 12:01
protectorNick Parker19-Aug-02 12:01 
GeneralXmlSerializer and interfaces Pin
Ryan Cromwell19-Aug-02 5:36
Ryan Cromwell19-Aug-02 5:36 
GeneralRe: XmlSerializer and interfaces Pin
Andy Smith19-Aug-02 19:52
Andy Smith19-Aug-02 19:52 
GeneralRe: XmlSerializer and interfaces Pin
leppie20-Aug-02 2:30
leppie20-Aug-02 2:30 
GeneralRe: XmlSerializer and interfaces Pin
Ryan Cromwell20-Aug-02 4:18
Ryan Cromwell20-Aug-02 4:18 
GeneralRe: XmlSerializer and interfaces Pin
leppie20-Aug-02 4:50
leppie20-Aug-02 4:50 
GeneralRe: XmlSerializer and interfaces Pin
James T. Johnson20-Aug-02 6:41
James T. Johnson20-Aug-02 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.