C#多线程设计实例 [英] C# Multi Thread Design Example

查看:189
本文介绍了C#多线程设计实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的C#/。NET。我正在开发需要多线程的桌面应用程序。我想出了下面的下面的模式为基础。我想知道如果任何人都可以指出如何让它在编码方面更好,是线程安全的,并且是有效的。



希望这有一定的道理。




 公共抽象类ThreadManagerBase 
{
//静态类变量

私有静态ThreadManagerBase例如= NULL;
私有静态BackgroundWorker的线程= NULL;
私有静态ProgressBarUIForm进度= NULL;

///<总结>
///创建这个类的一个新实例。该内部都留给了派生类搞清楚。
///此只有一个实例可以在任何时间运行。应该只有主线程并且此线程。
///< /总结>
公共抽象静态ThreadManagerBase NewInstance方法();

///<总结>
///清除的实例。
///< /总结>
公共静态无效ClearInstance()
{
实例= NULL;
}

///<总结>
///初始化一些预设的后台工作。
///显示进度条。
///< /总结>
私人抽象静态无效InitializeThread()
{
线程=新的BackgroundWorker();

thread.WorkerReportsProgress = TRUE;
thread.WorkerSupportsCancellation = TRUE;

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

thread.RunWorkerAsync();

进度=新ProgressBarUIForm();

progress.EnableCancelButton = TRUE;

progress.UserCanceled + =新EventHandlerCancelClicked(progress_UserCanceled);

progress.ShowDialog();

thread.Dispose();

线= NULL;
}

私有静态无效progress_UserCanceled(布尔userCanceled)
{
thread.CancelAsync();
}

私有静态无效thread_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
{
progress.SetProgressLevel = e.ProgressPercentage;
progress.SetProgressMessage = e.UserState.ToString();
}

私有静态无效thread_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
{
progress.Close();

进度= NULL;
}

私有静态无效thread_DoWork(对象发件人,DoWorkEventArgs E)
{
ProcessWork();
}

私人抽象静态无效ProcessWork()
{
//在这里做实际工作的东西。
//派生类将采取管道的照顾。
}
}


解决方案

您除非你想被spoonfed不需要BackgroundWorker的,正常的线程是完全可以接受的,只要你遵守规则。


I am relatively new to C#/.Net. I'm developing a desktop application that requires multi threading. I came up with the following pattern below as a base. I was wondering if anyone could point out how to make it better in terms of coding, being thread safe, and being efficient.

Hopefully this makes some sense.


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.
    }
}

解决方案

You don't need a BackgroundWorker unless you want to be spoonfed, normal threads are perfectly acceptable, as long as you follow the rules.

这篇关于C#多线程设计实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆