C# Multi Thread Design Example

12,458

Solution 1

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

Solution 2

Have you looked into the Microsoft Parallel Extensions to .NET Framework 3.5? It's a pretty good library that takes a lot of the work out of threading.

There are also a lot of articles on MSDN about threading patterns that you should research too. Threading can get really complicated really fast. It's nice to have someone else to have though of all the important stuff that can go wrong, and simplify it down to a library or a pattern. Of course, there's danger in that too if you don't understand the gotchas of any particular solution. So, make sure you research well whatever solution you choose.

Share:
12,458
Brownman98
Author by

Brownman98

Updated on June 04, 2022

Comments

  • Brownman98
    Brownman98 almost 2 years

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