简体   繁体   English

从asp.net mvc调用exe

[英]calling an exe from asp.net mvc

I have an application written in C#. 我有一个用C#编写的应用程序。 Basically its an exe. 基本上是一个exe文件。 This application scans for the network at 3 seconds interval and populates the database with the network information. 该应用程序每隔3秒扫描一次网络,并使用网络信息填充数据库。

I want to run this application from asp.net mvc for few seconds and then stop it and again start and stop. 我想从asp.net mvc运行此应用程序几秒钟,然后将其停止,然后再次启动和停止。

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. 我需要单击开始按钮来启动exe,并且需要单击停止按钮来停止它。 The exe will be running continuously until I click on the stop button after it gets invoked. 该exe文件将一直运行,直到调用它后我单击停止按钮为止。

Is it possible to call this exe from asp.net mvc framework? 是否可以从asp.net mvc框架调用此exe?

If yes, how can it be done? 如果是,该怎么办? I need some pointers. 我需要一些指示。 Please provide me. 请提供我。

You will need namespaces System.Diagnostics & System.Timers. 您将需要名称空间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... 你准备好了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM