- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 Windows 服务,它使用带有回调的 System.Threading.Timer 来更新端点,如下所示:
UpdateEndpointTimer = new Timer(
new TimerCallback(UpdateSBEndpoints),
Endpoint.Statuses.Online,
EndpointUpdateFrequency,
EndpointUpdateFrequency);
我的更新方法大致如下:
private void UpdateSBEndpoints(object state)
{
...
using (var context = new TestHarnessContext())
{
var endpoints = context.Endpoints.Where(p =>
p.Binding == Endpoint.Bindings.ServiceBus
&& p.State == Endpoint.States.Enabled
&& p.Status != status).ToList();
foreach (var endpoint in endpoints)
{
//Do stuff here
}
...
}
现在,由于计时器使用 ThreadPool 中的线程来触发回调,我需要采取措施来控制线程。当多个线程可以在第一个线程完成工作之前从数据库中获取相同的端点时,就会出现一个特定的问题,这会导致在 foreach 循环中完成重复的工作。
我知道该问题有两种可能的解决方案,我想知道哪一种更好,更适合使用。解决方案是 ConcurrentDictionary
和 ManualResetEvent
。
在第一种情况下,我会把它放在我的 foreach 循环中,以确保一次只有一个线程可以在给定的端点上运行:
if (EndpointsInAction.TryAdd(endpoint.Id, endpoint.Id) == false)
// If we get here, another thread has started work with this endpoint.
return;
...
//Do stuff with the endpoint, once done, remove its Id from the dictionary
...
int id;
EndpointsInAction.TryRemove(endpoint.Id, out id);
在第二种情况下,我会像这样控制线程:
protected ManualResetEvent PubIsBeingCreated { get; set; }
protected ManualResetEvent SubIsBeingCreated { get; set; }
...
this.PubIsBeingCreated = new ManualResetEvent(true);
this.SubIsBeingCreated = new ManualResetEvent(true);
...
foreach (var endpoint in endpoints)
{
if (!this.PubIsBeingCreated.WaitOne(0))
// If we get here, another thread has started work with this endpoint.
return;
try
{
// block other threads (Timer Events)
PubIsBeingCreated.Reset();
// Do stuff
}
...
finally
{
// Restore access for other threads
PubIsBeingCreated.Set();
}
}
现在这两种方法似乎都有效我想知道哪种方法更适合使用(效率更高?)。我倾向于使用 ConcurrentDictionary
,因为它允许更好地过滤线程,即不允许两个线程与特定端点一起工作,而不允许两个线程与特定端点一起工作类型(ManualResetEvents 中的发布和订阅)。可能有其他解决方案优于我的解决方案,因此我们将不胜感激。
最佳答案
在任何情况下,您都不应为此目的使用 ManualResetEvent
。使用提供更高级别抽象的对象总是更好,但即使您想在低级别控制它,也可以使用 .NET 的 Monitor 类(通过 lock
语句和 Monitor .Wait()
和 Monitor.Pulse()
方法比使用 ManualResetEvent
更好。
您似乎有一个相当标准的生产者/消费者场景。在那种情况下,在我看来,您最好使用 ConcurrentQueue
而不是 ConcurrentDictionary
。您的生产者会将事物排入队列,而您的消费线程会将它们从队列中取出以进行处理。
关于C# ConcurrentDictionary 与线程控制的 ManualResetEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26639150/
我正在尝试实现一个类,该类使用一个简单的缓存来保存从内部服务检索到的数据。我正在使用 ManualResetEvent 来阻止多个线程,这些线程可能会尝试与第一个线程同时刷新缓存数据,以便在通过调用
我有要处理的批处理列表。永远。 我想并行处理每个 block (5),完成后移动到下一个 block 。 出于某种原因,下面的代码不会等待 block 完成并继续,即使它没有完成。 while (tr
我知道如何使用 ManualResetEvent 或同步原语(如 Monitor)来等待事件和/或锁定,但我想知道是否有一种方法可以实现如下内容: ManualResetEvent resetEven
问题: 我正在尝试从 ThreadPool 中抛出 6 个线程来处理单个任务。每个任务的ManualResetEvent 都存储在一个手动重置事件数组中。线程数对应ManualResetEvent数组
我正在使用一个使用 ManualResetEvent 同步线程的应用程序。 FxCop 告诉我处理这些对象。我发现以下讨论告诉我相同的内容: Do I need to Dispose() or Clo
我有一个类允许其他线程等待,直到它使用 ManualResetEventSlim 完成操作。 (操作通常很简短) 这个类没有明确的生命周期,所以没有一个地方可以轻松关闭事件。 相反,我想在事件完成后立
嗨当我使用以下代码时: myManualResetEvent.Dispose(); 编译器报错: 'System.Threading.WaitHandle.Dispose(bool)' is in
假设我必须在 .Net 3.5 SP1 中编排同步算法,标题中列出的任何同步原语都非常适合该任务。 从性能的角度来看,是否有任何一个比其他的性能更好? 我问这个是因为我已经编码了一段时间,但对这个主题
我在锁定 manualResetEvent 实例时遇到了死锁。我无法弄清楚如何解决它。我将不胜感激任何帮助。 我在一个由不同线程执行的类中有两个方法: private ManualResetEvent
我有以下代码 ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadAsync), apiMethod); downloadHandle.Wait
我觉得我应该知道这个问题的答案,但我还是会问,以防万一我犯了一个潜在的灾难性错误。 以下代码按预期执行,没有错误/异常: static void Main(string[] args) { M
我对我的简单代码有疑问: public partial class Form1 : Form { ManualResetEvent ResetEvt = new ManualResetEvent
我写了一个下载一些文件的方法,现在我试图让它并行下载最多 5 个文件,其余的等待前面的文件完成。我为此使用了 ManualResetEvent,但是当我包含同步部分时,它不再下载任何内容(没有它就可以
是c# ManualResetEvent和 AutoResetEvent创建或维护类的成本很高吗? 它们是否消耗某种有限的 Windows 内核资源,如果是,它的限制程度如何? 例如如果我的代码可以每
我还处于计划阶段,所以我没有完整的代码可以展示。但是我很好奇,如果您想在不同的程序集之间同步线程,将如何使用 manualresetevent。例如,我在 assembly1 中编写了执行 task1
我正在使用 Starksoft.Net.Ftp 执行异步上传操作。 看起来像这样: public void UploadFile(string filePath, string package
我有一个 Windows 服务,它使用带有回调的 System.Threading.Timer 来更新端点,如下所示: UpdateEndpointTimer = new Timer(
我正在测试中使用 ManualResetEvent 类。 基本上,我想在调用特定函数时调用 Set() 方法。这看起来像: var mre = new ManualResetEvent(false);
在我的一个项目中,我需要安排一项任务在后台线程中的某个指定时间间隔后完成。我使用 ThreadPool.RegisterWaitForSingleObject 机制来做到这一点。根据文档,第一个参数可
我对我正在使用的 ManualResetEvent 感到有点困惑,它似乎没有解锁。有人知道为什么会这样吗? 我遇到的情况与此类似。实际情况相当复杂,我没有设法隔离出一段可以合理发布以重现问题的代码。
我是一名优秀的程序员,十分优秀!