-
February 17th, 2009, 12:04 AM
#1
C# Form Updating Using Delegate or Callback?
I'm new to C# so please bear with me. I have a form with a text box and a couple of buttons.
The buttons are used to call some long running methods in a class. I would like to update the text box with some status information regarding the progress or failure of the method. What are some of the preferred best practices to accomplish this and keeping the code for the form isolated from the class code? I've looked at callbacks/delegates, etc, but I need a real simple example to get the gist of how this works.
Thanks In Advance
-
February 17th, 2009, 01:17 AM
#2
Re: C# Form Updating Using Delegate or Callback?
The BackgroundWorker class is usually what most people will suggest for dealing with tasks such as this. create one and handle three important events:
(From MSDN)
DoWork - Occurs when RunWorkerAsync is called.
ProgressChanged - Occurs when ReportProgress is called.
RunWorkerCompleted - Occurs when the background operation has completed, has been canceled, or has raised an exception.
So, you can do your work in the handler for the DoWork event. When you want to send a message to the UI, call the ReportProgress() method. This will raise the ProgressChanged event, and in the handler for that event you may update the UI without worry of a corss-thread exception. Handle the RunWorkerCompleted event if you need to do anything after the method completes.
-
February 17th, 2009, 09:14 AM
#3
Re: C# Form Updating Using Delegate or Callback?
Tags for this Thread
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
|