可以判断让新启动的进程直接杀掉,但是怎么都不能使老进程的界面从最小化变成最大化,恳请各位帮忙解答,谢谢!
在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());
我这段代码怎么都不行。
在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());
我这段代码怎么都不行。
{
sw.WriteLine(e1.ToString());
}
这段代码我写错了。
我记得好像是要调用api来实现,先调用SetWindowLong,然后再调用SetActiveWindow
{
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;
这里有你需要的
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());