在我的应用程序中,我有许多 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.
