Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Dear all,

i'm pretty new in the .net programming and i would like to get some suggestions.

I'm trying to create an easy client GUI application using the VS2010 Designer to create a single form in which i have:
1 comboBox, (containing the list of possible commands);
1 button, (used to execute the command selected in the combobox);
1 picturebox (in which i display images received from my server application);

I was able to create my client application and display a different image in the picturebox received from the server everytime i press the button.

What i would like to do is a not blocking loop in the event button click so that as long as the the client combobox command is set to start imaging, the images sent by the server are displayed in the picturebox and it stops when the client combobox command is set to stop imaging.

I'm not sure about how to do that because if i try and loop in the event button click, the GUI becomes unresponsive and i don't have a chance to change the command in the combobox.

Any help would be much appreciated.
Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 21-Mar-12 12:32pm    
What is that? Please, tag tag of application or UI library you use. WPF? Forms? Anything else?
--SA
lukin4that 21-Mar-12 12:47pm    
It is a Form in a Windows Form application using C++ in .Net4 framework
Sergey Alexandrovich Kryukov 21-Mar-12 20:46pm    
Tag it! Anyway, you got the answer, but you are the one who is interested in proper tagging the most, to be heard by a proper expert.
--SA

1 solution

Don't even play with the idea of intervention on the UI cycle (it is possible, dirty and ineffective, just a bad idea).

In all cases, use a separate thread with delay (System.Threading.Thread.Sleep). In this thread, change the picture periodically. Use one of these three method of obtaining a thread:
http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[^],
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^].

If you want to create a thread each time you want to start this "animation", you should use BackgroundWorker (referenced above). You can also create a thread from the very beginning and throttle it with a event wait handle, set it when you want to start animation, reset to pause/stop it. Please see System.Threading.EventWaitHandle or System.Threading.ManualResetEvent:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^].

For the thread created by a thread constructor (see the very first reference above), it's the best to use a thread wrapper I offered in my past answers:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

Now, the problem is that you need to show the change in image in UI. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 

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