Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c# multitasking problem

Status
Not open for further replies.

RanchuPanchu

Programmer
Jun 2, 2007
5
IL
Hello,

I have made a simple program in c# which consists of a serialPort and a GUI. the serialPort interface with the GUI is only through BeginInvoke method.
The problem: I have noticed that the applications works very well as long as I do not move the main window too fast , which means that somehow the main window task is handled before the serial port iin such cases. I have changed the main window task priority to be lowest ( in the Initializer of the main window:

Thread.CurrentThread.Priority = Thread.Priority.Lowest;

But the problem persist.
I assume That maybe if I change the priority of the serialPort IO component thread to be highest, it might solve the problem, but I do not know how to do it.

I would thank you for your help,

Regards,

David

 
Setting your thread to be lowest priority may not be the best thing to do here...

How are you using the SerialPort? is it a 3rd party pre-built dll that you are communicating with or did you create your own instance of the SerialPort class in .Net 2.0?

my guess is that you will want to have a multi-threaded application so that the serial IO is separate from the GUI thread.

Google Multi-threading C# tutorial and see waht you find...
 
Hello,

I am using an instance of the SerialPort class in .Net 2.0.
I have noticed that on opening the SerialPort, a new Thread is created in priority Normal. I thought that maybe if I change its priority to highest, it might solve my problem, but I do not know how to do it.

Regards,

David
 
There is no need to set the thread priority unless you are writing to a DVD Driver or something.

Otherwise your application can have VERY NEGATIVE effects on the OS.

Don't worry about the SerialPort class creating its own thread in this case. Change your main thread back to Priority normal too.

Once you have done this, please further describe the issue you are having. What do you mean when you say "the applications works very well as long as I do not move the main window too fast"?

Does communication to the port stop or slow down? does the form just not redraw quickly? what is the real issue here?
 
Hi again,

Thanks for the reply.
The problem is that I miss some of the bytes I should read, when I do "heavy things" in the main window.
"heavy things" could be just moving the main window very rapidly.
Therefore, my guess is that somehow the thread which is responsible for the GUI, is getting the CPU, instead of the SerialPort's thread, which obviously, is the more important thread in this case.


David
 
Create a separate thread for your Serial communication. This should prevent data from getting lost.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top