.Net2.0 的新线程 ParameterizedThreadStart &BackgroundWorker

 

如果你想为一个线程传入变量你怎么办?

ThreadStart可不支持带参数的方法.所以你无法使用Thread来启动一个带参数的方法..

ThreadStart myThreadDelegate = new ThreadStart(ThreadMethod);
//public delegate void ThreadStart();  u can't pass a Parameter
            Thread myThread = new Thread(myThreadDelegate);
            myThread.Start();  //myThread.Start(o); Wrong! 

不过在.Net1.0下,你可以通过Delegate的异步调用来实现.现在在.Net2.0下提供了ParameterizedThreadStart 这么一个Delegate.它和ThreadStart 的不同就在于可以拥有一个object类型的参数.也就是说你可以通过它来使用Thread类以启动一个线程并传入参数, 和Java很象了,不错的新功能.

using System;
using System.Threading;
namespace ParameterizedThreadStartTest
{
class Program
{
static void Main(string[] args)
{

            ParameterizedThreadStart myParameterizedThreadDelegate = new ParameterizedThreadStart(ThreadMethod);
            Thread myThread = new Thread(myParameterizedThreadDelegate);
object o = "hello";
            myThread.Start(o);

        }

private static void ThreadMethod(object o)
{
string str = o as string;
            Console.WriteLine(str);
        }
    }
}

还有一个新增的类BackgroundWorker,可以用于启动后台线程,并在后台计算结束后及时调用主线程的方法.
一个常见的应用就是在DataGrid中载入数据的时候.因为从数据库中载入DataSet比较耗时, 所以你可以使用
BackgroundWorker来进行载入, 当DataSet构造好后就立即绑定上DataGrid. 其实该功能同样可以通过Delegate的异步调用实现不过BackgroundWorker用起来更方便一些.

//1. Instantiate a BackgroundWorker instance:
BackgroundWorker myDataWorker = new BackgroundWorker(); 

//2. Setup a DoWork delegate that does the work that you want to be done on the background thread.  

myDataWorker.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs workerEventArgs) 
{
                                                    workerEventArgs.Result = new XXXDAL().GetData();
                                                }
                                                );

//3. Setup a RunWorkerCompleted delegate that handles updating your UI with the data recieved on the background thread.
myDataWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object o, RunWorkerCompletedEventArgs workerEventArgs) 
{
                                                            DataSet data = (DataSet) workerEventArgs.Result;
this.dataGrid.DataSource = data;
                                                        }
                                                        );

//4.Run your worker by calling the RunWorkerAsync() method on your BackgroundWorker instance.
myDataWorker.RunWorkerAsync();

顺便关注一下C#3.0
PDC上 Anders Hejlsberg将介绍未来的语言改进方向.

          C#: Future Directions in Language Innovation from Anders Hejlsberg

Join Anders Hejlsberg, Distinguished Engineer and chief architect of the C# language, for an in-depth walkthrough of the new language features in C# 3.0. Understand how features like extension methods, lambda expressions, type inference, and anonymous types make it possible to create powerful APIs for expressing queries and interacting with objects, XML, and databases in a strongly typed, natural way.

Session Level(s): 300

Track(s): Tools & Languages

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值