|
-
September 13th, 2006, 07:42 PM
#1
BeginInvoke / Updating Controls
Hi,
I think lots of them might have answered this topic. I am still struggling to understand this. I tried but somewhere i am sure i am doing mistake. I am kind of able to know the concept but want to implement that. This is basically updating UI controls of main thread when some other task is occuring in worker thread.
I want to update the progress bar control in the main form. From the form i am calling a method of different class in a different file. From there i would like to update the progress control in the main form.
Code:
//Assuming
AA.cs // File Name
public partial class MainForm: Form
{
Thread m_Thread = null; // Thread
public MainForm()
{
//Constructor
//Do something...
COtherclass other = new COtherclass(); //Class in Different File
}
private void OnButtonClick()
{
m_Thread = new Thread(new ThreadStart(ThreadFunction));
m_Thread.Start();
}
private void ThreadFunction()
{
other.LongFunction(); // Function which takes long time
}
private void UpdateProgressBar(int iCount)
{
progressBar.Value = iCount; // Update the progress Bar
}
}
//Assuming
BB.cs // Second File Name
public class COtherclass
{
public COtherclass()
{
//Constructor
//Do something...
}
public void LongFunction()
{
// Do some time consuming task
// Update the progress bar in the Mainform
// by calling UpdateProgressBar(...) method
}
}
Having this situation ,
How can i declare the delegate ?
How to implement BeginInvoke?
do i need the method Invokerequired()?
I saw in some forums implementing Sleep(). Do we need that?
Please help me to understand this and also help me to insert the relevant pseudo code in this sample. Once again i thank in advance and your help is greatly appreciated.
"Dont Forget to rate if it helped"
-
September 13th, 2006, 10:32 PM
#2
Re: BeginInvoke / Updating Controls
Read this blog:
http://blogs.msdn.com/csharpfaq/arch.../17/91685.aspx
Basically you need to modify this function:
Code:
private void ThreadFunction()
{
other.LongFunction(); // Function which takes long time
}
Here you have to use : Invoke(aDelegate)
where a delegate is a delegate with a signature matching your other.LongFunction(); .
You can make LongFunction in your MainForm class .
So :
Code:
public delegate void UpdateTextCallback(void);
UpdateTextCallback updateDelegate = new UpdateTextCallback(other.LongFunction);
Then your thread function becomes:
Code:
private void ThreadFunction()
{
Invoke(updateDelegate);
}
You don't need Thread.Sleep() . That static function is holding your thread for a period of time ( that you send it by parameter e.g Sleep(1000) sleeps for 1 second ).
Bogdan
If someone helped you then please Rate his post and mark the thread as Resolved
Please improve your messages appearance by using tags [ code] Place your code here [ /code]
-
October 2nd, 2006, 05:08 PM
#3
Re: BeginInvoke / Updating Controls
I am very sorry and am not able to follow. will you please insert the code of what you think in the sample provided by me?
I have already got existing classes designed in the same way. So i am trying to fit the UI threading in the existing code.
Please insert/ correct the code given by me. This is the first UI delegating sample i am trying to execute, so help me with code.
"Dont Forget to rate if it helped"
-
October 3rd, 2006, 01:57 AM
#4
Re: BeginInvoke / Updating Controls
Code:
private void UpdateProgressBar(int iCount)
{
BeginInvoke((MethodInvoker)delegate()
{
// This runs on the UI thread!
progressBar.Value = iCount;
});
}
This usually does the trick for me.
-
October 3rd, 2006, 11:03 AM
#5
Re: BeginInvoke / Updating Controls
hmmm....
The UpdateProgressBar() function is private. Do we make this public so that we can call this method from COtherClass ?
How to call the UpdateProgressBar() function, coz this function is in the MainForm.
"Dont Forget to rate if it helped"
-
October 4th, 2006, 04:37 AM
#6
Re: BeginInvoke / Updating Controls
Another choice is the BackGroundWorker, it is quite easy to use...
wxuf
Waiting OFFERS from CMU, UMass, UC Davis, OSU
Mail to me
-
October 4th, 2006, 10:58 AM
#7
Re: BeginInvoke / Updating Controls
Gurus,
Can anyone of you pls help me out in this?? is going on and on and am still struggling to understand the solution.
Please see my first post and let me know how i can implement the mechanism.
"Dont Forget to rate if it helped"
-
October 4th, 2006, 07:57 PM
#8
Re: BeginInvoke / Updating Controls
You can find an example in MSDN by searching BackGroundWorker.
THis example in my MSDN reside in :
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclictl/html/138f38b6-1099-4fd5-910c-390b41cbad35.htm
wxuf
Waiting OFFERS from CMU, UMass, UC Davis, OSU
Mail to me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|