using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;
namespace MyThread { class Program { private static int newTask(int ms) { Console.WriteLine("任务开始"); Thread.Sleep(ms); Random random = new Random(); int n = random.Next(10000); Console.WriteLine("任务完成"); return n; }
线程名.Resume();继续刚才被挂起的线程
td.Suspend();//挂起
td.Resume();//继续进行
td.Abort();//终止
看你自己想要什么操作了,暂停就用挂起了,如果要停止就用ABORT,然后再START
2.局部线程可以在实现后放到容器中,如List或Dictionary,根据线程标识就可以控制
td.Resume();//继续进行
td.Abort();//终止
或者像上面说的
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MyThread
{
class Program
{
private static int newTask(int ms)
{
Console.WriteLine("任务开始");
Thread.Sleep(ms);
Random random = new Random();
int n = random.Next(10000);
Console.WriteLine("任务完成");
return n;
}
private delegate int NewTaskDelegate(int ms);
static void Main(string[] args)
{
NewTaskDelegate task = newTask;
IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);
// EndInvoke方法将被阻塞2秒
int result = task.EndInvoke(asyncResult);
Console.WriteLine(result);