Thread交叉访问的问题.....

本来在做下载的进度条,自己的线程访问 ProgressBar时候出现错误:

ContractedBlock.gif ExpandedBlockStart.gif 错误代码
     private void down_Click(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
Thread th = new Thread(new ThreadStart(downfile));
            
th.Start();

         
        }

       
        
private void downfile()
ExpandedBlockStart.gifContractedBlock.gif        
{
                   
//--..这里错误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()
ExpandedBlockStart.gifContractedBlock.gif        
{
            
int tmpCount;
            
lock (stateLock)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                tmpCount 
= currentCount;
            }

            counter.Text 
= tmpCount.ToString();
        }

 

二.- -另外自定义委托的调用

delegate void StringParameterDelegate(string value);

 

 

// -----用线程直接访这个方法就可以  
void  UpdateStatus( string  value)
ExpandedBlockStart.gifContractedBlock.gif        
{//--判断是否在ui线程中工作哦 
            if (InvokeRequired)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                
//--BeginInvoke 所调用的委托根本就是在 UI 线程中执行的。- -优点想递调用
                
// We're not in the UI thread, so we need to call BeginInvoke
ExpandedSubBlockStart.gifContractedSubBlock.gif
                BeginInvoke(new StringParameterDelegate(UpdateStatus), new object[] { value });
                
return;
            }

            
// Must be on the UI thread if we've got this far,如果在直接设置
            statusIndicator.Text = value;
        }

 

 

例子

 

Thread交叉访问的问题....._第1张图片 

 

 

 

 

 

ContractedBlock.gif ExpandedBlockStart.gif test_2.Designer.cs
namespace DowonFile
ExpandedBlockStart.gifContractedBlock.gif
{
    
partial class test_2
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
        
/// Required designer variable.
        
/// 

        private System.ComponentModel.IContainer components = null;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
        
/// Clean up any resources being used.
        
/// 

        
/// true if managed resources should be disposed; otherwise, false.

        protected override void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (disposing && (components != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                components.Dispose();
            }

            
base.Dispose(disposing);
        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows Form Designer generated code#region Windows Form Designer generated code

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
        
/// Required method for Designer support - do not modify
        
/// the contents of this method with the code editor.
        
/// 

        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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(20546);
            
this.button1.Name = "button1";
            
this.button1.Size = new System.Drawing.Size(7523);
            
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(-3107);
            
this.progressBar1.Name = "progressBar1";
            
this.progressBar1.Size = new System.Drawing.Size(20223);
            
this.progressBar1.TabIndex = 1;
            
// 
            
// progressBar2
            
// 
            this.progressBar2.Location = new System.Drawing.Point(-346);
            
this.progressBar2.Name = "progressBar2";
            
this.progressBar2.Size = new System.Drawing.Size(20223);
            
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(292273);
            
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;
    }

}

 

ContractedBlock.gif ExpandedBlockStart.gif 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
ExpandedBlockStart.gifContractedBlock.gif
{
    
public partial class test_2 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
        
/// 线程用到的委托
        
/// 

        
/// 

        delegate void StringParameterDelegate(int v);

        
public test_2()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
        }


        
private void button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

          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()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
for (int i = 0; i < 100; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                UpdateStatus(i);

                Thread.Sleep(
100);
            }


        }



        
void UpdateStatus(int v)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{//--判断是否在ui线程中工作哦 
            if (InvokeRequired)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                
//--BeginInvoke 所调用的委托根本就是在 UI 线程中执行的。
                
// We're not in the UI thread, so we need to call BeginInvoke
ExpandedSubBlockStart.gifContractedSubBlock.gif
                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()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int i = 0; i < 100; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                U2(i);

                Thread.Sleep(
600);
            }

        
        }

        
void U2(int v)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//--判断是否在ui线程中工作哦 
            if (InvokeRequired)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                
//--BeginInvoke 所调用的委托根本就是在 UI 线程中执行的。
                
// We're not in the UI thread, so we need to call BeginInvoke
ExpandedSubBlockStart.gifContractedSubBlock.gif
                BeginInvoke(new StringParameterDelegate(U2), new object[] { v });
                
return;
            }

            
// Must be on the UI thread if we've got this far,如果在直接设置
            progressBar2.Value = v;
        }


    }

}

 

 

转载于:https://www.cnblogs.com/ajaxren/archive/2008/07/24/1250629.html

你可能感兴趣的:(ui)