0

我有一些用于在后台线程中运行方法的通用函数。除了正常的线程同步问题之外,这里是否存在任何危险?

    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);
    }
4

1 回答 1

1

后台线程不会使应用程序保持活动状态。如果您有长时间运行的后台进程正在运行并且您的应用程序终止,它不会等待这些后台线程完成 - 它会杀死它们。只是要记住的事情。

于 2012-08-15T14:54:24.003 回答