-1

I need to create running windows process (the one seen on task manager) when a Windows form loads because I need this application to be monitored by nagios (http://www.nagios.org/). So when the form loads, an exe will run in the background process, and when form closes, the process will have to stop too.

Update

So when the form loads, the current ApplicationName.exe will display in the Task Manager Processes Tab, and when form closes, the ApplicationName.exe will have to stop too.

I also found out that when you Start Without Debugging, the ApplicationName.exe will display in the Processes Tab of the Task Manager but if you Start Debugging (F5), you wont see the ApplicationName.exe in the Processess Tab. Now i want to make sure that even if I will Start Debugging it, I can still see the ApplicationName.exe in the Processes Tab. How do i do that?

light
  • 33
  • 7
  • you can create a windows service app and start it when form is invoke and stop it when your form is closed – Zaki Apr 10 '12 at 14:22
  • Do you mean a "a Windows form" that is part of an application you authored, or another form? – dumbledad Apr 10 '12 at 14:36
  • yes @Sam1, i can do that. but i wanted to make it so that i wouldnt have to create another app that would be executed in form Load. – light Apr 10 '12 at 16:31
  • @dumbledad - yes. same application – light Apr 10 '12 at 16:32

3 Answers3

0

Clarification needed: is the additional process that is running the Nagios monitor, or something else you create?

Either way, you can use Process.Start() to kick off a separate application from within your own:

//event handler for the form's Load event
public void MyWindow_FormLoad(object sender, EventArgs e)
{
   //kick off the process you want
   Process.Start(@"C:\Program Files\MyOtherApp\MyOtherApp.exe");
}

There are overloads allowing you to specify arguments, or customize the startup behavior of the process. But this is the basic call and (given a real program location) should kick off the separate EXE as a new process with default startup behavior (as if you'd double-clicked it in Windows Explorer).

Now, if you need more, like a way to have the two programs talk to each other, then you'll need to expand your question with the appropriate details.

EDIT FROM COMMENT: Ah. OK, that's slightly different.

Normally, any EXE that is running at any given time will appear in the Task Manager's processes list by default, with no special coding necessary. It's in fact very hard to get a proces to NOT show up in that list, because a process that doesn't want to be seen is one of the hallmarks of a virus.

However, when you run an app in Debug mode from Visual Studio, the code is compiled and run from within VS's process boundaries, and doesn't show as its own process. To get it to show up as its own process in Task Manager, the compiled application must be run from outside VS. You can still debug it, by "attaching" VS's debugger to the running process after you have started it. But, this means that the app must be stable and long-running enough for you to manually attach to it. A program that has finished most or all of its execution by the time it reaches a "resting" state will need some modification in order to wait for you to attach to it before doing whatever it was you wanted to debug.

KeithS
  • 70,210
  • 21
  • 112
  • 164
  • Sorry for the confusion, I wanted to just run the current app or make the current app exist in the Task Manager Processes Tab even if I Started Debugging it. – light Apr 10 '12 at 16:59
  • Ok KeithS. Understood. so the only way for me to make this Application to be monitored is to create another App and make this run when the form Loads. I get it now. Thanks for the help! :) – light Apr 10 '12 at 19:06
  • I'm not sure you get it. All you have to do is go into the bin/debug directory of the main project of your app, and run the exe that's in there. That will start your application as its own process, and then you can attach the VS debugger to it if you need to trace through execution. – KeithS Apr 10 '12 at 19:25
  • Yes but remember Im putting the code in the form Load. If I run the current app exe when the form Loads, it will trigger another form_Load event and will keep on calling the exe. and so on. – light Apr 10 '12 at 20:24
  • OK, now I'm completely confused as to what you want, and I think you're completely confused as to what I'm telling you to do. If you want your app to appear in the Process list as an entry, then simply launch it by double-clicking instead of using the Start Debugging button in VS. No Form_Load code needed. If you want to launch a second application when a form in your current app loads, then use the Process.Start(). If you want your app to launch *itself* when its main form loads, why? That's an infinite loop no matter how you start the first copy, and I can't think of any use for it. – KeithS Apr 10 '12 at 20:30
  • No, I cannot start the app by double clicking it. I wanted it to run as a Windows Service but for some reasons I am just currently running it by Debugging. So again, the "only way for me to make this Application to be monitored is to create another App and make this run when the form Loads". This other app that i will create will be a Windows Service. The Windows Service will be the one I will start when form Loads and I will Stop the Windows Service when the form Closes. This way, the Windows Service will be monitored by Nagios since it will be enlisted in the services/processes list. – light Apr 10 '12 at 20:53
  • OK, now I get it. Don't use Process.Start for this. Instead, use a ServiceController pointed at the service on the current machine, and call its Start() method (then wait for it to report it's running), then when the form closes call Stop() (and wait for the service to report it has stopped). See this question: http://stackoverflow.com/questions/467367/how-can-i-programmatically-stop-start-a-windows-service-on-a-remote-box – KeithS Apr 10 '12 at 21:02
  • Yes i can manage somehow from this point onwards. Thanks a lot for your help! :) – light Apr 10 '12 at 21:21
0

If you just want a Windows form application you have written to show up in the list of processes when it runs then you will find it does anyway. You do not need to do anything special for it to run in its own process. For example I made a simple out-of-the-box Windows form application in VS2010

Vanilla Windows form application

And then ran it (without debugging) and here it is in the process list of Task Manager.

Task Manager's list of running processes

(N.B. I'm running Win8, so your Task Manager may look a little different.)

If however you need to know when a Windows form application not written by you is started and stopped by the user you'll need to look at Windows hooks as mentioned in the answers to this question or at the process creation/modify/shutdown events in the Windows Management Instrumentation(WMI) API.

Community
  • 1
  • 1
dumbledad
  • 16,305
  • 23
  • 120
  • 273
  • Thanks! I didn't see the Windows Form.exe on Task Manager because I always don't Start it Without Debugging. – light Apr 10 '12 at 16:49
0

I have Test.exe ,a sample winform app. and used it as Process

    public Process process = new Process(); 
    protected override void OnLoad(EventArgs e)
    {
        process.StartInfo.Verb = "open";
        process.StartInfo.FileName = "Test.exe"; //Give your App or Process Name
        process.StartInfo.WorkingDirectory = @"C:\Users\sali\Documents\Visual Studio2010\Projects\Test\Test\bin\Debug"; //Give your App or Process path
        process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        process.Start();
    }

I hope it helps Or if you require something different, feel free to ask

Syed
  • 308
  • 1
  • 4
  • 12
  • Hi Syed, thanks for your reply. It didnt seem to work on my side. I didnt see "Test.exe" on the Processess tab in Task Mananger. – light Apr 10 '12 at 16:41
  • It did work when Started Without Debugging. In my case, I always "Start Debugging" the project. – light Apr 10 '12 at 17:36
  • You need to click "Show Processes from all users" button on Bottom Left of Task Manager, then you'll be able to see that – Syed Apr 10 '12 at 17:40
  • try { process.StartInfo.Verb = "open"; process.StartInfo.FileName = "Test.exe"; process.StartInfo.WorkingDirectory = @"C:\Users\sali\Documents\Visual Studio 2010\Projects\Test\Test\bin\Debug"; process.StartInfo.WindowStyle = ProcessWindowStyle.Normal; process.Start(); } finally { process.Dispose(); } – Syed Apr 10 '12 at 17:51
  • Hi Syed, that checkbox has always been checked even from the start. and the ApplicationName.exe doesnt still show if I Start Debugging it. – light Apr 10 '12 at 19:01