- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以基本上我的下载文件是:
public void DownloadFile()
{
settings_btn.Enabled = false;
label1.Text = "Checking for updates...";
//Defines the server's update directory
string Server = "http://downloadurl/update/";
//Defines application root
string Root = AppDomain.CurrentDomain.BaseDirectory;
//Make sure version file exists
FileStream fs = null;
if (!File.Exists("pversion"))
{
using (fs = File.Create("pversion")){}
using (StreamWriter sw = new StreamWriter("pversion")){sw.Write("1.0");}
}
//checks client version
string lclVersion;
using (StreamReader reader = new StreamReader("pversion"))
{
lclVersion = reader.ReadLine();
}
decimal localVersion = decimal.Parse(lclVersion);
//server's list of updates
XDocument serverXml = XDocument.Load(@Server + "pUpdates.xml");
//The Update Process
foreach (XElement update in serverXml.Descendants("pupdate"))
{
string version = update.Element("pversion").Value;
string file = update.Element("pfile").Value;
decimal serverVersion = decimal.Parse(version);
string sUrlToReadFileFrom = Server + file;
string sFilePathToWriteFileTo = Root + file;
if (serverVersion > localVersion)
{
using (webClient = new WebClient())
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
// The variable that will be holding the url address (making sure it starts with http://)
Uri url = new Uri(sUrlToReadFileFrom);
// Start the stopwatch which we will be using to calculate the download speed
sw.Start();
try
{
// Start downloading the file
webClient.DownloadFileAsync(url, sFilePathToWriteFileTo);
// Change the currently running executable so it can be overwritten.
Process thisprocess = Process.GetCurrentProcess();
string me = thisprocess.MainModule.FileName;
string bak = me + ".bak";
if (File.Exists(bak))
{
File.Delete(bak);
}
File.Move(me, bak);
File.Copy(bak, me);
//unzip
using (ZipFile zip = ZipFile.Read(file))
{
foreach (ZipEntry zipFiles in zip)
{
zipFiles.Extract(Root + "\\", true);
}
}
//download new version file
webClient.DownloadFile(Server + "pversion.txt", @Root + "pversion");
//Delete Zip File
deleteFile(file);
var spawn = Process.Start(me);
thisprocess.CloseMainWindow();
thisprocess.Close();
thisprocess.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
我的问题是,一旦它找到新版本并开始下载文件 webClient.DownloadFileAsync(url, sFilePathToWriteFileTo);
,它会立即运行下面的代码,即更改名称、解压缩和下载新版本文件进度
我想让它做的是等待它完成文件下载,然后再做剩下的事情。我该怎么做?
-- 如果有必要,ProgressChanged:
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
label3.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00")) + " " + string.Format("{0} MB's / {1} MB's", (e.BytesReceived / 1024d / 1024d).ToString("0.00"), (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));;
progressBar1.Value = e.ProgressPercentage;
label1.Text = e.ProgressPercentage.ToString() + "%";
}
并完成:
private void Completed(object sender, AsyncCompletedEventArgs e)
{
sw.Reset();
if (e.Cancelled == true)
{
label1.Text = "Download cancelled!";
}
else
{
label1.Text = "Download completed!";
}
}
最佳答案
您可以使用 DownloadFile 方法。 Async 一词表示此方法将异步运行(在其他线程中),这就是为什么它几乎立即转到下一行的原因。如果您想等待下载结束,请使用 DownloadFile 而不是 DownloadFileAsync
关于c# - 等待 DownloadFileAsync 完成下载,然后执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30226734/
我正在使用 C# 中的 WebClient 下载文件。我正在使用 client.DownloadFileAsnyc()。在过去这工作得很好,任何异常都会被捕获并在完成处理程序中返回 (AsyncCom
我正在制作一个 WPF 应用程序。我使用 WebClient 的 DownloadFileAsync 下载文件。我一次从文件夹中下载每个文件。我第一次调用 DownloadProtocol 它工作正常
我正在尝试编写一个简单的应用程序,它可以从网络上下载一个文件。 class Program { static void Main(string[] args) { We
我需要使用 WebClient 下载文件在 PowerShell 2.0 中,我想显示下载进度,所以我这样做了: $activity = "Downloading" $client = New-Obj
WebClient DownloadFileAsync() 不适用于相同的 URl 和凭据... 有什么线索吗? static void Main(string[] args) {
我正在尝试使用 WPF 和 MVVM 从我的网络服务器下载一个大文件 (500 mb)。因此,以下属性都绑定(bind)到某种控件(进度条)。问题是,应用程序仍然挂起,即使在使用 DownloadFi
DownloadFileAsync 和 DownloadFileTaskAsync 有什么区别? 什么时候应该使用一个而不是另一个?任何例子将不胜感激。 最佳答案 一般模式 - 如果您发现两个名称以
我正在尝试通过以下方式在我的服务器上下载一个 zip 文件 WebClient.DownloadFileAsync(new Uri(DownloadLink),
正如您在代码中间看到的那样,有一个丑陋的线程阻塞代码等待文件下载并在命令行中启用进度报告。我的意思是,它仍然比 Thread.Sleep() 或忙等待要好,对吧?不管怎样,我知道 Wait/Pulse
这几乎就是标题中的全部问题。我有一个 WPF C# Windows 应用程序,我为用户下载文件,现在想显示速度。 最佳答案 mWebClient.DownloadProgressChanged +=
美好的一天。我正在使用 DownloadFileAsync 处理文件下载器类。在正常情况下,一切正常。但是当我下载文件并禁用网络连接时,下载进度会无限期停止,不会引发任何错误或调用任何回调。任何想法如
我正在使用下面的代码从 TFS 服务器下载多个附件: foreach (Attachment a in wi.Attachments) { WebClient wc = new WebC
我正在使用 WebClient 的 DownloadFileAsync 方法从服务器下载一些文件,在我对代码的非正式测试中我不禁注意到在 VS2010 中,它在启动时阻塞了大约 3 秒,在我看来,这首
所以我认为 Webclient.DownloadFileAysnc 会有一个默认超时,但是查看文档我无法在任何地方找到任何关于它的信息,所以我猜它没有。 我正在尝试像这样从 Internet 下载文件
描述 使用 webclient 的 DownloadFileAsync 下载多个文件,并利用文本文件输入 URL 进行下载。 问题 我使用的方法根本不会下载文件。只是运行,什么也不做。它填充列表数组然
安全取消 DownloadFileAsync 操作的最佳方法是什么? 我有一个线程(后台 worker )开始下载并管理它的其他方面,当我看到线程有 CancellationPending == tr
所以我正在使用 webClient.DownloadFileAsync(url, sFilePathToWriteFileTo); 下载文件 网址是string url = "http://local
所以基本上我的下载文件是: public void DownloadFile() { settings_btn.Enabled = false; label1.Text = "Chec
我正在使用 WebClient 按以下方式从 Internet 下载一些文件: try { ManualResetEvent mr = new ManualResetEvent(false);
我有此 C# 代码,但最终的 esi.zip 结果为 0 长度或基本上为空。该 URL 确实存在并确认手动下载文件。我很困惑买这个。 string zipPath = @"C:\download\es
我是一名优秀的程序员,十分优秀!