0
我有一些泛型函數在後臺線程中運行方法。在正常的線程同步問題之外,這裏是否存在任何危險?在另一個線程中運行函數的C#泛型
public static void ThreadRunReturn<TReturn, TArgument>(Func<TArgument, TReturn> func, TArgument arg, bool background = true)
{
Thread th = new Thread(unused => func(arg));
th.IsBackground = background;
th.Start(th);
}
public static void ThreadRunReturn<TReturn, TArgument1, TArgument2>(Func<TArgument1, TArgument2, TReturn> func, TArgument1 arg1, TArgument2 arg2, bool background = true)
{
Thread th = new Thread(unused => func(arg1, arg2));
th.IsBackground = background;
th.Start(th);
}
public static void ThreadRun<TArgument>(Action<TArgument> action, TArgument arg, bool background = true)
{
Thread th = new Thread(unused => action(arg));
th.IsBackground = background;
th.Start(th);
}
public static void ThreadRun<TArgument1, TArgument2>(Action<TArgument1, TArgument2> action, TArgument1 arg1, TArgument2 arg2, bool background = true)
{
Thread th = new Thread(unused => action(arg1, arg2));
th.IsBackground = background;
th.Start(th);
}
一切都需要公開嗎? – KingCronus 2012-08-15 14:48:28
是不是使用TPL更容易? 'Task.Factory.StartNew' – 2012-08-15 14:55:50
@ L.B我會研究Task.Factory.StartNew,但快速瀏覽似乎並不那麼簡單。我希望能夠執行以下操作:MyClass.ThreadRunReturn(RunSShCommand,「reboot」); – Marinara54uc3 2012-08-15 15:07:51