locked
Problem in Threading with windows Form RRS feed

  • Question

  • Hi All,

    I have following problem while doing threading threading with C# Forms.

    1. I have requirement as, I want to process some large Data, at the same time I want to show one diaglog containing message as Process is going on.

    2. The problem is the diaglog which I am showing is windows Form using (Form.ShowDiaglog()).

    3. I can' t do this on main thread, as ShowDiaglog() will block UI thread.  But I want that user will now able to click on Form controls.

    4. So I have done that on another thread. But problem is how this thread will come to know that when to close?

    I mean how I can check if the process is completed and diaglog shoud be close?

    Any code sample will be helpful.

    Thanks,

    Ajit

    Wednesday, January 26, 2011 1:07 PM

Answers

  • Hi Ajit,

    if you are not interested in the progres, then you just do not need to subscribe to the ProgressChanged event. All you need now is the RunWorkerCompleted event.

    So this could be an example (Just the core commands, please look at the class description of the BackgroundWorker class for all required stuff!)

    public partial class MyDialog : Form
    {
     ...
     public void WorkerCompleted(object sender, RunWorkerCompletedEventArs e)
     {
      // Maybe check e.Error first to see if there was an error!
    
      // Close Form
      Close();
     }
     ...
    }
    
    // Somewhere in your code
    
    void DoWork(object sender, DoWorkEventArgs e)
    {
     // Do whatever you want here
    }
    
    // Somewhere else
    ...
    backgroundWorker1 += DoWork; // older versions of C# require a new EventHandler<DoWorkEventArgs>(DoWork);
    backgroundWorker1.RunWorkerAsync();
    MyDialog dialog = new MyDialog();
    dialog.Show();
    ...
    

    I hope this helped a little.

    With kind regards,

    Konrad 

    • Marked as answer by eryang Monday, February 7, 2011 3:06 AM
    Thursday, January 27, 2011 5:43 AM
  • Ajit,

    Why don't you handle your data processing using Asynchronous delegate call?  Just like this

    public delegate void DataProcessingDelegate();
    ...
    
    DataProcessingDelegate dp = new DataProcessingDelegate(BeginProcessData);
    dp.BeginInvoke(new AsyncCallback(EndProcessData), dp);
    
    public void EndProcessData(IAsyncResult result)
    {
      if (result.IsCompleted)
      {
        //Close your dialog here.
       }
    }
     
    public void BeginProcessData()
    {
       //Do your processing here.
    }
    

     

    And if you don't want to use Async delegate, then fire an event when the data processing is finished and inside the event handler, close your dialog.

    Hope this helps.

    ..............................

    Please mark this post as answer if you feel this solves your problem. Happy programming

     

    • Marked as answer by eryang Monday, February 7, 2011 3:06 AM
    Thursday, January 27, 2011 10:30 AM

All replies

  • Hi Ajit,

    I understood that you have a windows forms application. In such gui applications, it is very easy to use a background worker to do the work. The Progress and the End of the operation will both be signaled with an event so you can easily update the progress and you can easily perform some action (e.g. closing the form with the progress) when the operation ended.

    You will find examples in the MSDN Help for BackgroundWorker class: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

    With kind regards,

    Konrad

    Wednesday, January 26, 2011 8:32 PM
  • Hi Konrad,

    Thanks for the reply.

    I don't want to show the Progress Bar, instead that I want to  show doaglog box. After complation of the process, that diaglog shoud close.

    Can you please guide me how I can do it?

    Thanks

    Thursday, January 27, 2011 4:30 AM
  • Hi Ajit,

    if you are not interested in the progres, then you just do not need to subscribe to the ProgressChanged event. All you need now is the RunWorkerCompleted event.

    So this could be an example (Just the core commands, please look at the class description of the BackgroundWorker class for all required stuff!)

    public partial class MyDialog : Form
    {
     ...
     public void WorkerCompleted(object sender, RunWorkerCompletedEventArs e)
     {
      // Maybe check e.Error first to see if there was an error!
    
      // Close Form
      Close();
     }
     ...
    }
    
    // Somewhere in your code
    
    void DoWork(object sender, DoWorkEventArgs e)
    {
     // Do whatever you want here
    }
    
    // Somewhere else
    ...
    backgroundWorker1 += DoWork; // older versions of C# require a new EventHandler<DoWorkEventArgs>(DoWork);
    backgroundWorker1.RunWorkerAsync();
    MyDialog dialog = new MyDialog();
    dialog.Show();
    ...
    

    I hope this helped a little.

    With kind regards,

    Konrad 

    • Marked as answer by eryang Monday, February 7, 2011 3:06 AM
    Thursday, January 27, 2011 5:43 AM
  • Ajit,

    Why don't you handle your data processing using Asynchronous delegate call?  Just like this

    public delegate void DataProcessingDelegate();
    ...
    
    DataProcessingDelegate dp = new DataProcessingDelegate(BeginProcessData);
    dp.BeginInvoke(new AsyncCallback(EndProcessData), dp);
    
    public void EndProcessData(IAsyncResult result)
    {
      if (result.IsCompleted)
      {
        //Close your dialog here.
       }
    }
     
    public void BeginProcessData()
    {
       //Do your processing here.
    }
    

     

    And if you don't want to use Async delegate, then fire an event when the data processing is finished and inside the event handler, close your dialog.

    Hope this helps.

    ..............................

    Please mark this post as answer if you feel this solves your problem. Happy programming

     

    • Marked as answer by eryang Monday, February 7, 2011 3:06 AM
    Thursday, January 27, 2011 10:30 AM
  • Hello Konrad,

    We have used Backgroundworker in WPF application also. Our experience with it was very good.

    Regards.

    Vishal

     


    Vishal
    Saturday, January 29, 2011 7:03 AM
  • Ohh, my answer could be understood like background worker are for windows forms?

    Thank you Vishal for this important point!

    BackgroundWorker are a class that can be used in any application. They are not specific for Windows Forms!  They can be very usefull even in non gui application (e.g. a Windows Service).

    With kind regards,

    Konrad

    Saturday, January 29, 2011 7:25 AM