Solved

c# 2010 exit an application 'pronto'

Posted on 2014-02-24
8
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

695 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