C# 线程池中的线程都执行后,执行主线程中输出

没啥说的,直接上代码。

 

using System;
using System.IO;
using System.Security.Permissions;
using System.Threading;

public enum DatabaseType
{
    Business,
    System
}

class DataTransValue
{
    public DatabaseType DatabaseType;
    public string TableName = "";
    public string Type = "";
    public AutoResetEvent MainEvent;

    public DataTransValue(DatabaseType databaseType, string tableName, string type)
    {
        DatabaseType = databaseType;
        TableName = tableName  ;
        Type = type ;
    }
}


class Test
{

 

    public void DataTrans(object value)
    {
        Thread t = Thread.CurrentThread;
        DataTransValue selfDataTrans = (DataTransValue)value;
        Console.WriteLine("ThreadCode{0}/t databaseType{1}/t tableName{2}/t type{3}/t",
            t.GetHashCode(), selfDataTrans.DatabaseType.ToString(), selfDataTrans.TableName, selfDataTrans.Type);
        Thread.Sleep(100);
        selfDataTrans.MainEvent.Set();
    }

    static void Main()
    {
        AutoResetEvent[] MainEvent = new AutoResetEvent[50];      
        Test test = new Test();
        for (int i = 0; i < 50; i++)
        {
            DataTransValue tt = new DataTransValue(DatabaseType.Business, "TestTablename", "TestType");
            MainEvent[i] = new AutoResetEvent(false);
            tt.MainEvent = MainEvent[i];
            ThreadPool.QueueUserWorkItem(new WaitCallback(test.DataTrans), tt);
        }
        WaitHandle.WaitAll(MainEvent);
        Console.WriteLine("完成");
        Console.Read();
    }

 

}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页