在C#中使用线程的优缺点 [英] Pros and Cons of using Threads in C#

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

问题描述

大家好。

我已经了解了使用线程和多线程编程的好处,但我想知道,我们可以编写一个没有线程的程序吗?

Hi every one
I have understood the benefits of using threads and multithreading programming but I want to know, can we write a program without thread?

推荐答案

请参阅我对问题和解决方案2的评论。



现在,关于利弊的问题本身是不正确的。首先,你总是至少有一个线程。在许多情况下添加更多线程不是您的选择,它是由需求间接定义的。



例如,假设您对数值模拟有一些要求:您需要计算一些运动物体的轨迹(更确切地说,世界线 )。如果问题完全可以解决,你总是可以做一个线程,这可能是最好的选择。现在,想象一下,当运动进展时,你需要输出一些身体的中间位置,并让用户有动力停止运动或动态改变一些参数。那么你至少还需要一个线程。显然你需要至少两个线程,如果你必须可视化运动:一个用于UI,第二个用于运动场景。



你得到了吗?这个想法?这取决于场景和要求。要求不应该直接规定你使用线程,但它可能是不可避免的或非常实用的(或者说,有一个线程是非常不切实际的。)



和由您的应用程序建模的一些现实生活现象可以根据其性质进行多线程化。成像您正在模拟两个或更多动物的行为,每个动物都有自己的目标和行为计划。行为上的这种独立性在逻辑上可能需要每个玩家一个单独的线程(在这种情况下是动物)。



不,这不是关于利弊。这是关于什么以及如何。 :-)



-SA
Please see my comments to the question and Solution 2.

Now, the question about "pros and cons" is itself incorrect. First, you always have at least one threads. Adding more threads in many cases is not your choice, it is indirectly defined by the requirements.

For example, imagine that you got some requirements for numerical simulation: you need to calculate trajectories of some moving bodies (more exactly, worlds lines). If the problem is solvable at all, you always can do it one thread, and it's likely the best option. Now, imagine you need to output some intermediate locations of bodies as motion progresses and give the user the power to stop motion or change some parameters on the fly. Then you will certatainly need at least one more thread. And apparently you would need at least two threads if you have to visualize the motion: one for the UI, second one for the scenario of the motion.

Are you getting the idea? It depends on scenario and requirements. Requirements should not directly dictate you to use threads, but it can be either unavoidable or highly practical (or, rather, having one thread would be extremely impractical).

And some phenomena of real life to be modeled by your application can be multithreaded by their nature. Imaging you are modeling behavior of two or more animals, each one with its own goals and behavioral program. Such independence in behavior will likely logically require a separate thread per player (animal, in this case).

No, this is not about "pros and cons". This is about what and how. :-)

—SA


技术上不,一切都在线程上运行。 .NET框架负责处理线程,甚至只是消耗事件或使用委托创建 使用线程。 (编辑:为了正确扩展,委托和事件是C#中的类型安全函数指针,在其他线程上运行。事件是一种特殊类型的委托,有时在除控件或对象的所有者的线程之外的线程上调用创建于。)



您可以在不显式创建线程的情况下编程(不使用Thread类,BackgroundWorker或ThreadPool)吗?是的,当然,但仍有线程在幕后运行。 (编辑:线程意味着应用程序和与处理器相关的其他工作,任务调度程序基于线程。您的应用程序在处理器循环的一个(或多个)线程上运行,因此您至少有一个线程在你的程序中。)
Technically no, everything runs on threads. The .NET framework takes care of threading, even just consuming events or using delegates creates uses threads. ( To expand on that properly, delegates and events are typesafe function pointers in C# that operate on other threads. Events are a special type of delegate, and are sometimes invoked on threads other than the thread the owner of the control or object was created on.)

Can you program without explicitly creating threads (without using the Thread class, or BackgroundWorker, or ThreadPool)? Yeah, sure, but there are still threads running behind the scenes. ( Threads meaning applications and other work associated with the processor, of which the task scheduler is based on threads. Your application runs on one (or more) of these threads that the processor is cycling through, so you have at least one thread in your program at all times.)


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

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