[英]Run more than one instance of console application using .bat file
I have one batch file which run a console application written in C#. 我有一个批处理文件,该文件运行用C#编写的控制台应用程序。
This application will move some files from one folder to another application and make an entry in a databse. 该应用程序会将一些文件从一个文件夹移动到另一应用程序,并在数据库中进行输入。
The batch file is scheduled to run every 10 minutes. 批处理文件计划每10分钟运行一次。
Would there be any issues if one .exe runnig and after 10 minute another instance of same .exe is started? 如果运行一个.exe并在10分钟后启动同一.exe的另一个实例,会不会有问题?
What will happen if another instance of the .exe starts after 10 minute and the previous instance of the .exe is still running? 如果.exe的另一个实例在10分钟后启动并且该.exe的先前实例仍在运行,将会发生什么?
I would suggest forcing your executable to allow only a single instance. 我建议强制您的可执行文件只允许一个实例。
there was a question about how to run a single instance in C# with a mutex the link provided in the answer shows a great example. 有一个关于如何使用互斥量在C#中运行单个实例的问题,答案中提供的链接显示了一个很好的例子。
a single instance would defiantly prevent any database/file access exceptions. 一个实例将严格阻止任何数据库/文件访问异常。 if it runs every ten minutes I don't think canceling it as another instance is already running would be a bad idea, if not you could always schedule a new one a few minutes ahead.
如果它每十分钟运行一次,我不认为要取消它是因为另一个实例已经在运行,这不是一个好主意,否则,您总是可以提前几分钟安排一个新的实例。
There are various things you could try. 您可以尝试各种方法。
Perhaps, query the running processes and exit if a previous instance is running. 也许,查询正在运行的进程,如果先前的实例正在运行,则退出。
Alternatively, you could check to see if the file is in use inside the file copy program (a typical method used in file watchers). 或者,您可以检查文件复制程序中是否正在使用该文件(文件监视程序中使用的一种典型方法)。
I had a similar problem, what I did was write a quick console app that acted as an interface for the app. 我有一个类似的问题,我当时做的是编写一个充当应用程序界面的快速控制台应用程序。
within the main loop I wrote code to grab the processes running on the computer and checked to see if an instance is already launched. 在主循环中,我编写了代码以捕获计算机上正在运行的进程,并检查是否已启动实例。 If not I launched my executable.
如果没有,我启动了我的可执行文件。
Process[] iProcesslist = Process.GetProcesses();
foreach (Process process in iProcesslist)
{
if (process.ProcessName == "programName")
{
Log.Debug("ProgramName is currently running. Please wait for it to exit before launching again");
return 0;
}
else
{
using (var p = new Process())
{
try
{
p.StartInfo.FileName = @"C:\Users\username\Documents\Programming_Projects\programName\ProgramName\bin\Debug\programName.exe";
p.Start();
p.WaitForExit();
}
catch (Exception ex)
{
Log.Debug(ex.Message);
}
}
}
}
May not be the best solution, but it was quick and does what I want. 可能不是最好的解决方案,但是它很快并且可以满足我的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.