此方案是否需要多线程 [英] Is Multi Threading required for this scenario

查看:45
本文介绍了此方案是否需要多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个带2个按钮的窗体。一个按钮将调用Get方法,另一个按钮将调用update方法。所以最后两者都指向相同的数据表。我真的需要在这里使用多线程吗?



我还有另一个场景:



我有一个文本框,显示有多少线程到开始。如果值为10那么,那么Get方法将在10个线程上执行,我将确定哪个线程占用的时间最长,哪个线程花费的时间最短...请帮我怎样才能做到这一点。谢谢





请澄清......



谢谢。

解决方案

在您的情况下,多线程可能毫无用处。如果(至少)一个任务需要花费大量时间(用户可以注意到)来完成它将是有用的:通常使用线程来防止在长时间操作发生时冻结GUI。


< blockquote>请参阅我对该问题的评论。这个问题没有制定成允许最终决定的细节。



多线程可能有许多不同的理由。特别是,你应该在一个单独的(非UI)线程中调用阻止或简单地延迟执行的所有方法。



更基本上,它可能是必需的通过逻辑。如果你有一些顺序处理,或多或少独立于它的逻辑,它应该是一个单独的线程。应该以整体的方式考虑这个问题。某些算法在仅限控制台的应用程序中运行最佳,在窗口UI中需要单独的线程,依此类推。







请参阅我过去在相关主题上的一些答案:



创建主题的最佳方法之一:

如何将ref参数传递给线程 [ ^ ],

更改线程(生产者)启动后的参数 [ ^ ],

C#中的MultiThreading [ ^ ];



正确限制线程(Resume / Pause因不安全而被弃用):

线程中的手动重置事件和自动重置事件[ ^ ],

暂停正在运行的主题 [ ^ ];



调用UI线程:

控制.Invoke()vs. Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



我列出的主题实际上是最重要的,当用户界面时参与。



-SA


虽然CPallini一般是正确的,但我总是尽力保持任何可能重复使用的东西,或者有朝一日可能会出现意外或不需要的行为。如果您可以将数据库调用构建到可以重用的类中,那么就这样做。此外,我认为从UI线程关闭数据库并不是一个坏主意。总有一天可能会出现网络缓慢/故障,或数据库服务器速度缓慢或出现问题 - 如果你在UI线程中进行调用,那么你的用户界面就会冻结。



我将设置一个委托并从线程中执行该操作,然后将结果传递给更新UI。


Hi All,

I have a windows form with 2 buttons. One button would call the Get method and the other button will call update method. So at the end both will point to the same data table. Do i really need to use Multi-threading here?

I have an other scenario too:

I have a text box which shows how many threads to start. If the value is 10 then, then Get method will be executed on 10 threads and i would determine which thread is taking the longest time and which is taking the shortest time...Please help me how can i do this.Thanks


Please clarify...

Thanks.

解决方案

Probably multithreading is useless in your case. It would be usefulif (at least) one task takes a lot of time (that is user noticeable) to complete: often threads are used to prevent the GUI freeze while a long time operation is occurring.


Please see my comment to the question. The problem is not formulated to the detail allowing to decide finally.

Multithreading may have many different justification. In particular, you should call all methods which blocks or simply considerably delay the execution, in a separate (non-UI) thread.

More essentially, it can be required by logic. If you have some sequential processing, more or less independent in its logic, it should be a separate thread. The problem should be considered in a holistic way. Some algorithm working the best in a console-only application, will require a separate thread in a windowed UI, and so on.

[EDIT]

Please see some of my past answers on related topics:

One of the best ways to create a thread:
How to pass ref parameter to the thread[^],
Change parameters of thread (producer) after it is started[^],
MultiThreading in C#[^];

throttling threads properly (Resume/Pause is deprecated as unsafe):
ManualResetEvent and AutoResetEvent in Thread[^],
pause running thread[^];

invocation to UI thread:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

The topics I listed are practically the most important, when the UI is involved.

—SA


While CPallini is generally right, I always try to keep anything that is potentially reusable, or could possibly someday exhibit unexpected or unwanted behavior out of the UI. If there''s a possibility that you can build your database calls into a class you can reuse, then do that. Also, I think making database calls off the UI thread is not a bad idea. There could always be network slowness/trouble someday, or database server slowness or problems - and if your making those calls in the UI thread then your UI will freeze.

I would set up a delegate and do that stuff off thread, and then pass your callback the results to update your UI.


这篇关于此方案是否需要多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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