1、System.Threading 的Thread、ThreadStart类。使用代理对象,无参数,如:
Thread dlThread;
dlThread = new Thread(new ThreadStart(this.ThreadFuncDownload));
dlThread.Start();
2、System.Threading 的Thread、ParameterizedThreadStart类。使用代理对象,带参数,如:
List<Dish> recomList;
Thread FoodThread;
FoodThread = new Thread(new ParameterizedThreadStart(getDishFoodShow));
FoodThread.Start(recomList);
private void getDishFoodShow(object anyObj)
{
List<Dish> dishListParam = (List<Dish>)anyObj;
}
3、委托对象的异步调用方法:BeginInvoke(),BeginInvoke方法附带了AsyncCallback类型的回调函数委托以及object类型的参数。
然后可以在AsyncCallback类型的回调函数中使用EndInvoke方法来得到异步方法的返回值.
private delegate void GetDishListDeleg(List<Dish> listParam);
GetDishListDeleg recomListDelg;
recomListDelg = new GetDishListDeleg(getDishFoodShow);
recomListDelg.BeginInvoke(recomList, null, null);
4、线程池,使用ThreadPool.QueueUserWorkItem(new WaitCallback(method))来向线程池添加一个新线程
static void Main()
{
WaitCallback callBack;
callBack = new WaitCallback(PooledFunc);
ThreadPool.QueueUserWorkItem(callBack,
"Is there any screw left?");
ThreadPool.QueueUserWorkItem(callBack,
"How much is a 40W bulb?");
ThreadPool.QueueUserWorkItem(callBack,
"Decrease stock of monkey wrench");
Console.ReadLine();
}
static void PooledFunc(object state)
{
Console.WriteLine("Processing request '{0}'", (string)state);
}
5、System.Timers.Timer定时器类。可以使用System.Timers.Timer定时器类来实现在新线程中执行耗时操作。System.Timers.Timer定时器的定时事件的响应函数并不是在调用定时器Start方法的线程中去执行。
6、BackgroundWorker组件。
DoWork 、ProgressChanged 和 RunWorkerCompleted
在程序中调用RunWorkerAsync方法则会启动DoWork事件的事件处理,当在事件处理过程中,调用 ReportProgress方法则会启动ProgressChanged事件的事件处理,而当DoWork事件处理完成时,则会触发RunWorkerCompleted事件。
7、工作线程/后台线程回到ui线程/操作UI元素
其一:
private delegate void ShowDishListDeleg(List<Dish> listParam, int indexParam, Bitmap snapBitmapParam);
ShowDishListDeleg showListDelg = new ShowDishListDeleg(ShowDishList);
this.Dispatcher.BeginInvoke(showListDelg,dishListParam, i, snapBitmap);
private void ShowDishList(List<Dish> listParam, int indexParam, Bitmap snapBitmapParam)
{
}
其二:
public delegate void DownOver();
public void NetServerOver()
{
this.InitOperateClass();
System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new DownOver(InitFunc));
}
public void InitFunc()
{
this.InitOperateClass();
//把滚动界面添加进去
animRecomPage = new UserControlShowAnimRecom();
AnimGrid.Children.Add(animRecomPage);
}