解决方案 »
- C# WinForm向Http提交数据的问题
- 如何正确地使用异常处理?
- c# DATASET转换XML中的数据库格式DATETIME在XML的保存
- 线程问题:如何保证多个控件线程正确操作?
- 纯手动写的打印代码。(高手近来指点下,将不胜感激!)
- 向excel写数据
- 在用 C#写c/s结构的程序中,如何实现从网站上下载文件?
- 为什么我的窗体按Ctrl-C就关闭????
- 菜鸟问题:如何在C#里获得当前路径?
- 当我使用[assambly: AssamblyKeyFile("test.snk")] 时编译器说使用重复属性,这是什么原因
- c#外行问个问题
- 一个XslCompiledTransform对象,如何序列化?
return
break有什么用呢?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
delegate void HandleNotePadDelegate();
delegate void EnableTheButton1Delegate(); public partial class Form1 : Form
{
private HandleNotePadDelegate h;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
try
{
this.button1.Enabled = false;
h = new HandleNotePadDelegate(this.HandleNotePad);
h.BeginInvoke(this.EndAyncCall, null);
}
catch (Exception ex)
{
throw ex;
}
} private void HandleNotePad()
{
//结束某些进程
foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
{
if (thisproc.ProcessName.Equals("notepad"))
{
thisproc.CloseMainWindow();
thisproc.WaitForExit(1000);
thisproc.Close(); System.Threading.Thread.Sleep(2000);
}
}
System.Diagnostics.Process.Start("notepad");
} private void EndAyncCall(System.IAsyncResult ar)
{
try
{
h.EndInvoke(ar);
}
catch{ }
finally
{
EnableTheButton1Delegate h2 = new EnableTheButton1Delegate(this.EnableButton1);
this.Invoke(h2);
}
} private void EnableButton1()
{
this.button1.Enabled = true; } }}
请lz参考一下吧。楼主贴出的代码完全是同步调用。禁用button1、关闭进程和打开进程三个动作都在UI线程里执行的话,当其中某个动作需要较长时间的时候就容易导致窗口停滞。上面的代码大致是按照如下思路来做的——
(1)把关闭进程和打开进程两个可能耗时较长的动作放到一个辅助线程里去异步执行:
h = new HandleNotePadDelegate(this.HandleNotePad);
h.BeginInvoke(this.EndAyncCall, null); (2)开启异步操作之前先行禁止button1:this.button1.Enabled = false; (3)异步操作结束时将回调EndAyncCall方法,在该方法里解除针对button1的禁用:
EnableTheButton1Delegate h2 = new EnableTheButton1Delegate(this.EnableButton1);
this.Invoke(h2);
foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
{
if (!thisproc.ProcessName.Equals("notepad"))
{
System.Diagnostics.Process.Start("notepad");
button1.Enabled = true;
break;
}
}lz上面的这段代码有点看不懂。
既然只想保留一个notepad进程,为何要循环操作呢?
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace L2Restart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} static int i = 0; private void button1_Click(object sender, EventArgs e)
{
try
{
foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
{
if (thisproc.ProcessName.Equals("taskmgr"))
{
i++;
}
}
if (i > 0)
{
foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
{
if (thisproc.ProcessName.Equals("taskmgr"))
{
i--;
if (!thisproc.CloseMainWindow())
thisproc.Kill();
thisproc.WaitForExit(1000);
thisproc.Close(); System.Threading.Thread.Sleep(2000);
}
}
if (i==0)
System.Diagnostics.Process.Start("taskmgr");
}
else
System.Diagnostics.Process.Start("taskmgr");
}
catch (Exception ex)
{
throw ex;
} }
}
}谢谢楼上的,我改成这样啦,但是连续快速点击还是有点问题,我再测试下你的方案!