使用单例多线程 C# 编写程序
我用 C# 写了一个程序。现在我完成了所有的功能并且它可以工作了。但只用一个线程运行。我正在进行大量计算,有时会将大约 300 MB 或更多的测量文件加载到应用程序中。
我现在想让程序多线程,因为在密集处理或 I/O 操作时用户体验非常糟糕。
重构程序的最佳方法是什么,以便无需太多努力就可以使其成为多线程?我知道这是我之前应该考虑的事情。但我没有。
我对大约 3 个重要的大模块使用了单例模式,这些模块几乎涉及程序的所有其他功能。
我使用了或多或少干净的 MVC(模型视图控制)架构。所以我想知道是否可以让用户界面在一个线程中运行,而应用程序的其余部分在另一个线程中运行。
如果没有,加载和解析300MB,创建对象将需要大约3分钟才能完成。此时用户不会从 GUI 得到任何响应。 :/
更新: 我的单例被用作一种存储。一个单例保存解析的测量文件的对象,而另一个单例保存结果。我有不同的计算,它们使用相同的测量文件并创建他们想要使用其他单例保存的结果。这是一个问题。
第二个是让该人对用户操作做出响应,或者至少避免出现窗口没有响应的警告。
谢谢大家的建议。我会尝试一下。抱歉回复晚了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,我会避免使用单例模式,因为它会在以后产生很多问题,特别是在测试中。但是,如果您想要每个线程一个单例,则有一个相当简单的解决方案可以使其适用于多个线程。将您的单例引用放在一个字段(而不是属性)中,并使用 ThreadStaticAttribute 对其进行修饰:
现在每个线程都将拥有自己的 MySingleton 实例。
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:
Now each thread will have its own instance of MySingleton.
最简单的方法是将所有计算移至一个单独的线程,并使用
Invoke
/InvokeRequired
更新 GUI。通过这种方式,您可以在计算运行时获得响应 GUI。
只要您不允许用户在运行计算期间进行更改,应用程序就是线程安全的。
使用多个线程进行计算是一个更加复杂的故事,我们需要更多信息才能为您提供正确的答案。
The easiest way is to move all calculations to one separate thread and update the GUI using
Invoke
/InvokeRequired
.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.
您可以使用 TPL
您可以使用 TPL 并行进行循环,并进一步更多的是它内置于.NET 4.0,因此您不必对程序进行太多更改
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