资源简介
当你用visual studio开发,出现C#线程间操作无效异常时,看看我这个就知道怎样做了!
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace 线程间操作无效Study
{
public partial class Form1 : Form
{
public delegate void TestCallBack(string str);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender EventArgs e)
{
Thread mythread = new Thread(new ThreadStart(MyUnsafeMethod));
mythread.Start();
}
private void MyUnsafeMethod()
{
textBox1.Text =“this is unsafe thread!“;
}
private void button2_Click(object sender EventArgs e)
{
Thread myThread = new Thread(new ThreadStart(MySafeMethod));
myThread.Start();
}
private void MySafeMethod()
{
SetText(“this is safe thread!“);
}
private void SetText(string str)
{
if (textBox1.InvokeRequired)
{
TestCallBack d = new TestCallBack(SetText);
this.Invoke(d new object[] { str });
}
else
{
textBox1.Text = str;
}
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
.CA.... 20480 2008-04-08 16:25 C#线程间操作无效Study\线程间操作无效Study\bin\Debug\线程间操作无效Study.exe
.CA.... 22016 2008-04-08 16:25 C#线程间操作无效Study\线程间操作无效Study\bin\Debug\线程间操作无效Study.pdb
.CA.... 5632 2005-12-08 14:51 C#线程间操作无效Study\线程间操作无效Study\bin\Debug\线程间操作无效Study.vshost.exe
.CA.... 1383 2008-04-08 16:25 C#线程间操作无效Study\线程间操作无效Study\Form1.cs
.CA.... 3153 2008-04-08 16:24 C#线程间操作无效Study\线程间操作无效Study\Form1.Designer.cs
.CA.... 5814 2008-04-08 16:24 C#线程间操作无效Study\线程间操作无效Study\Form1.resx
.CA.... 842 2008-04-08 16:24 C#线程间操作无效Study\线程间操作无效Study\obj\Debug\线程间操作无效Study.csproj.GenerateResource.Cache
.CA.... 20480 2008-04-08 16:25 C#线程间操作无效Study\线程间操作无效Study\obj\Debug\线程间操作无效Study.exe
.CA.... 180 2008-04-08 16:24 C#线程间操作无效Study\线程间操作无效Study\obj\Debug\线程间操作无效Study.Form1.resources
.CA.... 22016 2008-04-08 16:25 C#线程间操作无效Study\线程间操作无效Study\obj\Debug\线程间操作无效Study.pdb
.CA.... 180 2008-04-08 16:21 C#线程间操作无效Study\线程间操作无效Study\obj\Debug\线程间操作无效Study.Properties.Resources.resources
.CA.... 401 2008-04-08 16:26 C#线程间操作无效Study\线程间操作无效Study\obj\线程间操作无效Study.csproj.FileList.txt
.CA.... 485 2008-04-08 16:18 C#线程间操作无效Study\线程间操作无效Study\Program.cs
.CA.... 1200 2008-04-08 16:18 C#线程间操作无效Study\线程间操作无效Study\Properties\AssemblyInfo.cs
.CA.... 2907 2008-04-08 16:18 C#线程间操作无效Study\线程间操作无效Study\Properties\Resources.Designer.cs
.CA.... 5612 2008-04-08 16:18 C#线程间操作无效Study\线程间操作无效Study\Properties\Resources.resx
.CA.... 1110 2008-04-08 16:18 C#线程间操作无效Study\线程间操作无效Study\Properties\Settings.Designer.cs
.CA.... 249 2008-04-08 16:18 C#线程间操作无效Study\线程间操作无效Study\Properties\Settings.settings
.CA.... 3259 2008-04-08 16:21 C#线程间操作无效Study\线程间操作无效Study\线程间操作无效Study.csproj
.CA.... 967 2008-04-08 16:18 C#线程间操作无效Study\线程间操作无效Study.sln
.CA..H. 12288 2008-04-08 16:26 C#线程间操作无效Study\线程间操作无效Study.suo
.C.D... 0 2008-05-20 15:06 C#线程间操作无效Study\线程间操作无效Study\obj\Debug\TempPE
.C.D... 0 2008-05-20 15:06 C#线程间操作无效Study\线程间操作无效Study\bin\Debug
.C.D... 0 2008-05-20 15:06 C#线程间操作无效Study\线程间操作无效Study\obj\Debug
.C.D... 0 2008-05-20 15:06 C#线程间操作无效Study\线程间操作无效Study\bin
.C.D... 0 2008-05-20 15:06 C#线程间操作无效Study\线程间操作无效Study\obj
.C.D... 0 2008-05-20 15:06 C#线程间操作无效Study\线程间操作无效Study\Properties
.C.D... 0 2008-05-20 15:06 C#线程间操作无效Study\线程间操作无效Study
.C.D... 0 2008-05-20 15:06 C#线程间操作无效Study
----------- --------- ---------- ----- ----
............此处省略2个文件信息
评论
共有 条评论