本来在做下载的进度条,自己的线程访问 ProgressBar时候出现错误:
错误代码
private void down_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(downfile));
th.Start();
}
private void downfile()
{
//--..这里错误ThreadStateExceptionwasunhandled- -我靠怎么这样啊
ProgressDownBar.Value= ProgressDownBar.Maximum;
}
}
- -上网搜索下,原来我自己创建的线程和ui的线程不在一起.....看的我头大.
具体资料请看http://www.yoda.arachsys.com/csharp/threads/winforms.shtml
一直接调用委托不行的必须是,MethodInvoker updateCounterDelegate = new MethodInvoker(UpdateCount);
这个委托才可以,但是好象不能传递参数,可以通过,下面的方式传递参数,而且这样写就可以访问ui线程
int currentCount;
void UpdateCount()
{
int tmpCount;
lock (stateLock)
{
tmpCount = currentCount;
}
counter.Text = tmpCount.ToString();
}
二.- -另外自定义委托的调用
delegate void StringParameterDelegate(string value);
//-----用线程直接访这个方法就可以
void UpdateStatus(string value)
{//--判断是否在ui线程中工作哦
if (InvokeRequired)
{
//--BeginInvoke 所调用的委托根本就是在 UI 线程中执行的。- -优点想递调用
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new StringParameterDelegate(UpdateStatus), new object[] { value });
return;
}
// Must be on the UI thread if we've got this far,如果在直接设置
statusIndicator.Text = value;
}
例子
test_2.Designer.cs
namespace DowonFile
{
partial class test_2
{
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Windows Form Designer generated code#region Windows Form Designer generated code
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.progressBar2 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(205, 46);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(-3, 107);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(202, 23);
this.progressBar1.TabIndex = 1;
//
// progressBar2
//
this.progressBar2.Location = new System.Drawing.Point(-3, 46);
this.progressBar2.Name = "progressBar2";
this.progressBar2.Size = new System.Drawing.Size(202, 23);
this.progressBar2.TabIndex = 2;
//
// test_2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.progressBar2);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button1);
this.Name = "test_2";
this.Text = "test_2";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.ProgressBar progressBar2;
}
}
test_2.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Threading;
namespace DowonFile
{
public partial class test_2 : Form
{
/**//// <summary>
/// 线程用到的委托
/// </summary>
/// <param name="value"></param>
delegate void StringParameterDelegate(int v);
public test_2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(new ThreadStart(ThreadJob));
//--设置为后台主线程
t1.IsBackground = true;
t1.Start();
Thread t2 = new Thread(new ThreadStart(T2));
//--设置为后台主线程
t2.IsBackground = true;
t2.Start();
}
void ThreadJob()
{
for (int i = 0; i < 100; i++)
{
UpdateStatus(i);
Thread.Sleep(100);
}
}
void UpdateStatus(int v)
{//--判断是否在ui线程中工作哦
if (InvokeRequired)
{
//--BeginInvoke 所调用的委托根本就是在 UI 线程中执行的。
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new StringParameterDelegate(UpdateStatus), new object[] { v });
return;
}
// Must be on the UI thread if we've got this far,如果在直接设置
progressBar1.Value = v;
}
void T2()
{
for (int i = 0; i < 100; i++)
{
U2(i);
Thread.Sleep(600);
}
}
void U2(int v)
{
//--判断是否在ui线程中工作哦
if (InvokeRequired)
{
//--BeginInvoke 所调用的委托根本就是在 UI 线程中执行的。
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new StringParameterDelegate(U2), new object[] { v });
return;
}
// Must be on the UI thread if we've got this far,如果在直接设置
progressBar2.Value = v;
}
}
}