有这样的程序,先打开flashget,查找名称为flashget的,如果有,休息12秒,如果没有,打开他,轮流7200次(一天好像多12秒),然后重起电脑。现在的问题是,如果flashget正在运行,程序没问题,如果flashget退出,程序因为找不到flashget,得到的localbyname这个数组是空的,然后localbyname[0]显然是错误的,程序就跳出错误。
如果要加入错误修正,就是检查localbyname,如果是空的,就不运行下面的,直接打开flashget。
这样的错误修正程序应该怎么写呢,看msdn也不明白,求助啊。程序如下:using System;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
   public class MyProcess
     { 
                     public static void Main()
          {        
             Process.Start("c:\\flashget.exe");
             Thread.Sleep(12000);
            Process  [] localbyname = Process.GetProcessesByName("flashget");   
            Process myProcess= localbyname[0];
             for (int i = 0;i < 7200; i++)
                 {
                    if (!myProcess.HasExited)
                         {
                          // Discard cached information about the process.
                         myProcess.Refresh();
                           // Print working set to console.
                         Console.WriteLine("Physical Memory Usage: " 
                                + myProcess.WorkingSet.ToString());
                               // Wait 12 seconds.
                        Thread.Sleep(12000);
                         }
                   else 
                     {
                       Process.Start("c:\\flashget.exe");
                       } 
                    }
               Process.Start("shutdown.exe -r");
                 // Close process by sending a close message to its main window.
             //myProcess.CloseMainWindow();
            // Free resources associated with process.
           //myProcess.Close();      }    }        
}感谢上一个贴子中youzj(无缘)给的帮助,因为这个程序的目标没有那么多的process,因该只有一个,所以直接localbyname[0]就行了。如果这个问题解决了,我就再去看一下使用循环做多个process的问题。上个贴子结贴了我给不了分,不好意思。
请各位大侠帮忙。

解决方案 »

  1.   

    good good study
    day day up
      

  2.   

    Process  [] localbyname = Process.GetProcessesByName("flashget");   
    if( localbyname.Length > 0 )
    {
       // 存在flashget的进程
    }
    else
    {
       //启动flashget
    }
      

  3.   

    这么简单啊,唉,脑子秀逗了,去试一下先。一个数组,如果是空的,那么它的length是什么?
      

  4.   

    知道了,length=0,谢谢上面的,非常感谢,然后我去看一下把process也用i++循环一下,看多process的情况。
      

  5.   

    基本上完成了,做了个process的循环
    using System;
    using System.Diagnostics;
    using System.Threading;
    using System.ComponentModel;namespace MyProcessSample
    {
    /// <summary>
    /// Shell for the sample.
    /// </summary>
    public class MyProcess
    {
    //
           
    public static void Main()
    {
    string path = "c:\\Program Files\\flashget\\flashget.exe";
    for (int j = 0;j < 8440; j++)
    {
    Process [] localByName = Process.GetProcessesByName("flashget");
    if (localByName.Length >0)
    {

    for (int i = 0;i < localByName.Length; i++)
    {
    Process Myprocess = localByName[i];
    if (!Myprocess.HasExited)
    {
    // Discard cached information about the process.
    Myprocess.Refresh();
    // Print working set to console.
    //Console.WriteLine("Physical Memory Usage: " 
    // +Myprocess.WorkingSet.ToString());
    // Wait 20 seconds.
    Thread.Sleep(20000);
    }
    else 
    {
    Myprocess = Process.Start(path);

    }
    // Close process by sending a close message to its main window.
    // process.CloseMainWindow();
    // // Free resources associated with process.
    // process.Close(); }
    else 
    {
    Process.Start(path);
    }

    }
    Process.Start("shutdown.bat");
    }        
    }
    }然后就是一个问题,Process.Start 打开的程序可不可以带参数,比如"shutdown.exe -r"这样子。
    我找msdn没找到,有一个info可是没有这个。
    还是显示结贴, 还是都没加分,,奇怪,郁闷中