Solved

c# 2010 exit an application 'pronto'

Posted on 2014-02-24
8
227 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 74

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

21 Experts available now in Live!

Get 1:1 Help Now