c#
多线程编程
- 什么是多线程,相必有些程序或者计算机基础的就会有所了解,我就不做过多赘述了,确实不知道的,可以反手百度。
- 前台线程与后台线程:一个应用程序有没有结束主要取决于其前台线程运行完了没,如果前台线程全都运行完了,那么这个程序就结束了,所有没运行完的后台线程都会被强制终止。main方法是前台线程,通过Thread创建的线程默认是前台,通过异步委托创建的默认是后台。具体用法往下看。
线程开启方法之——异步委托
- 写在前面:异步委托的方法紧支撑FrameWork c#程序 不支持Code c#程序。另外不会用委托的去这篇博客康康委托常用操作合集
- 线程开启:c#中开启多线程的方法之一,是调用委托的BeginInvoke()方法,可以当即为该委托打开一个新的线程来跑其包含的方法。
- 参数:BeginInvoke()方法有俩个默认的参数,如果当前委托是有参数的,那么委托的参数写在该方法默认参数的前面。
- 判断线程状态:BeginInvoke()方法有个IAsyncResult类型的返回值,调用该返回值的IsCompleted参数可以知道当前线程有没有被执行完。
- 得到委托返回值:如果要得到该委托的返回值,那么需要在线程执行完之后调用EndInvoke()方法该方法有一个参数,就是执行BeginInvoke()方法时返回的IAsyncResult。
- 举个例子:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SikiCSharp高级2
{
class Program
{
static int Test1(string s)
{
Console.WriteLine(s);
Thread.Sleep(20);
return 123;
}
static void Main(string[] args)
{
Func<string, int> fu = Test1;
IAsyncResult ir = fu.BeginInvoke("开启子线程", null, null);
Console.WriteLine("主线程输出");
while(ir.IsCompleted==false)
{
Console.Write("。");
}
int a = fu.EndInvoke(ir);
Console.Write(a);
}
}
}
- 第二种判断线程结束的方法:可以使用IAsyncResult对象的AsyncWaitHandle.WaitOne();方法,来让当前线程等待IAsyncResult所表示的子线程一会,传入一个int型参数表示等待多久,单位ms。返回值是一个bool型,如果在等待时间内子线程结束了,则返回一个true。否则返回false。(由于这个改变比较简短,就不举例子了)
- 第三种判断线程结束的方法:这边就用到了BeginInvoke方法的内置的俩个参数,倒数第二个参数是回调函数,自己定义一个回调函数,传入其中,线程结束后会自动调用该函数,倒数第一个参数是传给回调函数的一个参数,这个参数是什么都可以,回调函数可以通过固定的IAsyncResult参数取到这个参数。回调函数定义时写上一个IAsyncResult型的参数,线程结束调用时系统会自动将其传入。
如果要取得这个子线程的返回值的话,不妨将委托作为最后一个参数,传给回调函数,回调函数取得这个委托之后调用其EndInvoke方法来取得返回值。
举例:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SikiCSharp高级2
{
class Program
{
static int Test1(string s)
{
Console.WriteLine(s);
Thread.Sleep(200);
return 123;
}
static void Main(string[] args)
{
Func<string, int> fu = Test1;
IAsyncResult ir = fu.BeginInvoke("开启子线程", CallBack, fu);
bool a = ir.AsyncWaitHandle.WaitOne(100);
if(a)
{
Console.WriteLine("子线程输出");
}
Thread.Sleep(200);
a = ir.AsyncWaitHandle.WaitOne(100);
if (a)
{
Console.WriteLine("子线程输出2");
}
Console.WriteLine("主线程输出");
}
static void CallBack(IAsyncResult ir)
{
Func<string, int> a = ir.AsyncState as Func<string, int>;
Console.WriteLine(a.EndInvoke(ir));
}
}
}
线程开启方法之——Thread
- 写在前面:开启新的子线程这种事情,果然还是交给专业的来省事的多
- 线程开启:创建一个新的Thread对象,给其传入一个方法(这个方法可以是静态的也可以不是静态的,因此可以调用其他对象的方法,传参的事就交给对象内部自己处理了)作为参数。就定义了一个执行该方法的线程。调用Thread对象的Start();方法线程就会开跑。
举个例子:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SikiCSharp高级2
{
class Program
{
static void Main(string[] args)
{
MyThread mt = new MyThread(123, "芜湖起飞");
Thread th = new Thread(mt.go);
th.Start();
Console.WriteLine("主线程输出");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SikiCSharp高级2
{
class MyThread
{
int a = 0;
string b = "";
public MyThread(int a,string b)
{
this.a = a;
this.b = b;
}
public void go()
{
Console.WriteLine("得到的数字为:" + a);
Thread.Sleep(2000);
Console.WriteLine(b);
}
}
}
- 有关前台后台:Thread创建的默认是前台线程,可以通过将Thread对象的IsBackground属性改程true来将其改为后台线程。
- 线程排队等待:在一个线程中调用另一个Thread对象的Join方法,就是让当前线程等目标线程执行完再继续。
比如:
main()
{
Thread t = new Thread(Test1);
t.Join();
}