JoeFox
JoeFox

Reputation: 1081

Making a program with singletons multithreaded C#

I have written a program in C#. Now I finished all the functionality and it works. But only running with one thread. I'm doing a lot of calculation and sometimes loading about 300 MB or more of measurement files into the application.

I now want to make the program multithreaded because the user experiance is really bad in times of intense processing or i/o operations.

What is the best way to refactor the program, so that it can be made multithreaded without too much affort? I know this is stuff I should have thougth before. But I havn't.

I used the singleton pattern for about 3 big and important modules which are involved in nearly every other functionality of the program.

I used a more or less clean MVC (Model View Control) architecture. So I wonder if it is maybe possible to let the User Interface run in one thread and the rest of the application in another.

If not, loading and parsing 300MB, creating objects will take about 3 minutes to finish. In this time the user gets no response from the GUI. :/

UPDATE: My singletons are used as a kind of storage. One singleton saves the objects of the parsed measurement files, while the other singleton saves the result. I have different calculations, which use the same measurementfiles and creating results which they want to save using the other singleton. This is one problem.

The second is to keep the guy responsive to user action or at least avoid this warning that the window is not responding.

Thank you all for all advices. I will try them. Sorry for the late answere.

Upvotes: 2

Views: 1994

Answers (3)

jgauffin
jgauffin

Reputation: 101140

The easiest way is to move all calculations to one separate thread and update the GUI using Invoke/InvokeRequired.

public partial class MyForm : Form
{
    Thread _workerThread;

    public MyForm()
    {
        _workerThread = new Thread(Calculate);
    }

    public void StartCalc()
    {
        _workerThread.Start();
    }

    public void Calculate()
    {
        //call singleton here


    }

    // true if user are allowed to change calc settings
    public bool CanUpdateSettings
    {
        get { return !_workerThread.IsAlive; } }
    }
}

In this way you have get a response GUI while the calculations are running.

The application will be thread safe as long as you don't allow the user to make changes during a running calculation.

Using several threads for doing the calculations is a much more complex story which we need more information for to give you a proper answer.

Upvotes: 1

dpurrington
dpurrington

Reputation: 1533

Generally, I avoid the singleton pattern because it creates a lot of issues down the road, particularly in testing. However, there is a fairly simple solution to making this work for multiple threads, if what you want is a singleton per thread. Put your singleton reference in a field (not a property) and decorate it with the ThreadStaticAttribute:

public class MySingleton
{
   [ThreadStatic]
   private static MySingletonClass _instance = new MySingletonClass();
   public static MySingletonClass Instance { get { return _instance; } }
}

Now each thread will have its own instance of MySingleton.

Upvotes: 5

user586399
user586399

Reputation:

You can use TPL

You can make the loops with TPL parallel, and further more it is built-in with .NET 4.0 so that you don't have to change your program so much

Upvotes: 0

Related Questions