private void ThreadStart() { Thread MyThread; SetProcess = new UpdateProcessDelegate(UpdateProcess); MyThread = new Thread(new ThreadStart(WarnErrThreadMethod)); MyThread.Start(); }
private void DownLoad() { try { if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string fileName = url.Substring(url.LastIndexOf('/') + 1, url.Length - url.LastIndexOf('/') - 1); FileStream fs = new FileStream(directory + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); byte[] mybyte = new byte[128 * 200];//128byte = 1kb 200kb/s int count = stream.Read(mybyte, 0, mybyte.Length); currentLength += count; fs.Seek(0, SeekOrigin.Begin); while (count > 0) { fs.Write(mybyte, 0, count); count = stream.Read(mybyte, 0, mybyte.Length); currentLength += count; Thread.Sleep(1); UpdateProcessDelegate d = new UpdateProcessDelegate(UpdateStatus); this.BeginInvoke(d, new object[] { 100 * currentLength / contentLength }); } stream.Close(); fs.Close(); this.BeginInvoke((ThreadStart) delegate() { this.lbl_Status.Text = "下载完成"; Thread.Sleep(500); this.lbl_Status.Text = "正在解压..."; }); if (Path.GetExtension(fileName).Equals(".zip")) { this.UnZipFile(directory + fileName); File.Delete(directory + fileName); Directory.Delete(directory, true); } Thread.Sleep(500); this.isDownLoading = false; this.BeginInvoke((ThreadStart) delegate() { LocalConfig.SetConfigValue("ver", ver); this.lbl_Status.Text = "完成更新"; this.btn_Complete.Enabled = true; this.btn_Update.Enabled = true; }); //MessageBox.Show("更新完成!"); } catch (Exception ex) { this.isDownLoading = false; this.BeginInvoke((ThreadStart) delegate() { this.lbl_Status.Text = "更新失败"; this.btn_Complete.Enabled = true; }); MessageBox.Show(ex.Message, "提示信息"); this.Close(); } }