Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GUI
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:
 
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:
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 2-Sep-10 11:17am
Comments
samqty at 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!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Chris Trelawny-Ross at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 2 Sep 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100