Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I need to create an modal dialog box that shows some custom progress information. However, I need to do it in a thread while the caller keeps doing its job.

I created a simple BackgroundWorker based class which owns and creates the dialog. It works fine but I am unable to make the dialog modal to my application. I am guessing this is because it is owned and managed by another thread. I was wondering if there is a way to get around this.

The main bits of code is as follows:

C#
public class ProgressDialogThread : BackgroundWorker
{        
  public ProgressDialogThread()
  {}
  protected override void OnDoWork(DoWorkEventArgs e)
  {
     // myForm is my Winform
     myForm.ShowDialog();         
  }
}


And I can simply call this as:
C#
ProgressDialogThread pdt = new ProgressDialogThread();
pdt.RunWorkerAsync();


As I said, the dialog shows and everything else is fine besides this modality issue.

I would greatly appreciate any help someone can give me.

Many thanks,

Keith
Posted
Comments
Samuel Cherinet 2-Sep-10 16:37pm    
why are you concerned with the modal dialog? Do you want it to block the main application while it is running? am a bit confused there!!

As far as I know, you're right on forms and modality.

You could try switching your approach: show a modal form that starts a BackgroundWorker.

This would also allow you easily to process the progress events and show a progress bar (or an animated 'still working' bar).

If you want to do this in an OO fashion, pass in a delegate to the function that will call the BackgroundWorker and do some extra event wiring so that you can still handle progress. This would allow for re-use of the dialog.

Cheers.
 
Share this answer
 
Yes, that would be the sane way to do this (by using the BackgroundWorker for it's intended purpose!). However, the caller is an unmanaged DLL and it is sort of set in stone with no scope for changes...

I could have become a pilot...why did I ever decide to do software!

Thanks for your reply though.

Keith
 
Share this answer
 
Comments
Chris Trelawny-Ross 2-Sep-10 17:40pm    
"why did I ever decide to do software!" ... because in software accidents are generally not fatal? :)

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