I develop a C# programme to get real time data From sensor ... its working .. But i cant close it or click another button in form after i start reading ports... so can any one tell me why is that ??? any ideas to correct the code ..
There are stickies at the top of the forum, describing how to post code. Nowhere in them does it suggest that green is a suitable color. Go read those posts, and try again.
This code in your timer handler will prevent the handler from returning, so the calling thread will be blocked and not available to handle any other events. It would work better if you only stayed in the loop while there was input available on the port, rather than as long as the port remained open.
damith14:
... i cant close it or click another button in form after i start reading ports...
so can any one tell me why is that ???
This is really a c# problem: It's because you are running these three lines of code inside the while(port.IsOpen) loop and never leaving the loop to run anything else.
c# provides Application.DoEvents() to allow other portions of your program to have some processing time when you are running a tight loop as above.
while (port.IsOpen)
{
data = port.ReadLine().Split(',');
textBox1.Text = data[0];
textBox2.Text = data[1];
Application.DoEvents(); ///Allow other parts of your program to receive messages/events.
}
c# provides Application.DoEvents() to allow other portions of your program to have some processing time when you are running a tight loop as above.
You don't need a tight loop like that, and you don't need a timer to trigger the function. The SerialPort class has events. Subscribe to the right ones, and the callback will happen only when there is serial data to read.
The thing you need to remember with the windows program is that the windows environment is a very complex multi threaded place. The key is to make your serial comms event driven. I have posted on your other thread with a breif example. If it would help people I will write a guide on serial programming in c#.
If it would help people I will write a guide on serial programming in c#.
It's been done before, but there is no harm in repeating it. I, for one, would like to see how you tackled the problem. I can then compare that to my method.