Sunday, July 12, 2009

C# Threading question.?

I created a work thread using the Thread class. In that thread a UdpClient reveives datagrams and writes them to an xml file.


The problem is that when I close the main window the work thread doesnot exit and I have to terminate the process in the task manager. Can anyone help me out? Thanks a million!


BTW: Is there anyway I can compile my c# program into binary machine code so that I can run it on a PC without .Net runtime?

C# Threading question.?
You need to set the IsBackground property on the thread to true.





I don't know why, but they decided to make threads foreground by default which means that they are not terminated by main thread exit.





In response to your second question, Microsoft has an incubation project dealing with compiling managed code to something that has no Fx dependency. I can't provide any details other than you might see something like that in the future but it does not exist right now.
Reply:You could call the Abort() method on the thread instance in your app. For example:


//Called when "Exit" is selected from the context menu.


protected void Exit (object sender, System.EventArgs e){


if(myThread.IsAlive){


myThread.Abort();


}


this.Close();


}





Concerning the .Net framework, if you compile any code with a compiler that targets the .NET Framework, the compiler compiles to Common Intermediate Language (CIL) which is assembled into bytecode. Therefore, the answer is no, you need the .Net common language runtime.


No comments:

Post a Comment