例如,我有两个主要方法和一个UI方法:
支持互斥体结构或一类数组的方法1
void Method1(){
// ORM & Database readings code goes here
}
void Method2(){ dim x as integer }
x不是互斥体,为什么?
不是method3()调用method2()_ mutex :: monitor ??为什么?
答案 0 :(得分:1)
在这种情况下,我建议使用SemaphoreSlim
以避免轮询。
ctor()
{
// Initialize the semaphore.
this.semaphore = new SemaphoreSlim(1, 1);
}
public async void Button_Click(object sender, EventArgs args)
{
await Method2Async();
}
private async Task Method2Async()
{
await this.semaphore.WaitAsync();
ICalculationResult result = await CalculateAsync(); // Do your calculations and then continue
await Method1Async(result);
this.semaphore.Release();
}
private async Task Method1Async()
{
// Async Method1 implementaion
}
private async Task<ICalculationResult> CalculateAsync()
{
await Task.Run(()=>
{
return new ICalculationResult(); // Do the calculation and return the result
});
}