gpt4 book ai didi

c# - System.Threading.Timer 回调中的关键部分

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:11 25 4
gpt4 key购买 nike

在我的应用程序中,我有许多 System.Threading.Timer 实例。它们中的两个可能会相互重叠。问题是回调方法中有一个关键部分。我关心的是提供适当的同步还是框架提供了解决方案?

问题有点疑惑,我再说清楚一点:回调中我暂时没有同步。

最佳答案

Is it my concern to provide appropriate synchronization

如果它很关键并且它应该具有互斥访问权限而不是是你需要自己处理它。

does framework provide a solution

框架确实提供了多种工具来进行锁定和监控。这取决于您的要求,您需要什么。你需要看看here看看框架提供了什么。

The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.

您还可以选择使用 Mutex

When two or more threads need to access a shared resource at the same time, the system needs a synchronization mechanism to ensure that only one thread at a time uses the resource. Mutex is a synchronization primitive that grants exclusive access to the shared resource to only one thread. If a thread acquires a mutex, the second thread that wants to acquire that mutex is suspended until the first thread releases the mutex.

关于c# - System.Threading.Timer 回调中的关键部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20682295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com