C#Thread 带参数、有返回值

/// 
/// 又要结果 要不阻塞 
/// 
/// 
/// 
/// 
public  Func<T> ThreadWithReturn<T>(Func<T> func)
{
    var t = default(T);
    var thread = new Thread(() =>
    {
        t = func.Invoke();
    });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    return () =>
    {
        thread.Join();
        return t;
    };
}
//调用此方法
public string invoking(string sWhere)
{
    var func = this.ThreadWithReturn<string>(() =>
    {
        return Count(sWhere);
    });
    return func.Invoke();
}
//工作类
public string Count(string sWhere)
{
    for(int i = 0; i < 100000; i++)
    {
         Console.WriteLine(sWhere);
    }
    return sWhere;
}



你可能感兴趣的:(c#,thread)