检测相同的可执行文件是否正在运行,并等待直到其关闭

本文关键字:等待 运行 可执行文件 是否 检测 | 更新日期: 2023-09-27 18:15:40

我有一个新闻和一些指南的web应用程序。

在后台你有一个功能,你可以上传多个图像没有限制,因为分页。

控制器在图像被成功添加到一个ImageOptimizer Task(用于JPEG和PNG)条目后运行。

我今天做了一个大的压力测试,我的内存使用率是100%,因为所有的进程都在同一时间运行。

我的问题是:是否有可能让ProcessStart等待,直到相同的可执行文件结束?那会很有帮助的:-)

启动任务的代码粘贴在下面。所以我在c#中使用了简单的processstart Cls。

    public static string Do(string path, bool clientMode = false)
    {
        /** I want to do something like this:**/
        while(ThisExecutableIsAllreadyRunning);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.AppendFormat("Optimizing '"{0}'"", path).AppendLine();
        long length = new FileInfo(path).Length;
        stringBuilder.AppendFormat("Size before: {0}", length).AppendLine();
        string text = "~/Executables/optipng.exe";
        if (clientMode)
        {
            if (String.IsNullOrEmpty(ClientModeExecutablePath))
                throw new Exception("Client Mode for IMG Optim required ClientModeExecutablePath to be set");
            text = ClientModeExecutablePath;
        }
        else
            text = HttpContext.Current.Server.MapPath(text);
        Process process = Process.Start(text, "-strip all " + path);
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = false;
        process.Start();
        while (!process.StandardOutput.EndOfStream)
        {
            string value = process.StandardOutput.ReadLine();
            stringBuilder.AppendLine(value);
        }
        length = new FileInfo(path).Length;
        stringBuilder.AppendFormat("Size After: {0}", length).AppendLine();
        stringBuilder.AppendLine("Done...");
        return stringBuilder.ToString();
    }

检测相同的可执行文件是否正在运行,并等待直到其关闭

是否有可能让ProcessStart等待直到相同的可执行文件结束?

。如果我没理解错的话,您想要等待进程退出,直到继续。试试下面的代码:

Process process = Process.Start(text, "-strip all " + path);
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.Start();
while (!process.StandardOutput.EndOfStream)
{
     string value = process.StandardOutput.ReadLine();
     stringBuilder.AppendLine(value);
}
process.WaitForExit();  // <-------- WAIT HERE

MSDN:

指示Process组件无限期地等待关联进程退出。WaitForExit() 使当前线程等待,直到关联的进程终止。它应该在调用进程上的所有其他方法之后调用。要避免阻塞当前线程,请使用exit事件。

并发

很难判断您的Do()方法是否被并发调用。如果是这样,您可能希望使用某种形式的lock()或临界区来保护它,以确保一次只生成一个进程。这将守卫放置在启动器中。

或者,您可以在. exe文件中创建一个命名互斥。如果发现互斥锁先前已退出,则应立即退出。

祝你好运!

如何检查一个特定的exe应用程序是否由于其他线程或进程而已经在运行:

using System.Diagnostics;
//get all currently running applications
var allProcesses = Process.GetProcesses().ToList();
//filter out the processes that don't match the exe you're trying to launch
foreach(var process in allProcesses.Where(p => p.Modules[0].FileName.ToLower().EndsWith(ClientModeExecutablePath.ToLower())))
{
    try
    {           
        Console.WriteLine("Process: {0} ID: {1}, file:{2}", process.ProcessName, process.Id, process.Modules[0].FileName);
        //wait for the running process to complete
        process.WaitForExit();
    }
    catch (Exception ex)
    {
        Console.WriteLine (ex);
    }
   //now launch your process
   //two threads could still launch the process at the same time after checking for any running processes
}