问题描述
我有一个自定义操作,可以下载一个大文件(~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 (将#修改为@)