Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top