Solved

c# 2010 exit an application 'pronto'

Posted on 2014-02-24
8
229 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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