Solved

c# 2010 exit an application 'pronto'

Posted on 2014-02-24
8
228 Views
Last Modified: 2014-03-03
Hi

I have some code in the constructor of a class, which is checking that a file exists on the network.

If the file exists, fine.. if not, it enters a loop and retries until the user clicks cancel.  At which point I want the application to exit.

So in  my code I have

application.Exit();

But this is allowing the next line of code from the form to execute...

Is there any way to stop this happening?
0
Comment
Question by:cycledude
  • 4
  • 3
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39882411
if(your test goes here)
{
  application.Exit();
  return;
}
0
 

Author Comment

by:cycledude
ID: 39882433
Hi

Thanks, I think it will help with a snippet of code, I forgot to take out the text int my question about the constructor, the code is not there any more.... so from my form_load() I am doing



            bool stop = false; 
            stop = si.check_network(@"W:\path_to_file\file.name");

            if (stop)
                Application.Exit();
                        

Open in new window


and the check_network is simple enough

 public bool check_network(string Path)
        {
            bool value = false;

            while (!File.Exists(Path) && value == false)
            {

                DialogResult res = MessageBox.Show("There is a problem connecting to the network, click 'Retry'.\n\nTo close the application click 'Cancel'.", "Network problem", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);

                if (res == DialogResult.Cancel)
                {
                    value = true;
                }

            }

            return value;

        }

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39882439
bool stop = false;
            stop = si.check_network(@"W:\path_to_file\file.name");

            if (stop)
{
                Application.Exit();
return;
}
0
 

Author Comment

by:cycledude
ID: 39882440
there is code after the

if (stop)
                Application.Exit();

Open in new window


which executes, even if the application.exit() is envoked...

I can put a condition to say

if (!stop)

{

// run the other code


}

but it would be good if 'application.stop()' actually did what it was meant to
0
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.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39882448
Your current code runs the line Application.Exit() and then continues running the following lines of code.  The Application.Exit will cause the application to stop - but windows is a message handling system so you can not guarantee it will stop immediately.
So to stop the lines of code after the Application.Exit being executed you need to prevent them being reached, eg. by returning from the function.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39882462
ps.  This is a 'problem' with .net, in that it is hiding rather more of how windows functions in the background.  One gets the impression it is an event driven operating system, unfortunately it is not, it is a message driven operating system.  Messages are placed into a queue and then acted upon at some time in the future.  Some messages can jump to the head of the queue.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39882731
You might try Environment.Exit instead.
0
 

Author Closing Comment

by:cycledude
ID: 39902588
this seems to work just as I wanted, thanks!
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
Achieve json result 2 65
Excel file not created as expected 7 51
Syntax error 9 45
Importing CSV file encoded in UTF-8 in SQL SERVER 2008 using BULK INSERT 5 31
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

27 Experts available now in Live!

Get 1:1 Help Now