C# 多线程设计实例

标签 c# multithreading design-patterns

我对 C#/.Net 比较陌生。我正在开发一个需要多线程的桌面应用程序。我想出了下面的模式作为基础。我想知道是否有人可以指出如何在编码、线程安全和高效方面让它变得更好。

希望这是有道理的。


public abstract class ThreadManagerBase
{
    // static class variables

    private static ThreadManagerBase instance = null;
    private static BackgroundWorker thread = null;
    private static ProgressBarUIForm progress = null;

    /// <summary>
    /// Create a new instance of this class. The internals are left to the derived class to figure out.
    /// Only one instance of this can run at any time. There should only be the main thread and this thread.
    /// </summary>
    public abstract static ThreadManagerBase NewInstance();

    /// <summary>
    /// Clears the instance.
    /// </summary>
    public static void ClearInstance()
    {
        instance = null;
    }

    /// <summary>
    /// Initializes the background worker with some presets.
    /// Displays progress bar.
    /// </summary>
    private abstract static void InitializeThread()
    {
        thread = new BackgroundWorker();

        thread.WorkerReportsProgress = true;
        thread.WorkerSupportsCancellation = true;

        thread.DoWork += new DoWorkEventHandler(thread_DoWork);
        thread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(thread_RunWorkerCompleted);
        thread.ProgressChanged += new ProgressChangedEventHandler(thread_ProgressChanged);

        thread.RunWorkerAsync();

        progress = new ProgressBarUIForm();

        progress.EnableCancelButton = true;

        progress.UserCanceled += new EventHandlerCancelClicked(progress_UserCanceled);

        progress.ShowDialog();

        thread.Dispose();

        thread = null;
    }

    private static void progress_UserCanceled(bool userCanceled)
    {
        thread.CancelAsync();
    }

    private static void thread_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progress.SetProgressLevel = e.ProgressPercentage;
        progress.SetProgressMessage = e.UserState.ToString();
    }

    private static void thread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progress.Close();

        progress = null;
    }

    private static void thread_DoWork(object sender, DoWorkEventArgs e)
    {
        ProcessWork();
    }

    private abstract static void ProcessWork()
    {
        // do actuall stuff here.
        // the derived classes will take care of the plumbing.
    }
}

最佳答案

你看过Microsoft Parallel Extensions to .NET Framework 3.5吗? ?这是一个相当不错的库,可以完成线程处理中的大量工作。

MSDN 上也有很多关于线程模式的文章,您也应该研究一下。线程可以变得非常复杂非常快。很高兴让其他人了解所有可能出错的重要内容,并将其简化为库或模式。当然,如果您不了解任何特定解决方案的陷阱,那也存在危险。因此,无论选择哪种解决方案,请务必仔细研究。

关于C# 多线程设计实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/596991/

相关文章:

c# - 如何在 C# 中更改 UWP Apps 中按钮的背景颜色?

c# - 创建/更新/删除分层数据的正确方法

c# - 向 3rd 方开发人员公开我的核心 API 的关键类/方法的最佳方式是什么?

oracle - 了解 Table 和 Transaction API 之间的区别

c# - 如何从继承的 FromBody 模型中获取正确的类型?

c# - 如何在 C# 中访问特定日期的 sql server 2008 表中的数据?

java - 如何同步不同的java程序访问公共(public)资源

java - 分布式系统中的警报/消息转发

java - 如何在没有 IllegalMonitorStateException 的情况下在 Java 中使用等待和通知?

java - AtomicInteger 等原子包类如何工作