I have an application written in C#. Basically its an exe. This application scans for the network at 3 seconds interval and populates the database with the network information.
I want to run this application from asp.net mvc for few seconds and then stop it and again start and stop.
I need to start the exe at the click of a start button and need to stop it at the click of a stop button. The exe will be running continuously until I click on the stop button after it gets invoked.
Is it possible to call this exe from asp.net mvc framework?
If yes, how can it be done? I need some pointers. Please provide me.
You will need namespaces System.Diagnostics & System.Timers. Then follow below steps.
static System.Timers.Timer tTimer;
const Int32 iInterval = 30;
static Boolean IsProcessRunning = false;
static Int32 iProcessID = 0;
static Int32 SetTimerInterval(Int32 minute)
{
if (minute <= 0)
minute = 60;
DateTime now = DateTime.Now;
DateTime next = now.AddMinutes((minute - (now.Minute % minute))).AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1);
TimeSpan interval = next - now;
return (Int32)interval.TotalMilliseconds;
}
static void timer_Elapsed(object sender, EventArgs e)
{
if (!IsProcessRunning)
{
ProcessStartInfo objStartInfo = new ProcessStartInfo();
objStartInfo.FileName = "C:\\Windows\\notepad.exe";
Process objProcess = new Process();
objProcess.StartInfo = objStartInfo;
objProcess.Start();
iProcessID = objProcess.Id;
IsProcessRunning = true;
}
else
{
Process objProcess = Process.GetProcessById(iProcessID);
objProcess.Kill();
IsProcessRunning = false;
}
tTimer.Interval = SetTimerInterval(iInterval);
}
Then on your start button click...
tTimer = new System.Timers.Timer();
tTimer.Interval = SetTimerInterval(iInterval);
tTimer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
tTimer.Start();
You can stop this anytime by...
tTimer.Stop();
And you are ready to go...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.