C#WUApiLib-异步下载并安装 [英] C# WUApiLib - Download and Install Asynchronously

查看:119
本文介绍了C#WUApiLib-异步下载并安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft的WUApiLib库进行编程.我编写了一个简单的应用程序,该应用程序搜索所有未安装的软件更新,然后下载并安装它们.这部分工作正常(我使用此处找到的代码来提供帮助: http://www.nullskull.com/a/1592/install-windows-updates-using-c--wuapi.aspx ).

I am programming using the WUApiLib library from Microsoft. I have written a simple application that searches for all software updates that are not installed and then downloads and installs them. This part is working perfectly (I used the code found here to help: http://www.nullskull.com/a/1592/install-windows-updates-using-c--wuapi.aspx).

但是,我想利用该库的BeginDownload,EndDownload,BeginInstall,EndInstall功能,以便它可以将进度报告回界面.那篇文章中的功能是同步的,而我提到的功能是异步的.

However, I want to make use of the BeginDownload, EndDownload, BeginInstall, EndInstall functionality of the library so that it can report progress back to the interface. The functions in that article are synchronous and the functions I mention are asynchronous.

我正在使用此页面上的第一个答案作为模板: C#和WUAPI:BeginDownload函数

I am using the first answer on this page as a template: C# and WUAPI: BeginDownload function

但是,我的Invoke()函数从未调用过,我也不知道为什么.

However, my Invoke() function is never getting called and I can't figure out why.

iUpdateDownloader_onProgressChanged progress = new iUpdateDownloader_onProgressChanged(this);
IDownloadJob downloadJob = downloader.BeginDownload(progress, new iUpdateDownloader_onCompleted(this), new iUpdateDownloader_state(this));

public class iUpdateDownloader_onProgressChanged : IDownloadProgressChangedCallback
{
    private frmMain form1;

    public iUpdateDownloader_onProgressChanged(frmMain mainForm)
    {
        this.form1 = mainForm;
    }

    // Implementation of IDownloadProgressChangedCallback interface...
    public void Invoke(IDownloadJob downloadJob, IDownloadProgressChangedCallbackArgs e)
    {

        decimal bDownloaded = ((e.Progress.TotalBytesDownloaded / 1024) / 1024);
        decimal bToDownloaded = ((e.Progress.TotalBytesToDownload / 1024) / 1024);
        bDownloaded = decimal.Round(bDownloaded, 2);
        bToDownloaded = decimal.Round(bToDownloaded, 2);

        form1.setDownloadProgressText("Downloading Update: "
         + e.Progress.CurrentUpdateIndex
         + "/"
         + downloadJob.Updates.Count
         + " - "
         + bDownloaded + "Mb"
         + " / "
         + bToDownloaded + "Mb");
    }
}

我可以在Invoke函数的第一行上放置一个断点,但是它永远不会到达那里.

I can put a breakpoint on the first line in the Invoke function and it never reaches there.

有什么想法吗?

推荐答案

我注意到的第一件事是错误的:对form1.setDownloadProgressText的调用是对来自不同异步线程的UI的调用.您需要将其包装在this.Invoke(如果是WinForms)或Dispatcher.Invoke(如果是WPF)中,以将UI操作编组到UI线程.

First thing I notice that's wrong: the call to form1.setDownloadProgressText is a call to the UI made from a different, asynchronous thread. You'll want to wrap that in a this.Invoke (if WinForms) or Dispatcher.Invoke (if WPF) to marshall the UI manipulation to the UI thread.

此外,在构造函数之前出现的成员变量声明列表中,"this"变量不可用,所以

Also, the "this" variable is not available in the list of member variable declarations that occur before the constructor function, so

    iUpdateDownloader_onProgressChanged progress = new iUpdateDownloader_onProgressChanged(this);
    IDownloadJob downloadJob = downloader.BeginDownload(progress, new       
    iUpdateDownloader_onCompleted(this), new iUpdateDownloader_state(this));

除非包装在构造函数或表单加载事件处理程序中,否则它将无法工作.

isn't going to work unless it's wrapped in a constructor or form load event handler, which isn't pictured here.

我的猜测是,在这里显示一个对象之前,您的onState对象已被调用,并且它正试图从UI之外的其他方向操纵该表单,并且这种爆炸正在防止其他对象(包括其中一个)

MY GUESS IS THAT YOUR onState OBJECT IS GETTING INVOKED BEFORE THE ONE SHOWN HERE, AND IT IS ATTEMPTING TO MANIPULATE THE FORM FROM A THREAD OTHER THAN THE UI ONE, AND THIS BLOW-UP IS PREVENTING THE OTHER OBJECTS (INCLUDING THE ONE DEPICTED HERE) FROM EVER BEING INVOKED.

这篇关于C#WUApiLib-异步下载并安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆