Solved

c# 2010 exit an application 'pronto'

Posted on 2014-02-24
8
231 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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