使用单例多线程 C# 编写程序 [英] Making a program with singletons multithreaded C#

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

问题描述

我用 C# 编写了一个程序.现在我完成了所有功能并且它可以工作.但只能用一个线程运行.我正在进行大量计算,有时将大约 300 MB 或更多的测量文件加载到应用程序中.

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/O 操作的时候用户体验真的很糟糕.

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.

我对大约 3 个大而重要的模块使用了单例模式,这些模块几乎涉及程序的所有其他功能.

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

我使用了或多或少干净的 MVC(模型视图控制)架构.所以我想知道是否有可能让用户界面在一个线程中运行,而应用程序的其余部分在另一个线程中运行.

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.

如果没有,加载和解析 300MB,创建对象大约需要 3 分钟才能完成.此时,用户不会从 GUI 得到响应.:/

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.

推荐答案

最简单的方法是将所有计算移动到一个单独的线程并使用 Invoke/InvokeRequired.

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

通过这种方式,您可以在计算运行时获得响应 GUI.

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.

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

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