Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys!
So I can't find solution for my issue!
I'm using Windows forms.
I have 1 button and 1 progressBar, 2 classes(1 main class, custom class)
So I pass some data from main class to custom class (for example number 10)
custom class count from 1 to 10(custom class using delegate, and beginInvoke Method)

how I can pass value to progressBar in winForm? (for example custom class variable increments, and progressBar value increments parallel).

Thanks for answers! :)
Posted
Comments
valza 23-May-11 9:45am    
Can someone write some code snipet?! :) Thanks

First, you're using the wrong terminology - you're not "invoking" the main thread because it's already running.

Second, you should learn how to use google:

Google results for "C# update ui from thread"[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-May-11 10:58am    
Absolutely wrong. Good note, a 5.
--SA
Sergey Alexandrovich Kryukov 23-May-11 11:02am    
Please see my solution.
--SA
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.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
 
Comments
Kim Togo 23-May-11 12:40pm    
Good links, my 5.
Sergey Alexandrovich Kryukov 23-May-11 13:01pm    
Thank you, Kim.
--SA
Check out CP article C# Event Implementation Fundamentals, Best Practices and Conventions[^]. A good article about your question.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-May-11 11:03am    
This is not the reference exactly to the point. The topic is more about threading. (I did not vote this time.)

Please see my solution.
--SA
valza 23-May-11 11:04am    
OK. Thanks a lot t reading... Hope this will help me!
A understand how use on Form.invoke(new Action ()... and so on! but how invoke from another class with running thread - Not! :)
thanks...
Kim Togo 23-May-11 12:42pm    
See links SAKryukov. Just remember that a non-UI thread has to use Invoke or BeginInvoke to change the UI controls. Invoke if you want execute it sync and BeginInvoke for async.
Sergey Alexandrovich Kryukov 23-May-11 13:05pm    
Well, another problem is that Dispatcher is not mentioned. It makes it not applicable to WPF and incomplete for Forms.
Maybe it is just a bit obsolete. I think I have to vote 4 for your answer.
--SA

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