Solved

Application Exit Checking

Posted on 2006-11-25
4
630 Views
Last Modified: 2010-05-18
HI,
I am having an issue with a couple of methods in my program.

Is there a way I can check and see if any of these threads are running and if not the software goes by it without crashing?
   
EC0-EC270 are programs

EC4-EC7 are forms off of the main

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (EC0 != null)
                EC0.Kill();
            if (EC90 != null)
                EC90.Kill();
            if (EC180 != null)
                EC180.Kill();
            if (EC270 != null)
                EC270.Kill();
            if (ec4 != null)
                ec4.Close();
            if (ec5 != null)
                ec5.Close();
            if (ec6 != null)
                ec6.Close();
            if (ec7 != null)
                ec7.Close();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            if (EC0 != null)
                EC0.Kill();
            if (EC90 != null)
                EC90.Kill();
            if (EC180 != null)
                EC180.Kill();
            if (EC270 != null)
                EC270.Kill();
            if (ec4 != null)
                ec4.Close();
            if (ec5 != null)
                ec5.Close();
            if (ec6 != null)
                ec6.Close();
            if (ec7 != null)
                ec7.Close();

            Application.Exit();
        }

        private void resetApplicationToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            if (EC0 != null)
                EC0.Kill();
            if (EC90 != null)
                EC90.Kill();
            if (EC180 != null)
                EC180.Kill();
            if (EC270 != null)
                EC270.Kill();
            if (ec4 != null)
                ec4.Close();
            if (ec5 != null)
                ec5.Close();
            if (ec6 != null)
                ec6.Close();
            if (ec7 != null)
                ec7.Close();

            Application.Restart();
        }
0
Comment
Question by:schenkp
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 18013080
Hi, not sure on what you exactly mean - can you explain further please? although, one modification that I would make is to encapsulate that common code into a seperate method so that you only need to change the code in one location in the future:

private void KillAndStuff()
{
            if (EC0 != null)
                EC0.Kill();
            if (EC90 != null)
                EC90.Kill();
            if (EC180 != null)
                EC180.Kill();
            if (EC270 != null)
                EC270.Kill();
            if (ec4 != null)
                ec4.Close();
            if (ec5 != null)
                ec5.Close();
            if (ec6 != null)
                ec6.Close();
            if (ec7 != null)
                ec7.Close();
}
0
 

Author Comment

by:schenkp
ID: 18014376
Hi Zepher,
ECO, EC90, EC180, and EC270 are real programs that get launched when the user click on a button on the main program.

There path is C:\program files\mysuff\ECO.exe, etc

EC4, EC5, EC6, and EC7 are all forms that get launched from the main program when a user click on the same button.

What happens is the user makes a choice and EC0, EC4, and EC90 are launched (for example) so now we have the main program, one form( EC4)  and 2 external programs (EC0, EC90) are running.   No i have a timer that executes and usess StuffandKill();  

EC0, EC4, and EC90 all close down they way they should, but after this if the user clicks on any of these methods

Appliation.Ext();
KillAndStuff()


Application.Rest();
KillAndStuff()

Form_closed();
KillAndStuff()


The application throws up and exception and says that EC0;

System.InvalidOperationException was unhandled
  Message="Cannot process request because the process has exited."
  Source="System"
  StackTrace:
       at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
       at System.Diagnostics.Process.Kill()
       


So i was thinking that maybe the KillandStuff() methode needs a little error checking or something?



0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
ID: 18014400
Hi, thanks for clarifying... you can check if a process has exited by using the HasExited property, so your Kill() calls should only execute if the Process object is not null and the process has not already exited:

if (EC0 != null && !EC0.HasExited)
    EC0.Kill();
...

I don't think calling Close() on an already closed or disposed Form object should cause a problem...
0
 

Author Comment

by:schenkp
ID: 18014541
Thanks,

Works great:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XAML: Layout 8 25
Excel file not created as expected 7 51
Not showing JavaScript in the list 5 40
how to read json value 2 31
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now