/// <summary>
/// 启动进程的程序路径
/// </summary>
private string m_fileName = "Notepad.exe";
void ProcessAdd(EventArgs e)
{
string path = Environment.CurrentDirectory + "\\MyFile";
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
string argument = System.IO.Path.Combine(path, Guid.NewGuid().ToString() + ".txt");
if (!System.IO.File.Exists(argument))
{
System.IO.File.CreateText(argument);
}
//设置要启动的就用程序及参数
ProcessStartInfo ps = new ProcessStartInfo(m_fileName, argument);
ps.WindowStyle = ProcessWindowStyle.Normal;
Process p = new Process();
p.StartInfo = ps;
p.Start();
//等待启动完成,否则获取进程信息可能失败
p.WaitForInputIdle(); //MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString()); //创建新的Process组件的数组,并将它们与指定的进程名称(Notepad)的所有进程资源相关联.
Process[] myprocesses;
var fileName = System.IO.Path.GetFileNameWithoutExtension(m_fileName);
myprocesses = Process.GetProcessesByName(fileName);
foreach (Process p1 in myprocesses)
{
//通过向进程主窗口发送关闭消息达到关闭进程的目的
p1.CloseMainWindow();
//等待1000毫秒
//System.Threading.Thread.Sleep(1000);
//释放与此组件关联的所有资源
p1.Close();
} MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString());
}这段程序开始是,用记事本打开一个文本文件
然后后面那段是取得当前所有记事本的进程,然后关闭他
但程序运行手,可以开一个记事本的进程打开文件,
后面也可以取得系统所有记事本,
但是在p1.close后关没有关闭这个记本事程序
这是为什么呢?谢谢
解决方案 »
- 关于treeview控件的更新问题
- C#中的括号
- 两个byte[]拼接问题?急.....在线等
- 请各位高人大虾帮小弟指点 ,我这个方法这么写的话,是在做一个什么操作 ,请帮我分析一下。谢谢
- 谁能提供给我一些键盘的API参数啊,急用
- 为什么有些邮件头的主题没有带编码?怎么解?
- 求一图片上传至数据库里的代码。。
- datagrid下边的空白大家怎么解决的。单元格以外.另外有好的编辑办法吗
- 急!急!急!请问大家如何winform里动态添加控件!
- 上传文件过大
- 请高手指导为何s不显示行数,最后未通过审查条数都是零条。哪里错了么?求指导,我是菜菜鸟
- |ZYCWPF| WPF中写控件样式的时候,有没有办法像HTML的CSS写如 Grid Button{Margin5 5 5 5} 谢谢
Application.DoEvents();
MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString());
但还是有问题在测试几十次总会有几次还是返回的是1
----------
难道我的WaitForExit不是已经等待系统把记事本程序关了后再继续执行吗?
为什么WaitForExit后再Close后,
Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString()
还有可能得到这个进程呢?
谢谢 /// <summary>
/// 启动进程的程序路径
/// </summary>
private string m_fileName = "Notepad.exe";
void ProcessAdd(EventArgs e)
{
string path = Environment.CurrentDirectory + "\\MyFile";
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
string argument = System.IO.Path.Combine(path, Guid.NewGuid().ToString() + ".txt");
if (!System.IO.File.Exists(argument))
{
System.IO.File.CreateText(argument);
}
//设置要启动的就用程序及参数
ProcessStartInfo ps = new ProcessStartInfo(m_fileName, argument);
ps.WindowStyle = ProcessWindowStyle.Normal;
Process p = new Process();
p.StartInfo = ps;
p.Start();
//等待启动完成,否则获取进程信息可能失败
p.WaitForInputIdle(); //MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString()); //创建新的Process组件的数组,并将它们与指定的进程名称(Notepad)的所有进程资源相关联.
Process[] myprocesses;
var fileName = System.IO.Path.GetFileNameWithoutExtension(m_fileName);
myprocesses = Process.GetProcessesByName(fileName);
foreach (Process p1 in myprocesses)
{
//通过向进程主窗口发送关闭消息达到关闭进程的目的
p1.CloseMainWindow();
//等待1000毫秒
//System.Threading.Thread.Sleep(1000);
//释放与此组件关联的所有资源
p1.WaitForExit(10000);
p1.Close();
}
MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString()); }
理论上p1.WaitForExit(10000);表示等待1秒钟notepad还没有退出的话,这个方法也就返回了,这时候GetProcessesByName还能找到这个进程。不过1秒钟notepad还不能退出是不可思议的,除非它弹出保存对话框或者你是在其它什么复杂的环境中做的测试(多线程)?
要确保p1退出,你改成p1.WaitForExit();让它无限制等待试试。
------------------------
我这里是10秒,我叫别人帮我测试一下
改成p1.WaitForExit();
也是一样,
这个是我这个线程,没有编译出死循环和我那个记事本出现“1”的代码
谢谢