用于C#的多线程包装器或SDK [英] Multithreading wrapper or SDKs for C#

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

问题描述

.NET框架中是否包含除线程之外的其他C#包装程序或SDK?

Are there any other wrapper or SDK for C# other than thread include in .NET framework?

推荐答案

如果您认为直接使用Thread 类不适合您,看看BackgroundWorker class.

从理论上讲,尽管您可以编写自己的框架来与本地线程API进行P/Invokes或以其他方式互操作,但我不确定该框架是否可以像您期望的那样自动使用托管代码.因为.NET在创建其托管"线程时似乎还要做很多额外的工作.
If you think directly using the Thread class does not suite you, take a look at the BackgroundWorker class.

And theoretically, while you can write your own framework that P/Invokes or otherwise interops with the native thread APIs, I am not sure it will automatically work as you expect with managed code. Because .NET seems to do quite some extra plumbing work when it creates its "managed" threads.


在线程周围进行包装实际上非常有用.同时,尝试为所有可想到的线程应用程序提供通用解决方案不是很有用,否则可以改进线程类本身,或者建议使用特殊的语言构造.

同时,在应用程序级别缠绕线程非常方便.

我可以在提示/技巧"部分的小文章中说明此想法:
用于线程通信和线程间调用的简单阻塞队列 [ ^ ].

请查看用法示例,并注意如何在类QueuedThreadDelegateInvocationThread中包装线程.首先,要在线程中运行的方法被封装并从所使用的类(私有)中隐藏起来,因此没有人可以在错误的线程中调用它.线程初始化方便地被隐藏;还封装了一种用于线程控制和通信的方法,因此没有人可以通过直接访问该线程来滥用该线程.
Wrapping around a thread is actually very useful. At the same time, an attempt to make a universal solution for all thinkable thread application not very useful, otherwise thread class itself could be improved, or a special language constructs would be advised.

At the same time, wrapping around thread at the level of application is very convenient.

I can illustrate this idea in my small article from Tips/Tricks section:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

Please look at the usage sample and pay attention how threads are wrapped in the classes QueuedThread and DelegateInvocationThread. First of all, the method to be run in a thread is encapsulated and hidden from the class used (private), so no one can call it in a wrong threads; thread initialization is conveniently hidden; also encapsulated a method for thread control and communication, so nobody can abuse the thread via direct access to it.


这篇关于用于C#的多线程包装器或SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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