Sujith Kumar A
Sujith Kumar A

Reputation: 11

how to stop the code from being executed without exiting application in c#

I have a windows forms application, I have implemented a project that should stop the execution without closing the application.I used environment.exit(0) to stop the execution but it closes the windows forms application. for example I have a button in my windows forms application,

    buttnclick(obj sender,eventargs e)
    {
somefun();//calls somefun
otherfun();
    }
private void somefun()
{
if(smthng true)
    {
    //some code goes here
    }
    else
    {
    environment.exit(0);//closing the windows form but I don't want
//again I want to click the button to execute without closing windows forms
    }
}

any suggestions I am greatful.

Upvotes: 0

Views: 3808

Answers (3)

dotNET
dotNET

Reputation: 35470

"Stop executing" is incorrect term here. Technically no process can stop executing. Even when none of your code is running, something somewhere keeps the program running, even if it be the very low-level message loop.

Remember whether you issue a return, or exit, the code will go back to the calling method. If it reaches the main method, a return will do the same thing as exit.

Upvotes: 0

eMi
eMi

Reputation: 5628

Use the return statement.

buttnclick(obj sender,eventargs e)
{
   if(smthng true)
   {
      //some code goes here
   }
   else
   {
      return;
   }
}

Upvotes: 0

marsh
marsh

Reputation: 2740

replace environment.exit(0); with return;

Upvotes: 4

Related Questions