Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
调用webservice时WPF窗口被锁定。即使是异步运行_Wpf_Web Services_Asynchronous - Fatal编程技术网

调用webservice时WPF窗口被锁定。即使是异步运行

调用webservice时WPF窗口被锁定。即使是异步运行,wpf,web-services,asynchronous,Wpf,Web Services,Asynchronous,当从WPF应用程序调用web服务时,我遇到了一个大问题。应用程序/窗口将锁定,直到进程完成。我已尝试异步运行此操作,但问题仍然存在 目前,我正在进行的web服务调用可以持续45-60秒。它在服务器上运行一个进程来获取一大块数据。因为这需要一段时间,我想让一个进度条不确定地移动,让用户看到应用程序没有停止或发生任何事情(你知道他们有多么不耐烦) 因此: 单击一个按钮,应用程序将创建异步内容将发布到的表单,并设置调用异步方法的异步内容 public bool setupDrawingList

当从WPF应用程序调用web服务时,我遇到了一个大问题。应用程序/窗口将锁定,直到进程完成。我已尝试异步运行此操作,但问题仍然存在

目前,我正在进行的web服务调用可以持续45-60秒。它在服务器上运行一个进程来获取一大块数据。因为这需要一段时间,我想让一个进度条不确定地移动,让用户看到应用程序没有停止或发生任何事情(你知道他们有多么不耐烦)

因此:

单击一个按钮,应用程序将创建异步内容将发布到的表单,并设置调用异步方法的异步内容

    public bool setupDrawingList(Guid ID)
    {
        if (systemManager.set(ID))
        {
            wDrawingList.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
            {
                wDrawingList.ShowForm();
                Hide();
            }));

            return true;
        }

        return false;
    }
这是异步方法。showForm方法包含设置新表单的调用,包括monster web服务调用

    public void MyAsyncCallback(IAsyncResult ar)
    {
        // Because you passed your original delegate in the asyncState parameter of the Begin call, you can get it back here to complete the call.
        MethodDelegate dlgt = (MethodDelegate)ar.AsyncState;

        // Complete the call.
        bool output = dlgt.EndInvoke(ar);

        try
        {
            // Retrieve the delegate. 
            AsyncResult result = (AsyncResult)ar;
            AsyncMethodHandler caller = (AsyncMethodHandler)result.AsyncDelegate;

            // Because this method is running from secondary thread it can never access ui objects because they are created 
            // on the primary thread.

            // Call EndInvoke to retrieve the results. 
            bool returnValue = caller.EndInvoke(ar);

            // Still on secondary thread, must update ui on primary thread 
            UpdateUI(returnValue == true ? "Success" : "Failed");
        }
        catch (Exception ex)
        {
            string exMessage = null;
            exMessage = "Error: " + ex.Message;
            UpdateUI(exMessage);
        }
    }

    public void UpdateUI(string outputValue)
    {
        // Get back to primary thread to update ui 
        UpdateUIHandler uiHandler = new UpdateUIHandler(UpdateUIIndicators);
        string results = outputValue;

        // Run new thread off Dispatched (primary thread) 
        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, uiHandler, results);
    }

    public void UpdateUIIndicators(string outputValue)
    {
        // update user interface controls from primary UI thread
        sbi3.Content = "Processing Completed.";
    }
任何帮助或理论都将不胜感激。我不知所措


提前谢谢

我在写这篇文章的时候确实回答了我自己的问题,但我想在我写这篇文章的时候,我可以把答案和它一起发布,以防其他人需要它

我意识到导致问题的方法是在dispatcher中,它基本上覆盖了主线程,所以即使我设置了一个很好的AsSynchronous线程,我仍然返回并在基本上是主线程的线程上运行主进程

或者至少这是我的解释,任何有助于我进一步理解的进一步评论都会被重复

所以我从ShowForm方法中删除了monster调用,并将其直接放在async方法中!窗口功能是我的


HTH

实际上,我在写这篇文章时回答了我自己的问题,但我认为,在我写这篇文章时,我不妨将答案与文章一起发布,以防其他人需要它

我意识到导致问题的方法是在dispatcher中,它基本上覆盖了主线程,所以即使我设置了一个很好的AsSynchronous线程,我仍然返回并在基本上是主线程的线程上运行主进程

或者至少这是我的解释,任何有助于我进一步理解的进一步评论都会被重复

所以我从ShowForm方法中删除了monster调用,并将其直接放在async方法中!窗口功能是我的


HTH

您正在使用dispatcher在GUI线程上调用该方法。这意味着,即使您从后台线程开始,您也会直接切换回GUI线程,因此GUI将无法响应


您需要让主要工作在后台线程上进行,然后只在需要更新进度条时使用dispatcher切换到GUI线程。

您正在使用dispatcher在GUI线程上调用该方法。这意味着,即使您从后台线程开始,您也会直接切换回GUI线程,因此GUI将无法响应


您需要让主要工作在后台线程上进行,然后只在需要更新进度条时使用调度程序切换到GUI线程。

看起来您是从异步方法(wat?)同步执行UI任务。此外,您的代码过于复杂,您可能会注意到这一点,因为您对它感到困惑


我建议大大简化这一点,主要是通过异步执行web服务调用,然后从回调中更新UI。一旦你完成了这项工作,你就可以看到如何把所有其他让我头疼的东西都加回去。

看起来你是在用异步方法(wat?)同步地做UI工作。此外,您的代码过于复杂,您可能会注意到这一点,因为您对它感到困惑


我建议大大简化这一点,主要是通过异步执行web服务调用,然后从回调中更新UI。一旦你完成了这项工作,你就可以看到把所有其他让我头疼的东西都加回去。

结果是你比我打字更快地解决了它。很好。结果是你比我打字快。不错,我同意。我确实需要把它整理一下。这只是几个小时的黑客攻击和尝试不同事物的结果lol@SumGuy是的,以前做过。现在,我制作了一个测试应用程序,它只做我需要的事情,以证明代码,然后将其推出。这比在我迷失方向时尝试使用已有代码更好…同意。我确实需要把它整理一下。这只是几个小时的黑客攻击和尝试不同事物的结果lol@SumGuy是的,以前做过。现在,我制作了一个测试应用程序,它只做我需要的事情,以证明代码,然后将其推出。当我迷失方向时,使用新代码比尝试使用已有代码更好。。。
    public void MyAsyncCallback(IAsyncResult ar)
    {
        // Because you passed your original delegate in the asyncState parameter of the Begin call, you can get it back here to complete the call.
        MethodDelegate dlgt = (MethodDelegate)ar.AsyncState;

        // Complete the call.
        bool output = dlgt.EndInvoke(ar);

        try
        {
            // Retrieve the delegate. 
            AsyncResult result = (AsyncResult)ar;
            AsyncMethodHandler caller = (AsyncMethodHandler)result.AsyncDelegate;

            // Because this method is running from secondary thread it can never access ui objects because they are created 
            // on the primary thread.

            // Call EndInvoke to retrieve the results. 
            bool returnValue = caller.EndInvoke(ar);

            // Still on secondary thread, must update ui on primary thread 
            UpdateUI(returnValue == true ? "Success" : "Failed");
        }
        catch (Exception ex)
        {
            string exMessage = null;
            exMessage = "Error: " + ex.Message;
            UpdateUI(exMessage);
        }
    }

    public void UpdateUI(string outputValue)
    {
        // Get back to primary thread to update ui 
        UpdateUIHandler uiHandler = new UpdateUIHandler(UpdateUIIndicators);
        string results = outputValue;

        // Run new thread off Dispatched (primary thread) 
        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, uiHandler, results);
    }

    public void UpdateUIIndicators(string outputValue)
    {
        // update user interface controls from primary UI thread
        sbi3.Content = "Processing Completed.";
    }