Jorge Cuevas
Jorge Cuevas

Reputation: 774

Start a c# console application when .NET MVC Application starts on Visual Studio

I'm looking to launch a console application that has to run parallel to my main .NET MVC Application, when the main app starts.

It is a Console Application that keeps running until I manually quit the process.

My guess is that this can be done somewhere on the Start Up of my MVC application.

How can this be accomplished?

Upvotes: 0

Views: 342

Answers (1)

johnny 5
johnny 5

Reputation: 21005

You probably want to host a windows service to do this, but if you want a quick and dirty way to do so you can spawn a new process in a thread:

 var t = Task.Run(() => {
     Process myProcess = new Process();

     try
     {
         myProcess.StartInfo.UseShellExecute = false;
         // You can start any process, HelloWorld is a do-nothing example.
         myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
         myProcess.StartInfo.CreateNoWindow = true;
         myProcess.Start();
     }
     catch(Exception ex)
     {
     }
 });

Upvotes: 1

Related Questions