{ "err_code": 200, "err_msg": "success", "version": "2.1.2", "download_url": "http://******/upgrade_package/Upgrade2.1.2.zip", "fingerprint": "d5af2da283ed4da97dbb1e445620669f" }
https://download.csdn.net/download/qq592691117/10623907
主要问题:没有使用线程,从而升级过程界面会假死。
使用线程方式,将过程放入 Upgrade() 方法中,在线程中调用,有点小优雅吧。
private void StartUpgrade() { Thread thread = new Thread(new ThreadStart(() => { try { Upgrade(); // writeLog("更新成功!"); ShowTip("更新成功!", Colors.Green); } catch (Exception ex) { common.log.LogTextHelper.WriteLine(ex.Message); ShowTip(ex.Message, Colors.Red); } finally { DeleteFile(); } Thread.Sleep(3000); this.Dispatcher.Invoke(new Action(() => { this.Close(); })); })); thread.Start(); }
进度条UI的更新(调用此方法在线程中更新)
private void AddPercent() { this.Dispatcher.Invoke(new Action(() => { pgbUpdate.Value++; })); }
不允许同时打开多次(在App.xaml.cs中)
public partial class App : Application { System.Threading.Mutex mutex; //重写OnStartup,获得启动程序 protected override void OnStartup(StartupEventArgs e) { bool ret; mutex = new System.Threading.Mutex(true, "AutoUpdate", out ret); if (!ret) // 不可多次打开 { Environment.Exit(0); } else { if (e.Args != null && e.Args.Count() > 0) { var lastVersion = e.Args[0]; // 取得传递的版本号 } base.OnStartup(e); } } }
取得文件的 MD5 值
public class FileHelper { static public string GetMD5WithFilePath(string filePath) { using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] hash_byte = md5.ComputeHash(file); string str = System.BitConverter.ToString(hash_byte); str = str.Replace("-", "").ToLower(); return str; } } }
private void ShowTip(string tip, Color color) { this.Dispatcher.Invoke(new Action(() => { lbl_name.Foreground = new SolidColorBrush(color); lbl_name.Content = tip; })); }
更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。
长按或扫描下方二维码,免费获取 Python公开课和大佬打包整理的几百G的学习资料,内容包含但不限于Python电子书、教程、项目接单、源码等等
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!