我的FORM上有个LABEL WT;由于有2工作个线程对WT进行操作,主要是WT.TEXT,WT.backcolor等属性进行操作。现在导致有时WT.TEXT改变后,WT在窗体上看不到图形上的变化。例如wt.text="A"在窗体上应该能看到A的,可是WT没有变化,WT.TEXT的属性读出来确实是"A",是有变化的。所以我猜想不知道什么原因导致LABEL WT不能正确的进行自我绘制。请大家帮帮忙看有什么解决办法。这问题很郁闷,我在公司的所有计算机上都模拟不出来,但客户那里确有这样的情况。急死了。备注:是在.NET2.0下开发的由于2.0要求线程安全所以我用CheckForIllegalCrossThreadCalls = false;来实现异步线程访问窗体允许。
delegate d;//你对l要进行的操作的delegate。
object data[];//要传递的参数
l.Invoke(d,data);这么试试看。
using System.Drawing;
using System.Windows.Forms;
using System.Threading; public class MyFormControl : Form
{
public delegate void AddListItem(String myString);
public AddListItem myDelegate;
private Button myButton;
private Thread myThread;
private ListBox myListBox;
public MyFormControl()
{
myButton = new Button();
myListBox = new ListBox();
myButton.Location = new Point(72, 160);
myButton.Size = new Size(152, 32);
myButton.TabIndex = 1;
myButton.Text = "Add items in list box";
myButton.Click += new EventHandler(Button_Click);
myListBox.Location = new Point(48, 32);
myListBox.Name = "myListBox";
myListBox.Size = new Size(200, 95);
myListBox.TabIndex = 2;
ClientSize = new Size(292, 273);
Controls.AddRange(new Control[] {myListBox,myButton});
Text = " 'Control_Invoke' example ";
myDelegate = new AddListItem(AddListItemMethod);
}
static void Main()
{
MyFormControl myForm = new MyFormControl();
myForm.ShowDialog();
}
public void AddListItemMethod(String myString)
{
myListBox.Items.Add(myString);
}
private void Button_Click(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(ThreadFunction));
myThread.Start();
}
private void ThreadFunction()
{
MyThreadClass myThreadClassObject = new MyThreadClass(this);
myThreadClassObject.Run();
}
}
public class MyThreadClass
{
MyFormControl myFormControl1;
public MyThreadClass(MyFormControl myForm)
{
myFormControl1 = myForm;
}
String myString; public void Run()
{
for (int i = 1; i <= 5; i++)
{
myString = "Step number " + i.ToString() + " executed";
Thread.Sleep(400);
// Execute the specified delegate on the thread that owns
// 'myFormControl1' control's underlying window handle with
// the specified list of arguments.
myFormControl1.Invoke(myFormControl1.myDelegate,
new Object[] {myString});
}
}
}
Application.DoEvent(); //<----
2. CheckForIllegalCrossThreadCalls = false; is bad.
Because you do have multiple threads that access the same UI resource.
I would suggest that you switch to InvokeRequired and Invoke();
private void btnStart_Click(object sender, System.EventArgs e)
{
Thread th = new Thread(new ThreadStart(WorkThreadMethod));
th.Name = "TH" + i;
th.Start();
}// 代理
public delegate void UpdateTextCallback(string threadName, DateTime time);// 执行方法
private void WorkThreadMethod()
{
Thread.Sleep(10000);
// Invoke
txtTime.Invoke(new UpdateTextCallback(this.UpdateText), new object[]{Thread.CurrentThread.Name, DateTime.Now});
}private void UpdateText(string threadName, DateTime)
{
txtTime.Text = threadName + time.ToString();
}或者:
在构造函数里加上这句,就可以不用Invoke,加上下面这句后,就像平常的做法就可以了.
CheckForIllegalCrossThreadCalls = false;
如:
public class A:Form
{
public A()
{
CheckForIllegalCrossThreadCalls = false; }
} 要注意的是,这个A类有继承form或cotrol后,才可以正常使用CheckForIllegalCrossThreadCalls = false;