- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将第三方组件添加到我们的一个产品中(这是一种可以 24/7 全天候运行的 Windows 服务)。
3PC 是一个 .net 库,它基于一些核心的 C++ 可爱操作图像。
3PC 要求为它运行的每个线程调用 Initialize 和 Teardown 例程。
这在我们在旧软件中使用它的地方很好,但是这个产品是用 .Net 线程池编写的,池化 worker 将使用 3PC。我不知道如何安全地调用 Initialize 和 Teardown 例程。
我最接近的是在初始化 ThreadStatic 成员时调用 3PC Initialize
方法,但是我无法调用 Teardown
在同一线程 Initialize
被召唤。
如果我包装 Initialize
和 Teardown
在一个对象中,使用 Teardown
在对象的 Finalize 方法中调用,然后是 Teardown
将被 GC 自己的 Finalize 线程调用,而不是对象静态的线程(更不用说不能保证 Finalizer 将永远运行的事实)。
显然我担心泄漏的资源,因为线程池在幕后管理线程,我不知道线程是否或何时会被销毁或创建 ,所以我不知道在一段时间内服务可能会泄漏多少。
有人有什么想法吗?我错过了什么吗?还有什么可以尝试的吗?
谢谢
更新
问:Teardown 有什么作用?
我假设它“释放一些内存”,但老实说我不知道。我尝试使用 Reflector 遍历程序集,但它很快从 IL 下降到 native 机器代码。我要在(第三)方路线上,这必须完成。
这绝对是一个子系统拆卸的东西。
此外,几年前,我们在另一个产品中发现了有关此组件的错误。并不是每个线程都调用了 Initializer,这导致了一些非常罕见的未定义行为。
最佳答案
如果最坏的情况出现,并且没有更好的解决方案,您可以使用固定数量的线程(=核心数?)创建自己的线程池。通过在每个线程中创建一个 3PC 实例并调用 Initialize(),您应该没问题。
就像是:
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Threading;
namespace WindowsPoolApp
{
public abstract class Task {
public EventHandler FonComplete;
public ThreadPool myPool;
protected int param;
public Exception error;
public Task(int inParam, EventHandler OnDone) { param = inParam; FonComplete = OnDone; }
public abstract void run();
};
public class PoolThread{
private
3PC my3PC;
BlockingCollection<Task> FinQueue;
public
PoolThread(BlockingCollection<Task> inQueue)
{
FinQueue=inQueue;
}
Task inMess;
public void run(){
my3PC = new 3PC();
my3PC.Initialize();
while(true){
inMess=FinQueue.Take();
if(inMess==null){
my3PC.Teardown();
return;
}
try
{
inMess.run();
inMess.error = null;
}
catch (Exception e)
{
inMess.error = e;
}
inMess.FonComplete(inMess, null);
}
}
};
public class ThreadPool {
volatile int FthreadCount;
BlockingCollection<Task> queue;
void startThread(){
PoolThread thisPoolThread=new PoolThread(queue);
Thread thisThread=new Thread(new ThreadStart(thisPoolThread.run));
thisThread.Priority = ThreadPriority.BelowNormal;
thisThread.IsBackground = true;
thisThread.Start();
}
void SetThreadCount(int newCount){
while(FthreadCount<newCount){startThread();};
while(FthreadCount>newCount){
queue.Add(default(Task));
FthreadCount--;
};
}
public ThreadPool(int initThreads){
queue=new BlockingCollection<Task>();
for(FthreadCount=0;FthreadCount<initThreads;FthreadCount++) startThread();
}
public int threadCount{
get{return FthreadCount;}
set
{
while (FthreadCount < value) {
startThread();
FthreadCount++;
};
while (FthreadCount > value)
{
queue.Add(default(Task));
FthreadCount--;
}
}
}
public void submit(Task task){
task.myPool=this;
queue.Add(task);
}
};
}
关于.net - 无论如何要知道池线程(或 ThreadStatic 成员)何时被销毁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10399680/
我正在使用 if(Thread.IsAlive) 检查线程的状况。该线程中正在运行一个表单。有时在执行过程中,即使窗体保持打开状态,对 Thread.IsAlive 的调用似乎评估为 false。我想
This question让我想到了 .NET 等价物。 ThreadState 有什么值(value)? Thread类的属性?在此代码示例中: if (someThread.ThreadState
我有以下代码。这可能是个愚蠢的问题,但我不确定是否需要同步。 class MyClass { [ThreadStatic] private static object _staticObjec
我们有一个相当大的现有代码库,用于构建在 ASP.NET 之上的各种网络服务,该代码大量使用访问 HttpContext.Current.User(包装为 Client.User ),我相当确定它在内
[ThreadStatic] 属性如何工作?我假设编译器会发出一些 IL 来填充/检索 TLS 中的值,但查看反汇编时它似乎并没有在那个级别执行它。 作为后续,如果你把它放在一个非静态成员上会发生什么
[线程静态] 私有(private)静态 Foo _foo; 公共(public)静态 Foo CurrentFoo { 得到 { 如果(_foo == null){ _foo = new Foo()
我们的 WCF 服务引用的库之一使用 ThreadStatic 变量。服务方法在每次调用开始时设置它的值。我想知道这是否安全——换句话说,我们能否保证只有一个线程专门用于整个调用?或者是否有可能调用可
对于一些静态方法,我意识到在操作过程中使用一个小数组来临时存储值是非常方便的。所述数组很有用,因为您需要索引,但每次调用该方法时都会分配那个小数组。 这是解决 C# 中缺少类 C 静态局部变量的好方法
有没有一种优雅的方法可以知道工作线程何时执行完毕,以便我可以访问它产生的资源? 例如,如果工作线程使用查询 SQL Server 列表 ServersSqlDataSourceEnumerator.I
我有一个需要几秒钟加载的表单。因此,我想显示一个带有文本“正在加载,请稍候”的小表格。表单加载完成后,必须关闭加载表单。 所以,我做了一个简单的类,它在线程中显示加载表单: public class
是否有可能根据 Thread 引用获取该线程的 ThreadStatic 值? 最佳答案 不,那是不可能的。与此类属性一样,[ThreadStatic] 属性被抖动识别。它生成对 CLR 的调用以获取
当我使用 ThreadStatic 属性探索线程和任务时,我遇到了一些奇怪的事情。我相信这可能非常特定于线程和任务。考虑以下代码片段: [ThreadStatic] static int range=
开始编辑 执行此操作的“正确”方法(4.5 之前)是使用此处概述的 SynchronizationContext:http://msdn.microsoft.com/en-us/magazine/gg
[ThreadStatic()]是什么意思 我得到了一段代码,我找到了[ThreadStatic()]在那里使用是什么意思....何时使用[ThreadStatic()] public class C
调用前: th.Abort(); 如何等待 ThreadState 变为 ThreadState.Abort 然后执行某些操作?可能吗? 最佳答案 您可以调用 Thread.Join() (MSDN)
ThreadStatic attribute将静态变量声明为每个线程唯一。您知道正确处理此类变量的简单模式吗? 我们在ThreadStatic之前使用的是一个ThreadContextManager。
怎么会像ThreadStatic在 TPL 任务中使用?我的理解(“Wrox Professional Parallel Programming with C#”,第 74 页)是任务可以在执行期间从
我想了解单例模式设计。我想从我的单例类中为每个线程创建单独的实例。所以我在下面提供了两种设计。 正在运行 class Program { static void Main(string[] a
我有一个我编写的日志记录框架,它能够跟踪“日志记录上下文”。它有一个可插入的策略框架,但我最常使用的是一个 ThreadStatic 变体,它在 [ThreadStatic] 中跟踪上下文。多变的。我
我正在阅读 tips and tricks发布,我想我会尝试一些我以前从未做过的 C# 东西。因此,下面的代码没有任何实际用途,只是一个“测试函数”,看看会发生什么。 无论如何,我有两个静态私有(pr
我是一名优秀的程序员,十分优秀!