114 lines
3.2 KiB
C#
114 lines
3.2 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Common.DbExtends.Extends;
|
|||
|
|
|||
|
namespace Server.Services
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 控制多线程任务等待帮助服务
|
|||
|
/// </summary>
|
|||
|
public class WaitTaskThreadService
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 等待
|
|||
|
/// </summary>
|
|||
|
private List<ManualResetEvent> manualEvents = new List<ManualResetEvent>();
|
|||
|
/// <summary>
|
|||
|
/// 启动任务
|
|||
|
/// </summary>
|
|||
|
/// <param name="name"></param>
|
|||
|
/// <param name="action"></param>
|
|||
|
/// <param name="stateObject"></param>
|
|||
|
public void StartThread(string name, Action<object> action, object stateObject)
|
|||
|
{
|
|||
|
var mre = new ManualResetEvent(false);
|
|||
|
manualEvents.Add(mre);
|
|||
|
var th = new Thread(o =>
|
|||
|
{
|
|||
|
var result = o as WaitTaskThreadObject;
|
|||
|
try
|
|||
|
{
|
|||
|
result?.Action?.Invoke(result.StateObject);
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
Client.SingleClient.Db.OnLog("处理任务" + name, e);
|
|||
|
throw e;
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
result?.ManualResetEvent.Set();
|
|||
|
}
|
|||
|
});
|
|||
|
th.Name = name;
|
|||
|
th.Start(new WaitTaskThreadObject()
|
|||
|
{
|
|||
|
Name = name,
|
|||
|
StateObject = stateObject,
|
|||
|
Action = action,
|
|||
|
ManualResetEvent = mre
|
|||
|
});
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 启动任务
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T"></typeparam>
|
|||
|
/// <param name="name"></param>
|
|||
|
/// <param name="action"></param>
|
|||
|
/// <param name="stateObject"></param>
|
|||
|
public void StartThread<T>(string name, Action<T> action, T stateObject = default)
|
|||
|
{
|
|||
|
StartThread(name, (o) =>
|
|||
|
{
|
|||
|
action?.Invoke((T)o);
|
|||
|
}, (object)stateObject);
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 启动任务
|
|||
|
/// </summary>
|
|||
|
/// <param name="name"></param>
|
|||
|
/// <param name="action"></param>
|
|||
|
public void StartThread(string name, Action action)
|
|||
|
{
|
|||
|
StartThread(name, (o) =>
|
|||
|
{
|
|||
|
action?.Invoke();
|
|||
|
}, null);
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 等待全部任务完成
|
|||
|
/// </summary>
|
|||
|
public void WaitAll()
|
|||
|
{
|
|||
|
WaitHandle.WaitAll(manualEvents.ToArray());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 线程启动参数数据
|
|||
|
/// </summary>
|
|||
|
public class WaitTaskThreadObject
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 线程名称,(调试用)
|
|||
|
/// </summary>
|
|||
|
public string Name { get; set; }
|
|||
|
/// <summary>
|
|||
|
/// 线程对象
|
|||
|
/// </summary>
|
|||
|
public object StateObject { get; set; }
|
|||
|
/// <summary>
|
|||
|
/// 执行具体方法
|
|||
|
/// </summary>
|
|||
|
public Action<object> Action { get; set; }
|
|||
|
/// <summary>
|
|||
|
/// 线程同步事件
|
|||
|
/// </summary>
|
|||
|
public ManualResetEvent ManualResetEvent { get; set; }
|
|||
|
}
|
|||
|
}
|