我怎样才能纠正错误和QUOT;从一个线程比它在"?创建线程的其他访问 [英] How can I correct the error "accessed from a thread other than the thread it was created on"?
本文介绍了我怎样才能纠正错误和QUOT;从一个线程比它在"?创建线程的其他访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这下code给我下面的错误。我想我需要InvokeRequired。但我不明白,我怎么用?
跨线程操作无效:控制'listBox1中'从一个线程比它创建于其他线程访问
在code:
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Text;
使用System.Windows.Forms的;
使用的System.Threading;
命名空间的WindowsApplication1
{
公共部分类Form1中:形态
{
公共Form1中()
{
的InitializeComponent();
}
保护静态数据集的数据集= NULL;
私人无效Form1_Load的(对象发件人,EventArgs的)
{
}
私人无效timer1_Tick(对象发件人,EventArgs的)
{
SimulationFrameWork.MCSDirector运行=新SimulationFrameWork.MCSDirector();
的DataSet ds为run.Get();
如果(ds.Tables [0] .Rows.Count大于0)
{
的for(int i = 0; I< ds.Tables [0] .Rows.Count;我++)
{
如果(ds.Tables [0] .Rows [I] [结果]。的ToString()==0)
{
数据集= run.Get(int.Parse(ds.Tables [0] .Rows [I] [标识]的ToString())。);
WorkerObject工人=
新WorkerObject(
int.Parse(dataset.Tables [0] .Rows [I] [标识]。的ToString()),
int.Parse(dataset.Tables [0] .Rows [I] [迭代]。的ToString()),
listBox1中,定时器1);
线程线程1 =新主题(新的ThreadStart(worker.start));
thread1.Start();
}
}
}
}
}
公共类WorkerObject
{
私人诠释身份证;
私人诠释n最大;
私人列表框List1中;
私人System.Windows.Forms.Timer定时器1;
公共WorkerObject(INT _id,诠释_nmax,列表框_list1,
System.Windows.Forms.Timer _timer1)
{
ID = _id;
n最大= _nmax;
List1中= _list1;
定时器1 = _timer1;
}
公共无效启动()
{
timer1.Stop();
INT I,idaire,J;
双PI = 0.0,X,Y;
随机RND =新的随机();
对于(i = 0; I< 100;我++)
{
idaire = 0;
为(J = 0; J< n最大; J ++)
{
X = rnd.Next(1 10000)/(双)10000;
Y = rnd.Next(1 10000)/(双)10000;
如果(Math.Pow(X,2)+ Math.Pow(γ,2)&其中; = 1.0)
idaire + = 1;
}
PI = 4 *(双)idaire /(双)n最大;
n最大* = 10;
list1.Items.Add(
Iterasyon:+
nmax.ToString()+
----->中+
pi.ToString()+
\ N);
System.Threading.Thread.Sleep(100);
}
SimulationFrameWork.MCSDirector运行=新SimulationFrameWork.MCSDirector();
run.Update(标识,PI);
list1.Items.Add(\ñ\ñislem比蒂......);
}
}
}
解决方案
只是封装添加文本列表框,以另一种方法:
私人无效timer1_Tick(对象发件人,EventArgs的)
{
// ...
AddTextToListBox(\ñ\ñişlem比蒂......);
}
私人无效AddTextToListBox(文本字符串)
{
如果(list1.InvokeRequired)
{
list1.Invoke(新MethodInvoker(AddTextToListBox),新的对象[] {文本});
返回;
}
list1.Items.Add(文本);
}
This following code gives me the error below . I think I need "InvokeRequired" . But I don't understand how can I use?
Cross-thread operation not valid: Control 'listBox1' accessed from a thread other than the thread it was created on.
The code:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected static DataSet dataset = null;
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
SimulationFrameWork.MCSDirector run = new SimulationFrameWork.MCSDirector();
DataSet ds = run.Get();
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i]["result"].ToString() == "0")
{
dataset = run.Get(int.Parse(ds.Tables[0].Rows[i]["ID"].ToString()));
WorkerObject worker =
new WorkerObject(
int.Parse(dataset.Tables[0].Rows[i]["ID"].ToString()),
int.Parse(dataset.Tables[0].Rows[i]["Iteration"].ToString()),
listBox1, timer1);
Thread thread1 = new Thread(new ThreadStart(worker.start));
thread1.Start();
}
}
}
}
}
public class WorkerObject
{
private int id;
private int nmax;
private ListBox list1;
private System.Windows.Forms.Timer timer1;
public WorkerObject(int _id, int _nmax, ListBox _list1,
System.Windows.Forms.Timer _timer1)
{
id = _id;
nmax = _nmax;
list1 = _list1;
timer1 = _timer1;
}
public void start()
{
timer1.Stop();
int i, idaire, j;
double pi = 0.0, x, y;
Random rnd = new Random();
for (i = 0; i < 100; i++)
{
idaire = 0;
for (j = 0; j < nmax; j++)
{
x = rnd.Next(1, 10000) / (double)10000;
y = rnd.Next(1, 10000) / (double)10000;
if (Math.Pow(x, 2) + Math.Pow(y, 2) <= 1.0)
idaire += 1;
}
pi = 4 * (double)idaire / (double)nmax;
nmax *= 10;
list1.Items.Add(
"Iterasyon:" +
nmax.ToString() +
" ----->" +
pi.ToString() +
"\n");
System.Threading.Thread.Sleep(100);
}
SimulationFrameWork.MCSDirector run = new SimulationFrameWork.MCSDirector();
run.Update(id, pi);
list1.Items.Add("\n\n islem bitti...");
}
}
}
解决方案
Just encapsulate adding the text to the listbox to another method:
private void timer1_Tick(object sender, EventArgs e)
{
// ...
AddTextToListBox("\n\n işlem bitti...");
}
private void AddTextToListBox(string text)
{
if(list1.InvokeRequired)
{
list1.Invoke(new MethodInvoker(AddTextToListBox), new object[] { text });
return;
}
list1.Items.Add(text);
}
这篇关于我怎样才能纠正错误和QUOT;从一个线程比它在&QUOT;?创建线程的其他访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文