可以判断让新启动的进程直接杀掉,但是怎么都不能使老进程的界面从最小化变成最大化,恳请各位帮忙解答,谢谢!
在main中

Process[] pr = Process.GetProcessesByName("AssayManager");
if (pr.Length > 1)
{
  try
   {
      pr[0].StartInfo.FileName = @"D:\SecondProWorking\lis\lisAll76\AssayManager.exe";
      pr[0].StartInfo.CreateNoWindow = true;
      pr[0].StartInfo. WindowStyle = ProcessWindowStyle.Normal;    
      pr[0].Start();
    }
  catch(Exception e1)
   {
      using (StreamWriter sw = new StreamWriter("er.log",true))
      sw.WriteLine(e1.ToString());
    }
  finally
   {
      pr[1].Kill();
   }
}
else
   Application.Run(new AssayManagerForm());
我这段代码怎么都不行。

解决方案 »

  1.   

    using (StreamWriter sw = new StreamWriter("er.log",true))
     {
       sw.WriteLine(e1.ToString());
     }
    这段代码我写错了。
      

  2.   

    你修改的是startinfo,如果程序已经运行,这些都是没用的,
    我记得好像是要调用api来实现,先调用SetWindowLong,然后再调用SetActiveWindow
      

  3.   

    楼上谢谢你,按照你的提示我已实现了,但是我还是想如果不用API,难道用C#间的进程通信就不能实现吗?期待各位高人发表意见,谢谢!
      

  4.   

    参考:static void Main(string[] args) 
    {
          
    Process instance = RunningInstance();
    if (args.Length == 0) 

    if(instance == null)
    {
    int result = MainForm.CheckLogin();                    if( result == 0 )
                        {
                            Application.Run(new MainForm());
                        }
                        else return;
    }
    else
    {
    DialogResult result = MessageBox.Show("您确定退出当前已经运行的程序么?\n 可能会导致未保存的内容丢失","程序已运行",MessageBoxButtons.OKCancel ,MessageBoxIcon.Information);
    if(result == DialogResult.No)
    {
    HandleRunningInstance(instance);
    }
    else if(result == DialogResult.OK)
    {
    instance.Kill(); Application.Run(new MainForm());
    }
    else
    {
    return;
    }
    }
    } else
    {   
    if(instance == null)
    {
    Application.Run(new MainForm("0" + args[0]));
    }
    else
    {
    DialogResult result = MessageBox.Show("您确定退出当前已经运行的程序么?\n 可能会导致未保存的内容丢失","程序已运行",MessageBoxButtons.OKCancel ,MessageBoxIcon.Information);
    if(result == DialogResult.No)
    {
    HandleRunningInstance(instance);
    }
    else if(result == DialogResult.OK)
    {
    instance.Kill();
    Application.Run(new MainForm("0" + args[0]));
    }
    else
    {
    return;
    }
    }
    }
    } public static Process RunningInstance()
    {
    Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName (current.ProcessName); //Loop through the running processes in with the same name
    foreach (Process process in processes)
    {
    //Ignore the current process
    if (process.Id != current.Id)
    {
    //Make sure that the process is running from the exe file.
    //   if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
    //   current.MainModule.FileName)
    //   {
    //Return the other process instance.
    return process;
    //   }
    }
    }
    //No other instance was found, return null.
    return null;
    } public static void HandleRunningInstance(Process instance)
    {
    //Make sure the window is not minimized or maximized
    ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL); //Set the real intance to foreground window
    SetForegroundWindow (instance.MainWindowHandle);
    } [DllImport("User32.dll")] 
    private static extern bool ShowWindowAsync(
    IntPtr hWnd, int cmdShow);
    [DllImport("User32.dll")] private static extern bool
    SetForegroundWindow(IntPtr hWnd);
    private const int WS_SHOWNORMAL = 3;
      

  5.   

    http://blog.csdn.net/serversql/archive/2006/04/06/652590.aspx
    这里有你需要的
      

  6.   

    给大家看看,我最后实现的:
    Process[] pr1 = Process.GetProcessesByName("AssayManager");
    if (pr1.Length > 1)
    {
      try
      {
        IntPtr winF = pr1[0].MainWindowHandle;
        int m = AttachThreadInput(pr1[0].Threads[0].Id,pr1[1].Threads[0].Id,1);
        SetActiveWindow(winF);
      }
      catch(Exception e1)
      {
        using (StreamWriter sw = new StreamWriter("er.log",true))
        {
          sw.WriteLine(e1.ToString());
        }
      }
     finally
     {
     }
    }
    else
      Application.Run(new BackChargeForm());