怎样跨线程调用控件,我这样的做法有什么问题,是错的
namespaceFiles{publicpartialclassUserControl1:UserControl{publicUserControl1(ListView...
namespace Files
{
public partial class UserControl1 : UserControl
{
public UserControl1(ListView lvi)
{
InitializeComponent();
ListVI = lvi;
}
private int proNum;
private string fileName;
private ListView listVI;
private Thread th;
delegate ListView SetTextCallback(ListView listVI);
public ListView ListVI
{
get { return listVI; }
set { listVI = value; }
}
protected int ProNum
{
get { return proNum; }
set { proNum = value; }
}
protected string FileName
{
get { return fileName; }
set { fileName = value; }
}
private void button2_Click(object sender, EventArgs e)
{
this.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
this.Visible = false;
th.Abort();
}
private void UserControl1_Load(object sender, EventArgs e)
{
timer1.Start();
th.Abort();
}
private void timer1_Tick(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(run));
th.IsBackground = true;
th.Start();
timer1.Stop();
}
public void ucrun()
{
th = new Thread(new ThreadStart(run));
th.Start();
}
public ListView SetText(ListView listVI)
{
ListView lv = new ListView();
if (this.listVI.InvokeRequired)
{
SetTextCallback call = new SetTextCallback(SetText);
lv= (ListView)this.listVI.Invoke(call, ListVI);
}
else
lv = ListVI;
return lv;
}
public void run()
{
Files files = new Files();
string newpath = null;
FileInfo fi = null;
for (int i = 0; i < ListVI.Items.Count; i++)
{
label2.Text= SetText(ListVI).Items[i].SubItems[2].Text;
}
}
}
} 展开
{
public partial class UserControl1 : UserControl
{
public UserControl1(ListView lvi)
{
InitializeComponent();
ListVI = lvi;
}
private int proNum;
private string fileName;
private ListView listVI;
private Thread th;
delegate ListView SetTextCallback(ListView listVI);
public ListView ListVI
{
get { return listVI; }
set { listVI = value; }
}
protected int ProNum
{
get { return proNum; }
set { proNum = value; }
}
protected string FileName
{
get { return fileName; }
set { fileName = value; }
}
private void button2_Click(object sender, EventArgs e)
{
this.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
this.Visible = false;
th.Abort();
}
private void UserControl1_Load(object sender, EventArgs e)
{
timer1.Start();
th.Abort();
}
private void timer1_Tick(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(run));
th.IsBackground = true;
th.Start();
timer1.Stop();
}
public void ucrun()
{
th = new Thread(new ThreadStart(run));
th.Start();
}
public ListView SetText(ListView listVI)
{
ListView lv = new ListView();
if (this.listVI.InvokeRequired)
{
SetTextCallback call = new SetTextCallback(SetText);
lv= (ListView)this.listVI.Invoke(call, ListVI);
}
else
lv = ListVI;
return lv;
}
public void run()
{
Files files = new Files();
string newpath = null;
FileInfo fi = null;
for (int i = 0; i < ListVI.Items.Count; i++)
{
label2.Text= SetText(ListVI).Items[i].SubItems[2].Text;
}
}
}
} 展开
3个回答
展开全部
最简单的办法 把控件的CheckForIllegalCrossThreadCalls属性设置为false
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用委托,然后用control.Invoke
例子:
public partial class chat_client : Form
{
delegate void AppendDelegate(string str);
AppendDelegate AppendString;
private void chat_client_Load(object sender, EventArgs e)
{
//实例化委托对象,与委托方法关联
AppendString = new AppendDelegate(AppendMethod);
}
private void AppendMethod(string str)
{
this.richtextbox1.AppendText(str);
}
//在另一个控件中使用
private void button1_Click(object sender, EventArgs e)
{
this.richtextbox1.Invoke(AppendString, new object[] { "\n请输入发送内容!"});
}
}
例子:
public partial class chat_client : Form
{
delegate void AppendDelegate(string str);
AppendDelegate AppendString;
private void chat_client_Load(object sender, EventArgs e)
{
//实例化委托对象,与委托方法关联
AppendString = new AppendDelegate(AppendMethod);
}
private void AppendMethod(string str)
{
this.richtextbox1.AppendText(str);
}
//在另一个控件中使用
private void button1_Click(object sender, EventArgs e)
{
this.richtextbox1.Invoke(AppendString, new object[] { "\n请输入发送内容!"});
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询