Dispatcher.Invoke with Action发送响应

时间:2018-02-04 10:44:59

标签: c#

主要问题:

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
        if (e.Error == null)
        {
            internetAvailable = true;

            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
            {
                 richTextBox1.Text = "Checking Network Connection... [STATUS = SUCCESSFUL]";
            }));
        }
        else 
        {
            internetAvailable = false;

            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
            {
                richTextBox1.Text = "Checking Network Connection... [STATUS = UNSUCCESSFUL]";
            }));
        }
}

当我使用此代码时,出现错误:

  

非静态字段,方法或属性,Dispatcher.Invoke(DispatcherPriority,Delegate)

需要对象引用

我最近在一个帖子中看到了如何通过C#表单查看计算机是否具有当前互联网连接的评论并且我决定使用它,但是我有所有引用可能不会抛出此错误。< / p>

以下是整个代码,以防您需要查看整个内容:

void CheckInternetConnectivity(object state)
{
        if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
                webClient.Proxy = null;
                webClient.OpenReadCompleted += webClient_OpenReadCompleted;
                webClient.OpenReadAsync(new Uri("http://clients3.google.com/generate_204"));
            }
        }
    }

    public delegate void NextPrimeDelegate();
    volatile bool internetAvailable = false;

    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            internetAvailable = true;
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
            {
                 richTextBox1.Text = "Checking Network Connection... [STATUS = SUCCESSFUL]";
            }));
        }
        else 
        {
            internetAvailable = false;
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
            {
                richTextBox1.Text = "Checking Network Connection... [STATUS = UNSUCCESSFUL]";
            }));
        }
}

所以它通过基于Web的客户端(http://clients3.google.com/generate_204)测试连接,然后如果它成功返回,它应该运行委派的操作,但是出了点问题。

无法发布图片链接:

任何帮助都会受到赞赏,欢呼声

0 个答案:

没有答案