How to wait until the dialog is really closed - c#

The software starts thread with some calculations and then shows another Form as a waiting dialog with ShowDialog. When the BackgroundWorker thread finishes its work, the Form is closed in the RunWorkerCompleted event and another calculation is start with another Form as a waiting dialog (with ShowDialog again).
The problem is that the first waiting dialog is still visible until the second waiting dialog is closed. How to wait with showing the second dialog after the first dialog is really closed?
Simple code to reproduce:
private BackgroundWorker _bgw = new BackgroundWorker();
private Form2 _msg = new Form2();
private Form3 _msg2 = new Form3();
public Form1()
{
_bgw.DoWork += BgwDoWork;
_bgw.RunWorkerCompleted += BgwRunWorkerCompleted;
_bgw.RunWorkerAsync();
_msg.ShowDialog();
}
private void BgwDoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(5000);
}
private void BgwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_msg.Close();
_msg2.ShowDialog();
}

Note that ShowDialog() is a blocking call. You still have not returned from the constructor when you show _msg2.
This is a quick fix:
public Form1()
{
_bgw.DoWork += BgwDoWork;
_bgw.RunWorkerCompleted += BgwRunWorkerCompleted;
_bgw.RunWorkerAsync();
_msg.ShowDialog();
_msg2.ShowDialog(); // here
}
private void BgwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_msg.Close();
//_msg2.ShowDialog(); // not here
}

Related

ShowDialog() is not preventing the originating form from being clickable

I have a winform with the following code that opens an external program when the form is opened. If the program closes, a dialog box is suppose to pop up with an option to return that a button was clicked. This will close the dialog box and return to the initial form and then run the function to open the external program again.
Here is the problem, the original form is STILL clickable AND the function is not running. Any ideas?
public Manager()
{
InitializeComponent();
ExternalProgramOpen();
}
private void ExternalProgramOpen()
{
Process startProgram = Process.Start("program.exe", Program.ConnectionArg);
startProgram.EnableRaisingEvents = true;
startProgram.Exited += this.PrematureClose;
}
private void PrematureClose(object sender, EventArgs e)
{
ManagerWarning messagepopup = new ManagerWarning();
messagepopup.ShowDialog();
using (var ManagerWarning = new ManagerWarning())
{
if (ManagerWarning.ShowDialog() == DialogResult.Retry)
{
ExternalProgramOpen();
}
}
}
The reason for this effect is probably that the Exited event is not raised in the same UI thread that started the process.
When you call ShowDialog() from another thread, the new window will not use and block the original UI thread.
To solve this, check if InvokeRequired is true and use Invoke:
private void PrematureClose(object sender, EventArgs e)
{
if (InvokeRequired)
{
Invoke(new Action(() => PrematureClose(sender, e)));
return;
}
// your code here
// ...
}

System.Windows.Forms.Timer And MessageBox

can someone please let me know why the System.Windows.Forms.Timer continues to show multiple message boxes? I thought that it is on GUI thread ... and therefore after the first messagebox the GUI thread should block. But this is not the case
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int nValue = 0;
void tmr_Tick(object sender, EventArgs e)
{
nValue++;
MessageBox.Show(nValue.ToString());
}
System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
private void btnStartTimer_Click(object sender, EventArgs e)
{
tmr.Interval = 500;
tmr.Enabled = true;
tmr.Tick += new EventHandler(tmr_Tick);
}
}
The MessageBox.Show() method includes (as all modal dialogs do) a message loop that continues to pump window messages.
Window messages are what allow a window to interact with the user (update itself, accept input, etc.), as well as what allows the Forms.Timer class to work.
If you want your Forms.Timer to stop ticking when the dialog is shown, you need to set the timer's Enabled property to false before you show the dialog.
In your Tick event stop the timer and then start again after MessageBox.Show like:
void tmr_Tick(object sender, EventArgs e)
{
tmr.Enabled = false;
nValue++;
MessageBox.Show(nValue.ToString());
tmr.Enabled = true;
}
The reason you are getting repeated MessgeBoxes is because your timer is continuing after showing the first MessageBox.
A message box does not block the GUI-Thread. It's as simple as that. You can interact with the message box, after all :)
Also: The internal workings of the timer are not clear, but I would guess that it runs on another thread and just returns on the GUI-Thread.

GUI busy message [duplicate]

This question already has answers here:
Gui busy pop up
(3 answers)
Closed 10 years ago.
I asked this q before but may be I was not clear so I am posting again. I have a gui. When I press "button1" my gui starts serial comm. I send some commands and receive some data. Once I press "button1" I am in the other thread (other than GUI thread) for 3,4 sec untill data is fully transferred. What I want is a dialog box, pop up or some thing like that (showing a busy message) to appear infront of my gui when I press the button1 and this pop closes automatically in the other thread in which serial communication is taking place when the whole data is transferred.
Some people suggested back ground worker but I was not clear.
An good explanation or a way to do it considering my scenario ?
Thanks
I would do it with the following code:
public partial class frmWait : Form
{
private BackgroundWorker worker;
public frmWait()
{
InitializeComponent();
this.worker = new BackgroundWorker
{
WorkerReportsProgress = true
};
this.worker.RunWorkerCompleted += WorkerOnRunWorkerCompleted;
this.worker.DoWork += WorkerOnDoWork;
this.worker.ProgressChanged += WorkerOnProgressChanged;
this.worker.RunWorkerAsync();
}
private void WorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Update UI or something else
}
private void WorkerOnDoWork(object sender, DoWorkEventArgs e)
{
// Do background-stuff here
}
private void WorkerOnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Actions after BackgroundWorker is done
Close();
}
}
in the form with button1, in the click-event of the button you just have to:
frmWait wait = new frmWait();
wait.ShowDialog();

Windows.Form topmost called from a workerthread doesnt work when ClickOnce is enabled and published

I have a main thread that is a form, that starts another application, in this case Notepad, then I spawn off a BackgroundWorker that waits for Notepad to be closed. When its closed, the BackgroundWorker shows another Form to display, topmost, to the user. This Form needs to be non-modal, so that the user can click on some buttons on the main thread dialog. The problem is this form (Form2, from the BackgroundWorker) is NOT TopMost, even though I set it to true. It works when I hit F5, but when I publish, as a ClickOnce application, to my server, form2 is no longer TopMost. I have tired Form2.Topmost = true, BringToFront, Activate, "MakeTopMost" from What is powerful way to force a form to bring front? .... nothing seems to work.
I even tried to get the handle of the main form, and use that as the parent of form2, but I'm getting "InvalidOperationException: Cross-thread operation not valid: Control 'Form2' accessed from a thread other than the thread it was created on."
Here is a code snippet:
public partial class Form1 : Form
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
private BackgroundWorker endApplicationBackgroundWorker= new BackgroundWorker();
public Form1(string[] args)
{
endApplicationBackgroundWorker.DoWork += new DoWorkEventHandler(endApplicationBackgroundWorker_DoWork);
p.StartInfo.FileName = "notepad";
p.Start();
endApplicationBackgroundWorker.RunWorkerAsync();
//Quit here so we can accept user inputs (button pushes ..)
}
private void endApplicationBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
p.WaitForExit();
Form2 form2 = new Form2();
form2.TopMost = true;
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(form1ProcessName);
if (procs.Length != 0)
{
IntPtr hwnd = procs[0].MainWindowHandle;
if (form2.ShowDialog(new WindowWrapper(hwnd)) == DialogResult.OK)
{
// process stuff
}
}
this.Close();
}
}
Any other ideas? Or can someone fix my code above? I have been dealing with this issue for weeks now and getting flustered.
Thanks!
Any work you do in a BackgroundWorker's DoWork method after calling the RunWorkerAsync procedure is NOT running on the UI thread, but your code is creating a form in the background.
Forms are UI elements, so this won't work:
private void endApplicationBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Form2 form2 = new Form2();
form2.TopMost = true;
// etc..
if (form2.ShowDialog(new WindowWrapper(hwnd)) == DialogResult.OK)
{
// process stuff
}
}
From the comments, you should subscribe to the RunWorkerCompleted event to show your second form. Also, you can't call the Close method either since you are trying to keep Form2 alive without a ShowDialog call, so try subscribing to the Form_Closing() event of the second form to notify when the main form should be closed, too:
public Form1(string[] args)
{
endApplicationBackgroundWorker.DoWork +=
new DoWorkEventHandler(endApplicationBackgroundWorker_DoWork);
endApplicationBackgroundWorker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(endApplicationBackgroundWorker_RunWorkerCompleted);
p.StartInfo.FileName = "notepad";
endApplicationBackgroundWorker.RunWorkerAsync();
}
private void endApplicationBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
p.Start();
p.WaitForExit();
}
private void endApplicationBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Form2 form2 = new Form2();
form2.TopMost = true;
form2.FormClosing += new FormClosingEventHandler(form2_FormClosing);
form2.Show(this);
}
private void form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.BeginInvoke(new MethodInvoker(delegate { this.Close(); }));
}
Another workaround can be, if you set the topmost to false and then set it back to true. It is strange, but it works. So the code could be the following for a form to be show with descending oppacity in a simple background thread:
for (int i = 0; i < 50; i++)
{
ppaForm.SetBitmap(bitmap, (byte)(255 - i * 5));
ppaForm.Show();
ppaForm.TopMost = false;
ppaForm.TopMost = true;
Thread.Sleep(6);
}
In this case ppaForm is a PerPixelAlphaForm, for which you can find a description here.

Opening A WinForm Programmatically In Another WinForm's Code

Happy Friday SO!
I'm building a multi-WinForm application and am having some troubles.
I have a main WinForm that stays open at all times. It hits a database every minute looking for changes, and if there is a change, it will open the second WinForm (this may seem like a stupid way to do things, but for my purpose, this is the best method).
Just sticking the following code into my Form1.cs doesn't do the trick:
Application.Run(new Form2());
Can you guys point me in the right direction? I have no idea where to turn.
Form2 form2 = new Form2();
form2.Show();
and to prevent a ton of forms being opened, maybe:
Form2 form2 = new Form2();
form2.ShowDialog();
#Comment:
A BackgroundWorker is used to keep your current UI Thread responsive. It was not designed to keep multiple forms pumping happily along. Look into running your intensive code as a Background thread within a ThreadPool.
If what you wish is to launch a long process and to show the progress to the user, for example just like when you have a progress bar or something alike, you should use a BackgroundWorker to do the job. Here's a simple example:
public partial class ProgressForm : Form {
// Assuming you have put all required controls on design...
// Allowing some properties to be exposed for progress update...
public properties MaximumProgress {
set {
progressBar1.Maximum = value;
}
public properties OverallProgress {
set {
progressBar1.Value = value;
}
}
public partial class MainForm : Form {
private BackgroundWorker backgroundWorker1;
private ProgressForm _pf;
public MainForm() {
InitializeComponent();
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
}
// Assuming process starts on Button click.
private void button1_Click(object sender, EventArgs e) {
_pf = new ProgressForm();
_pf.MaximumProgress = number-of-elements-to-treat-returned-by-prevision-or-whatever-else;
// Launching the background workder thread.
backgroundWorker1.RunWorkerAsync(); // Triggering the DoWork event.
// Then showing the progress form.
_pf.ShowDialog();
}
private void backgroundWorker1_DoWork(object sender, EventArgs e) {
LaunchProcess();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
_pf.OverallProgress = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, EventArgs e) {
_pf.Close();
_pf.Dispose();
}
private void LaunchProcess() {
// Do some work here...
// Reporting progress somewhere within the processed task
backgroundWorker1.ReportProgress();
}
}
This is not a compileable code as its purpose is to illustrate the main idea.
Now, is this something alike you want to do?

Categories