Click here to Skip to main content
15,892,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to add one button onto a winForm with code in the design environment Pin
Henry Minute1-May-09 1:21
Henry Minute1-May-09 1:21 
GeneralRe: How to add one button onto a winForm with code in the design environment Pin
mctramp1681-May-09 2:46
mctramp1681-May-09 2:46 
GeneralRe: How to add one button onto a winForm with code in the design environment Pin
mmgbmm1-May-09 4:32
mmgbmm1-May-09 4:32 
Question[Message Deleted] Pin
saoda1-May-09 0:03
saoda1-May-09 0:03 
AnswerAnswer! Pin
Nagy Vilmos1-May-09 0:11
professionalNagy Vilmos1-May-09 0:11 
AnswerAnswer Pin
dan!sh 1-May-09 0:12
professional dan!sh 1-May-09 0:12 
AnswerRe: Question! Pin
SimpleData1-May-09 0:19
SimpleData1-May-09 0:19 
QuestionProblem with events code and thread (long post) Pin
Otex30-Apr-09 23:02
Otex30-Apr-09 23:02 
I have a Class with a from and a basic UI (Form1), from that UI I instantiate a class (in another thread) that initializes the serial port and listens for an event that is raised when data reaches the serial port (WorkerClass). in the method launched in the class I wait 10 seconds and then the thread exits.

I launch my form and send data to my serial port, as expected nothing happens

I launch the workerthread and immediately send data to my serial port, as expected the event is raised.

After 10 seconds my worker thread exits, I send more data to the serial port, then to my surprise the event is launched ?!?

Is this normal behaviour ?
Will it last or will the garbage collector clean it from memory ?

Code the worker class:
class WorkerClass
    {
        public delegate void addToRTBDelegate(String text);
        public event addToRTBDelegate eventRTBadd;
        private SerialPort comport = new SerialPort();

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            
            Console.WriteLine("Data received on serial port");
        }

        public void openCOMPort()
        {
            comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            comport.Open();
            Console.WriteLine("Comport is Open");
            
            for (int i = 1; i < 10; i++)
            {
                Thread.Sleep(1000);
            }
            Console.WriteLine("exited loop");
                
        }

     }


Here is my Output:

Test-Form-Thread-2.vshost.exe' (Managed): Loaded 'D:\ISS\C-SHarp\Test-From-Worker-1\Test-Form-Thread-2\bin\Debug\Test-Form-Thread-2.exe', Symbols loaded.
Comport is Open
Data received on serial port
The thread 0x27cc has exited with code 0 (0x0).
exited loop
Data received on serial port
Data received on serial port



What is happening is:
1. Start the main thread
2. Start the worker and get in a loop in the worker with a sleep
in the mean time the handler for the event is active and when data reaches the serial port, the handler sends a console message. (This is expected)
3. The worker thread exits
Now the strange part, the even handler is still active and will fire when data is received on the SP after the worker thread has terminated.

Seeing the threads runnning at various points (see below) I suspect the event handler creates a new thread that is not closed when the worker exits.

Threads before I start the worker
~	 	15648		[Thread Ended]			
0	 	4736	Worker Thread	<No Name>		Highest	0
0	 	13456	Worker Thread	<No Name>		Normal	0
0	 	13632	Worker Thread	<No Name>		Normal	0
0	 	15912	Worker Thread	<No Name>		Normal	0
0	 	11144	Worker Thread	vshost.RunParkingWindow		Normal	0
0	 	14892	Worker Thread	.NET SystemEvents		Normal	0
0	>	10840	Main Thread	Main Thread	Test_Form_Thread_2.Program.Main	Normal	0


While the worker is running


~	 	15648		[Thread Ended]			
0	 	4736	Worker Thread	<No Name>		Highest	0
0	 	13456	Worker Thread	<No Name>		Normal	0
0	 	13632	Worker Thread	<No Name>		Normal	0
0	 	15912	Worker Thread	<No Name>		Normal	0
0	 	11144	Worker Thread	vshost.RunParkingWindow		Normal	0
0	 	14892	Worker Thread	.NET SystemEvents		Normal	0
0	>	10840	Main Thread	Main Thread	Test_Form_Thread_2.Program.Main	Normal	0
0	 	15240	Worker Thread	ThreadClass1	Test_Form_Thread_2.WorkerClass.openCOMPort	Normal	0
0	 	3208	Worker Thread	<No Name>		Normal	0
0	 	12668	Worker Thread	<No Name>		Normal	0


After the worker exited
( the event is still activated when data reaches the serial port)

~	 	15648		[Thread Ended]			
0	 	4736	Worker Thread	<No Name>		Highest	0
0	 	13456	Worker Thread	<No Name>		Normal	0
0	 	13632	Worker Thread	<No Name>		Normal	0
0	 	15912	Worker Thread	<No Name>		Normal	0
0	 	11144	Worker Thread	vshost.RunParkingWindow		Normal	0
0	 	14892	Worker Thread	.NET SystemEvents		Normal	0
0	>	10840	Main Thread	Main Thread	Test_Form_Thread_2.Program.Main	Normal	0
0	 	3208	Worker Thread	<No Name>		Normal	0
0	 	12668	Worker Thread	<No Name>		Normal	0


It is interesting to see that the worker thread is gone but threads 3208 and 12668 still exist

Is this normal behaviour ?


Thanks in advance
AnswerRe: Problem with events code and thread (long post) Pin
Nicholas Butler1-May-09 0:35
sitebuilderNicholas Butler1-May-09 0:35 
GeneralRe: Problem with events code and thread (long post) Pin
Otex1-May-09 0:48
Otex1-May-09 0:48 
AnswerRe: Problem with events code and thread (long post) Pin
Nicholas Butler1-May-09 5:22
sitebuilderNicholas Butler1-May-09 5:22 
QuestionRe: Problem with events code and thread (long post) Pin
Otex3-May-09 5:43
Otex3-May-09 5:43 
AnswerRe: Problem with events code and thread (long post) Pin
Nicholas Butler3-May-09 7:43
sitebuilderNicholas Butler3-May-09 7:43 
QuestionHow to Pass TableAdapter data to another form's datagrid for modification then return the data to the original TableAdapter Pin
MarkB12330-Apr-09 22:59
MarkB12330-Apr-09 22:59 
AnswerRe: How to Pass TableAdapter data to another form's datagrid for modification then return the data to the original TableAdapter Pin
Christian Graus30-Apr-09 23:49
protectorChristian Graus30-Apr-09 23:49 
Questionload settings.settings in an array and datagrid Pin
Otex30-Apr-09 22:52
Otex30-Apr-09 22:52 
AnswerRe: load settings.settings in an array and datagrid Pin
DaveyM691-May-09 0:13
professionalDaveyM691-May-09 0:13 
GeneralRe: load settings.settings in an array and datagrid Pin
Otex1-May-09 0:36
Otex1-May-09 0:36 
QuestionHow can I create a DataView based on several DataTables? Pin
maoz30-Apr-09 22:32
maoz30-Apr-09 22:32 
AnswerRe: How can I create a DataView based on several DataTables? Pin
Henry Minute1-May-09 1:03
Henry Minute1-May-09 1:03 
QuestionSql Problem!! Pin
Nafiseh Salmani30-Apr-09 21:56
Nafiseh Salmani30-Apr-09 21:56 
AnswerRe: Sql Problem!! Pin
Abhijit Jana30-Apr-09 22:51
professionalAbhijit Jana30-Apr-09 22:51 
AnswerRe: Sql Problem!! Pin
fly90430-Apr-09 23:21
fly90430-Apr-09 23:21 
QuestionString to Dataset? Pin
nabilg30-Apr-09 21:28
nabilg30-Apr-09 21:28 
AnswerRe: String to Dataset? Pin
HuntingWabbits30-Apr-09 23:28
HuntingWabbits30-Apr-09 23:28 

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.