Stopping further program execution.

This is a discussion on Stopping further program execution. within the C# Programming forums, part of the General Programming Boards category; Hi, I have a windows form application that when I click on the run button, it passes to another class ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Stopping further program execution.

    Hi,

    I have a windows form application that when I click on the run button, it passes to another class to perform some actions.

    Within that class there is something that throws an exception caught within a try/catch block. If an exception is thrown and caught, then a messagebox shows up with two option : retry and cancel.
    If the user clicks on retry, it simply retries the operation using DialogResult. However, when the user clicks cancel, I simply want the Messagebox to vanish and the program execution to stop.

    That is to say, I don't want the application to close down, just that I don't want it to progress any further and the actual form is still visible to the user to allow them to do further operations.

    Is there a simple method to do this?

    Thanks,

    Darren.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Really, all you need is an event.

    Code:
    public class tester
    {
    	public delegate void EndProgramEvent(object sender, EventArgs e);
    	public event EndProgramEvent EndNow;
    
    	public Tester()
    	{
    		
    	}
    
    	public void DoCalculations()
    	{
    		try
    		{
    			internalWork();
    		}
    		catch (Exception ex)
    		{
    			if(MessageBox.Show() == DialogResult.Retry)
    			{
    			}
    			else
    			{
    				if(EndNow!=null)
    				{
    					EndNow(null,EventArgs.Empty);
    				}
    			}
    		}
    	}
    
    	private void internalWork()
    	{
    		//  whatever work needs to be done is here
    	}
    }
    
    // Form1 here
    
    public partial class Form1
    {
    	tester testerclass = new tester();
    
    	public Form1(){}
    
    	// call this from your button event or add the lines in here to your button event
    	public void StartCalculations()
    	{
    		testerclass.EndNow+=new EndProgramEvent(EndProgramNow);
    		testerclass.DoCalculations();
    	}
    
    	public void EndProgramNow(object sender, EventArgs e)
    	{
    		// you could do anything here really, but this is something you could do
    		Application.Exit();
    	}
    }
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks.

    I'll have a try on that later, when I get back to that work.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Just noticed an error, the delegate goes in the namespace area, not a class.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free program I'm sharing: ConvertEnumToStrings
    By Programmer_P in forum Projects and Job Recruitment
    Replies: 101
    Last Post: 07-18-2010, 01:55 AM
  2. execution of a C program
    By kris.c in forum C Programming
    Replies: 7
    Last Post: 07-27-2006, 12:14 PM
  3. invoke the execution of one c program from another
    By kris.c in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-09-2006, 12:10 AM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21