• .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

    以后需要关注以下方面(很多方面和动态语言相关,推荐大家学习一下Python Ruby,谁有什么好的指导?):
    1. Extension methods
    2. Lambda expressions
    3. Type inference and implicit types
    4. Anonymous types
    5. Expression Trees
    有谁有经验的来介绍一下.
  • 相关阅读:
    segnet 编译与测试
    ubuntu(已经配置了python2+opencv)简易配置python3的opencv:
    OCR光学字符识别--STN-OCR 测试
    阿里云图形界面
    win10+UEFI下u盘安装ubuntu16.04
    ubuntu16.04+cuda8.0+cudnn5.0+caffe
    Angular 组件通讯、生命周期钩子 小结
    Angular 路由⑦要素
    关于克隆gitlab项目的一些 问题列表
    RxJS学习笔记
  • 原文地址:https://www.cnblogs.com/idior/p/199946.html
Copyright © 2020-2023  润新知