C#多线程设计示例

编程入门 行业动态 更新时间:2024-10-27 15:28:04
本文介绍了C#多线程设计示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我对C#/ .Net比较新。我正在开发需要多线程的桌面应用程序。我想出了下面的模式作为基地。我想知道有没有人可以指出如何在编码,线程安全和高效率方面做得更好。

希望这是有意义的。 >

public abstract class ThreadManagerBase { //静态类变量 private static ThreadManagerBase instance = null; private static BackgroundWorker thread = null; private static ProgressBarUIForm progress = null; ///< summary> ///创建此类的新实例。内部部分由派生类决定。 ///只有一个实例可以随时运行。应该只有主线程和这个线程。 ///< / summary> public abstract static ThreadManagerBase NewInstance(); ///< summary> ///清除实例。 ///< / summary> public static void ClearInstance() { instance = null; } ///< summary> ///使用某些预设初始化后台工作。 ///显示进度条。 ///< / 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() { //在这里做实际的东西。 //派生类将处理管道。 } }

解决方案

你不需要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#多线程设计示例

本文发布于:2023-05-27 19:33:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/300557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   多线程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!