【问题标题】:Restricting the instance of a process to 1 in C#在 C# 中将进程的实例限制为 1
【发布时间】:2011-07-18 10:09:18
【问题描述】:

我目前有一个大问题,下面的代码检查文件中的关键字“mp4:production/CATCHUP/”,如果找到它会启动一个可执行文件,尽管因为它找到了多个(完全相同)实例“mp4:production/CATCHUP/”它启动了几个进程。有没有限制这一点,以便在找到一个实例时它可能会停止查找?

我的代码如下:

string s = "";
        private void CheckLog()
        {
            bool _found;
            while (true)
            {
                _found = false;
                Thread.Sleep(5000);
                if (!System.IO.File.Exists("Command.bat")) continue;
                using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat"))
                {

                    while ((s = sr.ReadLine()) != null)
                    {
                        if (s.Contains("test"))
                        {
                            _found = true;
                            break;
                        }

                    }
                }
                if (_found)
                {
                    // Deletes filename in the log file, as the filename is instead handled by p.start

                    var result = Regex.Replace(s, @"test", string.Empty);

                    s = result;

                    RemoveEXELog(); // Deletes a specific keyword from Command.bat
                    RemoveHostFile();
                    Process p = new Process();
                    p.StartInfo.WorkingDirectory = "dump";
                    p.StartInfo.FileName = "test.exe";
                    p.StartInfo.Arguments = s;

                    p.Start();
                    p.WaitForExit();

                    MessageBox.Show("Operation Successful!");
                    string myPath = @"dump";
                    System.Diagnostics.Process prc = new System.Diagnostics.Process();
                    prc.StartInfo.FileName = myPath;
                    prc.Start();


                    ClearLog(); // Deletes Command.bat and then creates a new empty Command.bat
                    LogTrue();
                }
            }
        }

【问题讨论】:

  • 在 (!found) {} 时制作。这太琐碎了,在发布之前至少花 10 分钟思考您的代码。
  • 你为什么每天都开一个新账户?
  • @Henk Holterman 我使用 VPN,所以我不能使用 OPenID,或者它不记得我了
  • 我一定是错过了什么,Hans Passant 的评论有什么问题?
  • @Jodrell 是什么让你觉得有?

标签: c# .net windows process


【解决方案1】:

对于这种情况,我将使用 Singleton 类来管理工作流。 Singleton 将以全局线程安全的方式管理与您的 _found 变量等效的变量。然后所有线程都会查询这个属性。

类似于以下内容:

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();


   private Singleton() {}

   public bool Found { get; set; }

   public static Singleton Instance
   {
      get 
      {
         if (instance == null) 
         {
            lock (syncRoot) 
            {
               if (instance == null) 
                  instance = new Singleton();
            }
         }

         return instance;
      }
   }
}

那么您的代码将如下所示:

private void CheckLog()
    {
        //bool _found; //not needed anymore
        while (!Singleton.Instance.Found)
        {
            //_found = false;
            Thread.Sleep(5000);
            if (!System.IO.File.Exists("Command.bat")) continue;
            using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat"))
            {

                while ((s = sr.ReadLine()) != null)
                {
                    if (s.Contains("mp4:production/CATCHUP/"))
                    {
                        Singleton.Instance.Found = true;
                        break;
                    }

                }
            }
            if (Singleton.Instance.Found)
            {
                // Deletes filename in the log file, as the filename is instead handled by p.start

                var result = Regex.Replace(s, @"rtmpdump", string.Empty);

                s = result;

                RemoveEXELog(); // Deletes a specific keyword from Command.bat
                RemoveHostFile();
                Process p = new Process();
                p.StartInfo.WorkingDirectory = "dump";
                p.StartInfo.FileName = "test.exe";
                p.StartInfo.Arguments = s;

                p.Start();
                p.WaitForExit();

                MessageBox.Show("Operation Successful!");
                string myPath = @"dump";
                System.Diagnostics.Process prc = new System.Diagnostics.Process();
                prc.StartInfo.FileName = myPath;
                prc.Start();


                ClearLog(); // Deletes Command.bat and then creates a new empty            Command.bat
                LogTrue();
        }
    }
}

【讨论】:

    【解决方案2】:

    正如 Hans Passant 所建议的,一旦您发现它接近时,明显的停止循环有什么问题?不需要单例。

    private void CheckLog()
    {
        bool found = false;
        while (!found)
        {
            //your code ...
            while ((s = sr.ReadLine()) != null)                     
            {
                if (s.Contains("test"))   
                {
                    _found = true;
                    break;
                }
            }
            if (found)
            {
                //some more of your code ...
            }
            else
            {
                //get ready for the next iteration
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2021-05-02
      相关资源
      最近更新 更多