Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以从方法内部调用用于为方法创建参数的函数?(C)_C#_.net_Delegates - Fatal编程技术网

C# 是否可以从方法内部调用用于为方法创建参数的函数?(C)

C# 是否可以从方法内部调用用于为方法创建参数的函数?(C),c#,.net,delegates,C#,.net,Delegates,这有点难以解释,但我希望这个例子能澄清这一点 假设我有一些可见的函数调用: public bool Visible(/* Some page element */) { // Checks if something on a webpage is visible. Returns a "true" is yes, and "false" if not } 是否有可能等待该函数返回true?到目前为止,我写的是这样的: public void WaitUn

这有点难以解释,但我希望这个例子能澄清这一点

假设我有一些可见的函数调用:

public bool Visible(/* Some page element */)
    {
        // Checks if something on a webpage is visible. Returns a "true" is yes, and "false" if not
    }
是否有可能等待该函数返回true?到目前为止,我写的是这样的:

    public void WaitUntil(/*function returning bool*/ isTrue)
    {
        for (int second = 0; ; second++)
        {
            if (second >= 12)
            {
                /* Thow exception */
            }
            else
            {
                if (isTrue /*calls the isTrue function with given parameters*/)
                {
                    return;
                }
            }
        }
    }
使这两种方法可以一起使用,如:

WaitUntil(Visible(/* Some page element */));

要等待页面元素可见,请执行以下操作。。。这是可能的吗?

< P>这里是如何做这件事,尽管你应该考虑使用事件,因为这种等待被强烈地阻止了

/*Important Note: This is ugly, error prone 
          and causes eye itchiness to veteran programmers*/
public void WaitUntil(Func<bool> func)
{
    DateTime start = DateTime.Now;
    while(DateTime.Now - start < TimeSpan.FromSeconds(12))
    {
        if (func())
        {
                return;
        }
        Thread.Sleep(100);
    }
    /* Thow exception */
}

//Call
WaitUntil(() => Visible(/* Some page element*/));

我认为您应该使用一个事件,或者简单地实现对waituntil中所做的任何操作的调用,使某些内容在页面上可见或不可见。您在做什么WebForms包含PageLoaded的EventHandler-如何使用服务器端代码测试页面上是否有可见的内容?如果这是服务器端代码,类似的内容将阻止页面加载。用户将看到无需解释的长时间延迟。