C# 跨线程更新 UI
在 Winforms
中, 所有的控件都包含 InvokeRequired
属性, 如果我们要更新UI,通过它我们可以判断是否需要调用 [Begin]Invoke
.
delegate void SetTextCallback(string text);//定义一个委托类型
public void SetText(string text)
{
if (InvokeRequired)
{
var d = new SetTextCallback(SetText);
this.textBox1.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
直接使用SetText
即可
C# winform 启动外部程序
- 方法一:通过内置 Process 方式打开程序
Process m_Process = null;
m_Process = new Process();
m_Process.StartInfo.FileName = @"C:\test.exe";
m_Process.Start();
- 方法二:shell32.dll 方法
//class里面放入这段代码
[DllImport("shell32.dll")]
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
//需要打开的地方插入此段代码
ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder("test.exe"), new StringBuilder(""), new StringBuilder(@"C:\文件夹名"), 1);