就是已知一个程序在运行,然后另做一个监控程序,我想知道如何监控那个已运行程序,或者说我如何判断那个已运行程序是否还在运行中,判断线程?有什么方法大家讲解一下,不是很明白?

解决方案 »

  1.   

    判断当前进程是否存在。:            
    System.Diagnostics.Process[] updatePro = System.Diagnostics.Process.GetProcessesByName("userproname");
      

  2.   

    public static Proccess RunningInstance()
    {
      Process current = Process.GetCurrentProcess();
      Process[] processes = Process.GetProcessesByName(Current.ProcessName);
      foreach(Process process in processes)
      {
        if(process.Id != current.Id)
        {
          if(Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
          {
             return process;
          }
        }
         return null;
      }
    }
      

  3.   

    System.Diagnostics.Process.GetProcessesByName("userproname"); 
    这个函数调用可以得到userproname这个进程是否在。至于启动进程,请参考System.Diagnostics 名字空间下的其他类~
      

  4.   


    这个函数的意思就是,如果现在已经开启了程序,那么返回这个程序的process,这样你就可以启动它了。如果没有开启程序,那么返回Null。直接使用这个函数就行了。
      

  5.   

    public static Proccess RunningInstance() 

      Process current = Process.GetCurrentProcess(); 
      Process[] processes = Process.GetProcessesByName(Current.ProcessName); 
      foreach(Process process in processes) 
      { 
        if(process.Id != current.Id) 
        { 
          if(Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) 
          { 
            return process; 
          } 
        } 
        return null; 
      } 
    }麻烦问下,那这段代码应该写在什么地方呢,是写在winform里的load事件里还是写在程序的program里呢,麻烦了...
      

  6.   

    System.Diagnostics.Process[] updatePro = System.Diagnostics.Process.GetProcessesByName("userproname"); 
    这是 判断 程序是否运行,
     重启程序  process  对象的  start 方法
      

  7.   

    判断这个程序是否运行,那返回值是什么,我如何做判断,比如if(A=true)之类的.
      

  8.   

    你会查msdn么?搜索一下msdn啊~
      

  9.   

    System.Diagnostics.Process[] updatePro = System.Diagnostics.Process.GetProcessesByName("userproname"); 
    这个是判断是否存在启动System.Diagnostics.Process.start();
    这里是启动你也可以找一下其它的属性很方便的
      

  10.   

    public static Proccess RunningInstance() 

      Process current = Process.GetCurrentProcess(); 
      Process[] processes = Process.GetProcessesByName(Current.ProcessName); 
      foreach(Process process in processes) 
      { 
        if(process.Id != current.Id) 
        { 
          if(Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) 
          { 
            return process; 
          } 
        } 
        return null; 
      } 
    }现在问一下,是不是我这个代码写到程序里就可以了,但现在这个是不是只是判断这个进程是否在运行,那我如何判断它给我返回的是process还是null,还有判断后如果程序不在那我如何启动我所要监控的程序呢?
    直接写System.Diagnostics.Process.start(程序名); 这个么
      

  11.   

    守护进程,B守护A,当A被关掉后,B主动去重新调用
      

  12.   

    问题自己解决,把答案写下,让后人知道怎么回事
     bool isHave = false;
                Process[] myProcesses = System.Diagnostics.Process.GetProcesses();//查看所有进程并放到数组myProcesses里
                foreach (Process myProcess in myProcesses)//从进程里一个一个读出并在下面的if语句里与“”里的内容比对
                {
                    if ("ProcessMonitoring" == myProcess.ProcessName)
                    {
                        isHave = true;//如果为true则退出
                        break;
                    }
                }            if (!isHave)//如果不存在,则进行相应的操作
                {
                   //写自己所需要的操作
                }
    如果有不明白的可以加我QQ27461779问我,基本上大部分时间我都会在线,好了散分