我正在尝试编写在满足条件时执行的代码.目前,我正在使用while ...循环,我知道效率不高.我也在看AutoResetEvent(),但我不知道如何实现它,以便它一直检查,直到条件为真.
代码也恰好存在于异步方法中,所以可能某种等待可以工作吗?
private async void btnOk_Click(object sender, EventArgs e)
{
// Do some work
Task<string> task = Task.Run(() => GreatBigMethod());
string GreatBigMethod = await task;
// Wait until condition is false
while (!isExcelInteractive())
{
Console.WriteLine("Excel is busy");
}
// Do work
Console.WriteLine("YAY");
}
private bool isExcelInteractive()
{
try
{
Globals.ThisWorkbook.Application.Interactive = Globals.ThisWorkbook.Application.Interactive;
return true; // Excel is free
}
catch
{
return false; // Excel will throw an exception, meaning its busy
}
}
Run Code Online (Sandbox Code Playgroud)
我需要找到一种方法来保持检查,isExcelInteractive()
而不会在循环中卡住CPU.
注意:Excel中没有事件处理程序在未处于编辑模式时引发.
我把一些代码从Excel VBA
到C#
碰上了这个问题.我不确定c#中这段代码的等价物.Intellisence
不是很有帮助:(
Selection.ShapeRange.Adjustments.Item(1) = 90
我设法得到Adjustment
了c#,但没有Item
财产.