CloseMainWindow 失效????

m0792 2009-01-04 01:44:18
本人是C#初学者,写了一下一段代码,结果是:notepad没有关闭,但如果将程序中的if...else...注释掉却又可以了,不知道是怎么回事,往大家指点,谢谢


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace AutomationTestTraining
{
class Program
{
static void Main(string[] args)
{
try
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.Start();

if (myProcess.Responding)
System.Console.WriteLine("Responding");
else
System.Console.WriteLine("Not Responding");

Thread.Sleep(2000);
myProcess.CloseMainWindow();
myProcess.Close();
}

catch(Exception e)
{
Console.WriteLine("Something wrong");
Console.WriteLine(e.Message);
}

}
}
}
...全文
203 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
m0792 2009-01-04
  • 打赏
  • 举报
回复
The main window is the window that is created when the process is started. After initialization, other windows may be opened, including the Modal and TopLevel windows, but the first window associated with the process remains the main window.

You can get the MainWindowHandle property only for processes that are running on the local computer. The MainWindowHandle property is a value that uniquely identifies the window that is associated with the process.

A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window, the MainWindowHandle value is zero. If you have just started a process and want to use its main window handle, consider using the WaitForInputIdle method to allow the process to finish starting, ensuring that the main window handle has been created. Otherwise, an exception will be thrown.

Windows 98, Windows Millennium Edition Platform Note:

This property is not available on this platform if you started the process with ProcessStartInfo..::.UseShellExecute set to true.

^_^
m0792 2009-01-04
  • 打赏
  • 举报
回复
如楼上所说,我调试了几次,果然是那样

能告诉为什么吗?
米菲爸爸 2009-01-04
  • 打赏
  • 举报
回复
其实并不是myProcess.CloseMainWindow()失效,而是MainWindowHandle的值变了,你观察几次就明白了。如果MainWindowHandle=0了,就肯定不能关闭
m0792 2009-01-04
  • 打赏
  • 举报
回复
我单步走的时候,myProcess.CloseMainWindow()执行了,而且notepad也关了。为什么 ctrl+F5 或者F5 就不管用了呢?
米菲爸爸 2009-01-04
  • 打赏
  • 举报
回复
Start 以后稍等一会就好了

Process myProcess = new Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.Start();
//add some time
Thread.Sleep(2000);
//get MainWindowHandle (just for verify you did get MainWindowHandle)
IntPtr MainWindowHandle = myProcess.MainWindowHandle;
if (myProcess.Responding)
System.Console.WriteLine("Responding" + MainWindowHandle);
else
System.Console.WriteLine("Not Responding");

Thread.Sleep(2000);
myProcess.CloseMainWindow();
myProcess.Close();
whoami333 2009-01-04
  • 打赏
  • 举报
回复
你是在调试模式下吧。直接运行试一下。
Jack2013tong 2009-01-04
  • 打赏
  • 举报
回复
调试下是否有执行到
myProcess.CloseMainWindow();
这个代码?

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧