小编Key*_*lee的帖子

C#等到条件成立

我正在尝试编写在满足条件时执行的代码.目前,我正在使用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中没有事件处理程序在未处于编辑模式时引发.

c# loops

25
推荐指数
4
解决办法
8万
查看次数

Excel VBA到C#

我把一些代码从Excel VBAC#碰上了这个问题.我不确定c#中这段代码的等价物.Intellisence不是很有帮助:(

Selection.ShapeRange.Adjustments.Item(1) = 90

我设法得到Adjustment了c#,但没有Item财产.

c# excel vba

6
推荐指数
1
解决办法
74
查看次数

标签 统计

c# ×2

excel ×1

loops ×1

vba ×1