本人是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);
} }
}
}
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);
} }
}
}
myProcess.CloseMainWindow();
这个代码?
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();
^_^