如何解决无法取消具有异步等待的 wix 安装自定义操作
我有一个自定义操作,可以下载一个大文件(~800MB);为了显示下载进度,我将此操作设为 async-await。
但是在 Wix UI 中点击“取消”时,只有在下载完整文件后,安装才会被取消。
有没有办法在用户取消安装后立即取消它?
CustomAction.cs
public class CustomActions
{
[CustomAction]
public static ActionResult DownloadImage(Session session)
{
InstallerLog.Log(session,"DownloadImage","Entry Point :",InstallerLog.LoggerLevel.Info);
string url = session["DOWNLOAD_URL"];
string fileName = Path.GetFileName(new UriBuilder(url).Path);
string downloadpath = "SomePath";
string downloadFile = Path.Combine(downloadpath,fileName);
try
{
DownloadImageFile DIF = new DownloadImageFile(session);
DIF.DownloadFile(url,downloadFile);
}
catch (Exception exc)
{
InstallerLog.Log(session,"Error: " + exc.Message,InstallerLog.LoggerLevel.Error);
session.Message(InstallMessage.Error,new Record { FormatString = exc.Message });
return ActionResult.Failure;
}
return ActionResult.Success;
}
class DownloadImageFile
{
private volatile bool _completed;
private Session session;
public DownloadImageFile(Session sess)
{
session = sess;
}
public void DownloadFile(string address,string location)
{
WebClient client = new WebClient();
Uri Uri = new Uri(address);
_completed = false;
InstallerLog.Log(session,"DownloadFile","Starting file download");
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
client.Headers.Add("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0)");
client.DownloadFileAsync(Uri,location);
while (client.IsBusy) { } // THIS BLOCKS MY THREAD
}
public bool DownloadCompleted { get { return _completed; } }
public static double prevIoUsBytes = 0;
private void DownloadProgress(object sender,DownloadProgressChangedEventArgs e)
{
if (((double)e.BytesReceived - prevIoUsBytes) / e.TotalBytesToReceive * 100 > 0.5)
{
prevIoUsBytes = e.BytesReceived;
InstallerLog.Log(session,"File downloaded: " + e.Progresspercentage + "%");
}
}
private void Completed(object sender,AsyncCompletedEventArgs e)
{
if (e.Cancelled == true) // THIS IS NEVER CALLED
{
InstallerLog.Log(session,"Download has been canceled");
}
else if (e.Error != null)
{
InstallerLog.Log(session,"Error encountered while downloading image file:" + e.Error.ToString());
}
else
{
InstallerLog.Log(session,"Download completed");
}
_completed = true;
}
}
我的 Product.wxs 的 CA 条目为:
<CustomAction Id="DownloadImageCA" Impersonate="yes" BinaryKey="PreConBin" DllEntry="DownloadImage" Return="check" Execute="immediate" />
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。