2

I'm trying to write a program that can be used to launch another program. I have a button, and when it's clicked, I'd like to start a program, and also record that the program has been launched. When I go to start a new program, I'd like to first check that I haven't already started the program—and if I have, close the existing instance first (so at most one instance of the program will exist at a time).

I've already written the following code:

private void button1_Click(object sender, EventArgs e)
{
    bool status = false;
    if (status != true)
    {
        status = true;
        System.Diagnostics.Process.Start("C:\\Users\\David\\Desktop\\Test\\Test.exe");
    }
}

Now my problem is, if I click on the button, the variable is set to false as you can see on the first line. How I can do it correctly? Also, how I do return 0 if status is set on true?

icktoofay
  • 126,289
  • 21
  • 250
  • 231
JustMe.77
  • 19
  • 4
  • 3
    Please read up about scope. PS. What do you think the value of status should be after the line `bool status = false;` – Ed Heal Oct 22 '16 at 23:42

2 Answers2

1

Move the declaration of the bool variable outside the method if you would like to preserve its value across invocations:

bool status = false;
Process myProcess;
private void button1_Click(object sender, EventArgs e) {
    if (status != true) {
        myProcess = new Process()
        myProcess.EnableRaisingEvents = true;
        status = true;
        // Start a process to print a file and raise an event when done.
        myProcess.StartInfo.FileName = "C:\\Users\\David\\Desktop\\Test\\Test.exe";
        myProcess.Exited += new EventHandler(Process_Exited);
        myProcess.Start();
    }
}

private void Process_Exited(object sender, System.EventArgs e) {
    status = false;
}

Now the diagnostic message will come up only once per life time of your object. Once the process exits, the status is reset to false, letting you click the button again.

Also note that since the button does not do anything after the status is set to true, it is a good idea to disable it to avoid confusing end-users.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Additionally, you may consider disabling the button, since it won't do anything anyway – GolezTrol Oct 22 '16 at 23:44
  • Oh, I've tried the same thing, but my problem was, that I've added directly under "using System.Windows.Forms;" – JustMe.77 Oct 22 '16 at 23:47
  • Thanks for your help, any way to give you reputation points or something similar here ? – JustMe.77 Oct 22 '16 at 23:47
  • @JustMe.77 If the answer works, you can accept it by clicking the grey check mark next to it. The checkmark becomes active 15 minutes after you ask the question, so you if you would like to do it, you would need to wait for five more minutes. Good luck with your project! – Sergey Kalinichenko Oct 22 '16 at 23:49
  • But I've another question, hope you can answer it: – JustMe.77 Oct 22 '16 at 23:49
  • Sorry, for the multiple comments, I tried to add a new line with enter but it sends my reply :D My other question is: I'd like to set the status to false again if I close the program. How I can do that exactly? Thanks for your help – JustMe.77 Oct 22 '16 at 23:51
  • The status is false upon start. 'bool status = false' declares a new variable which is set to false upon start of your application. What exactly do you mean?! – DoubleVoid Oct 22 '16 at 23:54
  • What do you mean guys? I don't understand it well, I've just started today with it. – JustMe.77 Oct 22 '16 at 23:56
  • The current code look like this: bool status = false; private void button1_Click(object sender, EventArgs e) { if (status != true) { status = true; System.Diagnostics.Process.Start("C:\\Users\\David\\Desktop\\Test\\Test.exe"); } } Why is the button useless? With the button, I start the .exe I want – JustMe.77 Oct 22 '16 at 23:57
  • @dasblinkenlight I've opened the Test.exe, if I click again on it, it won't open (that's good), but if I close the Test.exe and try to reopen it with the button, its not working since the status is still set on true. That's why I am asking for a methode to check if I closed the program, so I can set the status to false again. – JustMe.77 Oct 23 '16 at 00:00
  • @dasblinkenlight I'm not closing the application, only the Test.exe which is going to be executed if I click on the button. The Test.exe is not listed in the Task Manager once I click on the X Button. – JustMe.77 Oct 23 '16 at 00:10
  • @dasblinkenlight Hey, thanks for your answer again. My current code look like this: http://pastebin.com/TcVzZr9k But I'm getting 3 times following error code: CS0103. Also, what do you mean with your last sentence? Why should I disable the button? I want that the user is anytime able to disable / enable the Test.exe. – JustMe.77 Oct 23 '16 at 00:26
  • @dasblinkenlight I'm now getting following error code on the new line CS0246. Current code look like: http://pastebin.com/icRs5vWV – JustMe.77 Oct 23 '16 at 00:37
  • @JustMe.77 Your latest sample is missing a `using System.Diagnostics;` for `myProcess`. Here are more details at [MSDN](https://msdn.microsoft.com/en-us/library/w7xf6dxs.aspx). – Jed Burke Oct 23 '16 at 01:26
  • The Errors are gone now @JedBurke, but I cant open the Test.exe again once I've closed the Test.exe.. What's wrong with my code? – JustMe.77 Oct 23 '16 at 13:58
  • @JustMe.77 Try moving the declaration of `myProcess` outside the `button1_Click` method to see if the callback would start happening. – Sergey Kalinichenko Oct 23 '16 at 14:25
  • @JustMe.77 You need to set `myProcess.EnableRaisingEvents = true;` Mentioned [here](http://stackoverflow.com/a/4504208) citing [this](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.enableraisingevents.aspx) MSDN article. – Jed Burke Oct 23 '16 at 14:25
  • 1
    @dasblinkenlight Indeed it is, I only learned about it now myself. – Jed Burke Oct 23 '16 at 14:31
  • @dasblinkenlight Move it to where exactly? I'm getting errors if I move it like this: http://pastebin.com/GBgKMw3K – JustMe.77 Oct 23 '16 at 14:32
  • 1
    @dasblinkenlight and Jedburke It work's like this, I've added myProcess.EnableRaisingEvents = true;. Thanks a lot to you two guys, appreciated! – JustMe.77 Oct 23 '16 at 14:34
0

The problem is that you are declaring the variable inside the click_button event, so the variable cannot be reached outside the method "click_button"

Try with something like this

bool status = false;
int ChangeStatus()
{
    if(status!=true)
    {
        status = true;
        System.Diagnostics.Process.Start("C:\\Users\\David\\Desktop\\Test\\Test.exe");
        return 0;
    }
  return 1; //if the status is false it will return 1 or the value you want
}

Then in the Button_Click event you should add

 private void button1_Click(object sender, EventArgs e) {
     //I don't know where you will display or save the value (0 or 1) I will assign it in a variable
    var result = ChangeStatus();
 }

Hope it could help you

Hugo Quiñónez
  • 176
  • 1
  • 9
  • Thanks for your reply, I've tried like this (don't know how to paste it as code here, so I'll use pastebin) http://pastebin.com/C0nX9cTj But I can't reopen Test.exe, once I close it. Could you tell me what I am doing wrong? – JustMe.77 Oct 23 '16 at 00:19
  • I don't understand what you mean with "reopen". So you want that your app read the last status? So if I set the status "true" and I close the app, then when I open the app again you want the status to be true? – Hugo Quiñónez Oct 23 '16 at 00:44
  • I want it like this: Open Test.exe -> Status is on true. Then, if I close Test.exe, the status should be false so I can open it again. Because currently, I wouldn't be able to open the Test.exe, even if the Test.exe has been closed. – JustMe.77 Oct 23 '16 at 00:49