. Net2.0 new thread ParameterizedThreadStart & BackgroundWorker

Source: Internet
Author: User

What if you want to input a variable for a thread?

ThreadStart does not support methods with parameters. Therefore, you cannot use Thread to start a method with parameters ..

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!

However. in Net1.0, You can implement it through asynchronous call of Delegate. now in. in Net2.0, ParameterizedThreadStart is provided. it differs from ThreadStart in that it can have an object-type parameter. that is to say, you can use the Thread class to start a Thread and input parameters, which is very similar to Java and has good new functions.

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 );
}
}
}

There is also a new class of BackgroundWorker, which can be used to start the background thread and call the main thread method in time after the background computing ends.
A common application is to load data in the DataGrid. Because loading DataSet from the database is time-consuming, you can use
BackgroundWorker for loading. After the DataSet is constructed, it is immediately bound to the DataGrid. in fact, this function can also be implemented through asynchronous call of Delegate, but it is more convenient to use 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 ();

Take a look at C #3.0
On the PDC, Anders Hejlsberg will introduce future Language Improvement Directions.

C #: Future Directions in Language Innovation from Anders Hejlsberg
Join Anders Hejlsberg, Distinguished Engineer and chief impact ect 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 strongrong typed, natural way. session Level (s): 300 Track (s): Tools & ages

You need to pay attention to the following aspects in the future (many aspects are related to dynamic languages. I suggest you learn Python Ruby. Who has any good guidance ?) :

  1. Extension methods
  2. Lambda expressions
  3. Type inference and implicit types
  4. Anonymous types
  5. Expression Trees

Who has experience to introduce it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.