CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009

    Angry 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

  2. #2
    Join Date
    Jun 2008

    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.

  3. #3
    Join Date
    Feb 2009

    Re: C# Form Updating Using Delegate or Callback?

    Thanks much BigEd!

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)