Click here to Skip to main content
16,022,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
My application plot values from an electronic device. It plots 10 values in one second. It waits for 100ms before fetching the next value. The waiting is given by
C#
Thread.Sleep(100);
This much part is working fine.

I am showing the mouse position in MouseMove event. But to show the position it takes a delay which i have given before.

So i want to avoid that delay. I tried to run the MouseMove event in thread like
C#
new Thread(chartControl1_MouseMove).Start();
. But it gives the following errors:

1. The best overloaded method match for 'System.Threading.Thread.Thread(System.Threading.ThreadStart)' has some invalid arguments.
2. Argument '1': cannot convert from 'method group' to 'System.Threading.ThreadStart'


Any suggestions...???
Posted
Comments
Sergey Alexandrovich Kryukov 10-Aug-11 22:06pm    
This is simply awful. Why would you ever run this method as thread, even if you could?
--SA

I assume that you are not using a separate thread for your plotting code?
Do not use Thread.Sleep on the main thread - all it will do is block everything else from working, as you have seen.
Instead, either use a background thread to do your data plots (but be careful, because you will need to pinvoke any UI elements as you can only access those from the thread that created them: the UI thread), or use a Timer instead on your main UI thread, and do your plot in the timer Tick event.

The latter is likely to best best solution: Sleep is not something you should rely on to give you regular updates as the interval between successive Sleeps ending is dependent on the processor load, rather than a regular interval.
 
Share this answer
 
Comments
good.shankar 10-Aug-11 3:57am    
i am not using separate thread to plot values. I use timertick event to plot the values. The timer has an interval of 1 second. The delay is given in between the timer tick. the delay is 100ms.

so how will i put the plot in seperate thread..???
OriginalGriff 10-Aug-11 4:22am    
Don't. The chances are you don't need to, in this case.
Change your timer interval to 100ms.
Every tick, fetch your next value.
Set up a counter, which re-plots your data every ten Tick events. (This is effectively the same as you have now)
You no longer need to Sleep any threads, and it all works, including the mouse! :laugh:
good.shankar 10-Aug-11 5:33am    
oh... It works great....
thank you very much.....
OriginalGriff 10-Aug-11 5:36am    
You're welcome
Sorry, this is not a solution, because you did not explain anything.

I just want to explain you the basic things. The title of the question itself already shows your misunderstanding: what is "run event". You should understand that chartControl1_MouseMove is not event or event instance. This is just the method. Now, it can be anything at all, but the name of the method suggests that this is an auto-generated method created by the Designer to handle the event MouseMove of the object chartControl1. (By the way, you should never use such names; everything auto-generated should be renamed somehow semantically; in this is a good tool to tell unattended symbol from the ones properly used.)

This method does not run as a thread method due to its signature. A thread needs a parameter-less method, static or instance one; another signature is the one accepting one object. I recommend never pass a parameter to the thread in this way, because of required type casting; I proposed much better method based on passing "this"; which is practically means using a non-static (i.e. instance) method and a thread wrapper. See my past solutions How to pass ref parameter to the thread[^] and change parameters of thread (producer) after it started[^].

Now, I have no idea why would you even think about doing something with the mouse event handler in a separate thread, but cannot make any sense. For just one thing, mouse event processing is an integral part of UI and could never be processed in a non-UI thread.

As to the communication with your electronic device, I would strongly recommend to get back to a separate thread. Only you should work with the IU using invocation mechanism, see below. As to the regular intervals: I don't think you need strictly regular time intervals. In case you really need good accuracy in timing, timer won't help you anyway, but working with the timer accurately is much more difficult. Threads are at least reliable. One thing about thread: for better (much better accuracy) don't use System.Windows.Forms.Timer — it's very bad. Use System.Threading.Timer or System.Timers.Timer.

For more about threading with UI, please see my past solutions:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^],
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900