用C#实现启动另一程序的方法实例
程序员文章站
2023-12-20 13:08:22
复制代码 代码如下: private void btncreate_click(object sender, eventargs e) ...{ int hwnd...
复制代码 代码如下:
private void btncreate_click(object sender, eventargs e)
...{
int hwnd = findwindow(null, "test");//窗體的名稱
//check if powerreuse is launched or not
//if yes, pass path of project to powerreuse
//or, launch powerreuse with specified parameter
if (hwnd > 0)
...{
messagebox.show("powerreuse has been launched already." + " " + hwnd.tostring());
//sendmessage to powerreuse
return;
}
try
...{
process main_p = new process();
//this path should be retrieved from windows registry,
//the loaction is written by installter during process of installation.
main_p.startinfo.filename = @"c: est.exe";//運行的exe路徑
//this url is passed to powerreuse to open
main_p.startinfo.arguments = @"c:tempabc.prj";//運行時的參數
main_p.startinfo.useshellexecute = true;
main_p.start();
//
//we have to wait for a while until ui has been initialized
//
main_p.waitforinputidle(10000);
//although ui has been initialzied,
//it does not mean main form of application has been completed.
//we may wait for another 10 seconds
for (int i = 0; i < 100; i++)
...{
hwnd = findwindow(null, "powerreuse (beta)");
//hwnd = main_p.mainwindowhandle.toint32() ;
if (hwnd > 0) break;
thread.sleep(100);
}
//here, we check if powerreuse is fully launched
if (hwnd == 0)
...{
//handle exception
messagebox.show("we cannot find window handle of powerreuse");
}
else
...{
//other handling
//
messagebox.show(hwnd.tostring() + " " + main_p.mainwindowhandle.tostring() + " " + main_p.mainwindowtitle);
}
}
catch (exception ex)
...{
messagebox.show(ex.message);
}
}