Application shuts down and not just the form
-
Hi guys, This might be a broad question for help but I request any suggestions. I have an application in which upon login I hide it with the use of a notify icon. As transactions happen, my hidden app calls another form which works fine for the most part but sometimes the form that I call, upon closing it shuts down my hidden application as well. Is there a way for me to troubleshoot this or avoid this issue. Please advice. Thanks in advance
Sameer
-
the form which you want to close. the deactivated() event of that form include code this.close();
-
it's a huge piece of code so I will try and explain it. The form that I am trying to close that is closing my application is: 1. visible=false 2. the part where I call this.close(), I am sending a print job upon which I call this.close(). So, when I call the close, my form is still invisible. Sameer
Sameer
-
the form which you want to close. the deactivated() event of that form include code this.close();
-
it's a huge piece of code so I will try and explain it. The form that I am trying to close that is closing my application is: 1. visible=false 2. the part where I call this.close(), I am sending a print job upon which I call this.close(). So, when I call the close, my form is still invisible. Sameer
Sameer
-
here is some code here is my hide application code: public void HideApp() { //this.WindowState = FormWindowState.Minimized; Hide(); } This is when I open the new form which upon closing is shutting down the whole application. this.timer1.Enabled=false; try { Payment frm = new Payment(m_pDBCnn, m_pDBUserId, m_pDBPassword, EmpId, TermNum, SiteId, TrnNum, LogId, SystemConfig); if (frm.ShowDialog(this) == DialogResult.Cancel) { Status = frm.Status; if (Status == 1) { this.timer1.Enabled = true; this.PopulateInvoices(); } } } catch (Exception ex) { MessageBox.Show("Error in payment, contact Administrator\n" + ex.Message, "SelecSoftware POS™", MessageBoxButtons.OK, MessageBoxIcon.Error); }
Sameer
-
Hi guys, This might be a broad question for help but I request any suggestions. I have an application in which upon login I hide it with the use of a notify icon. As transactions happen, my hidden app calls another form which works fine for the most part but sometimes the form that I call, upon closing it shuts down my hidden application as well. Is there a way for me to troubleshoot this or avoid this issue. Please advice. Thanks in advance
Sameer
The general pattern for this is to have the main form - the one that is hiding in the system tray - own a reference to the other form. Then when that child form needs to close, either call this.Close() from inside the child form, or have it call an event. The parent form(the one that's hiding) will subscribe to that event, and call Dispose() on it's reference to the child form. That should kill the child form while leaving the parent form running in the system tray. Just don't call Application.Exit() or it will kill all your forms.
-
The general pattern for this is to have the main form - the one that is hiding in the system tray - own a reference to the other form. Then when that child form needs to close, either call this.Close() from inside the child form, or have it call an event. The parent form(the one that's hiding) will subscribe to that event, and call Dispose() on it's reference to the child form. That should kill the child form while leaving the parent form running in the system tray. Just don't call Application.Exit() or it will kill all your forms.
-
If you mean the second method with the events, then yes and no. Yes, events use delegates behind the scenes, but no you don't have to actually declare any delegates yourself. Just declare an event on the child form using the basic EventHandler class, and then inside the parent form you create an instance of the child form, and subscribe to the event with the += syntax. The parent form then provides an event handler(just like button1_Click) and inside that method you can call Dispose() on the child form. Or set it to null, it's basically the same thing. on the child form:
public event EventHandler CloseMe;
on the parent form:ChildForm myChildForm = new ChildForm(); myChildForm.CloseMe += new EventHandler(CloseMethod);
and make sure the child form calls that event when it wants to be shut down. Of course depending on what you're doing, it would probably be easier to just call this.Close() inside the child form, but then the parent form wouldn't know about it. I haven't used these inline code blocks before, so hopefully that formats ok. -
If you mean the second method with the events, then yes and no. Yes, events use delegates behind the scenes, but no you don't have to actually declare any delegates yourself. Just declare an event on the child form using the basic EventHandler class, and then inside the parent form you create an instance of the child form, and subscribe to the event with the += syntax. The parent form then provides an event handler(just like button1_Click) and inside that method you can call Dispose() on the child form. Or set it to null, it's basically the same thing. on the child form:
public event EventHandler CloseMe;
on the parent form:ChildForm myChildForm = new ChildForm(); myChildForm.CloseMe += new EventHandler(CloseMethod);
and make sure the child form calls that event when it wants to be shut down. Of course depending on what you're doing, it would probably be easier to just call this.Close() inside the child form, but then the parent form wouldn't know about it. I haven't used these inline code blocks before, so hopefully that formats ok.I am using this.close() in the child form and I believe that is what is shutting down my application. What I have noticed is it shutsdown around the time I send a print job. I have this.close() right after I send the print job. I am using the same namespace in my child and parent form even though they are a part of a different project, would that cause the issue?
Sameer
-
I am using this.close() in the child form and I believe that is what is shutting down my application. What I have noticed is it shutsdown around the time I send a print job. I have this.close() right after I send the print job. I am using the same namespace in my child and parent form even though they are a part of a different project, would that cause the issue?
Sameer
That i'm not sure about. Maybe try handling the Closing event on the parent form, and then put in a breakpoint and look at the call stack. it'd be interesting to see what methods were called right before the main form closes.