无为清净楼资源网 Design By www.qnjia.com

主要对 多线程更新 winform 不是特别清楚,绕来绕去,搞得很晕乎,主要代码如下, 还请各位大侠多多指点,谢谢!

复制代码 代码如下:
using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.Security.Cryptography;
 using System.IO;
 using System.Threading;

 namespace FileMD5 {
     public partial class MainForm : Form {
         public MainForm() {
             InitializeComponent();
         }

         private void button_file_Click(object sender, EventArgs e) {
             OpenFileDialog fileDialog = new OpenFileDialog();
             fileDialog.Title = "请选择文件";
             fileDialog.RestoreDirectory = true;

             if (fileDialog.ShowDialog() == DialogResult.OK) {
                 textBox_file.Text = fileDialog.FileName;
                 textBox_result.Text = "";
                 FileInfo file = new FileInfo(fileDialog.FileName);
                 fileSzie = file.Length;
                 showFilesize(fileSzie);
             }

         }

         private void button_exit_Click(object sender, EventArgs e) {
             this.Close();
             this.Dispose();
         }

         private void button_check_Click(object sender, EventArgs e) {
             checkResult();
         }

         MD5 md5 = (MD5)CryptoConfig.CreateFromName("MD5");
         long fileSzie = 0;

         private void button_calc_Click(object sender, EventArgs e) {
             string file = textBox_file.Text;

             if (file.Length == 0) {
                 textBox_result.Text = "请先重新选择文件!";
                 return;
             }

             FileStream fs = null;
             try {
                 fs = new FileStream(file, FileMode.Open, FileAccess.Read);
             } catch (SystemException) {
                 textBox_result.Text = "文件打开错误,请重新选择文件!";
                 return;
             }

             //对于大于 100M 的文件启用多线程
             if (fs.Length > 100L * 1024 * 1024) {

                 string message = "文件已经超过 100M ,需要较长的计算时间。\n软件将启动后台线程进行处理。是否继续?";
                 string caption = "文件较大";
                 MessageBoxButtons buttons = MessageBoxButtons.YesNo;

                 if (MessageBox.Show(message, caption, buttons) == System.Windows.Forms.DialogResult.No) {
                     fs.Close();
                     textBox_result.Text = "文件较大,未计算。";
                     return;
                 }
                 textBox_result.Text = "正在计算中,请稍候......";
                 button_calc.Enabled = false;
                 button_file.Enabled = false;

                 Thread thread = new Thread(new ParameterizedThreadStart(calcMD5));
                 thread.Start(fs);

             } else {
                 calcMD5(fs);
             }
         }

         //建立一个 object 参数的函数,是为了处理线程调用中,使用参数的问题。
         private void calcMD5(object fs) {
             calcMD5((FileStream)fs);
         }

         // Invoke 函数需要使用的委托
         delegate void updateWindows(byte[] result);

         private void calcMD5(FileStream fs) {
             byte[] md5byte = md5.ComputeHash(fs);

             if (this.InvokeRequired) {
                 this.Invoke(new updateWindows(showResult), md5byte);
             } else {
                 showResult(md5byte);
             }
             fs.Close();
         }

         private void showResult(byte[] md5byte) {
             int i, j;
             StringBuilder sb = new StringBuilder(32);
             foreach (byte b in md5byte) {
                 i = Convert.ToInt32(b);
                 j = i 4;
                 sb.Append(Convert.ToString(j, 16));
                 j = ((i << 4) & 0x00ff) 4;
                 sb.Append(Convert.ToString(j, 16));
             }

             String result = sb.ToString().ToUpper();

             textBox_result.Text = result;
             button_calc.Enabled = true;
             button_file.Enabled = true;
             checkResult();

         }

         private void checkResult() {

             string result = textBox_result.Text;

             if (textBox_md5.Text.Length == 0) {
                 textBox_compare.Text = "";
                 textBox_compare.Visible = false;
                 return;
             }

             if(result.Length != 32 ) {
                 textBox_compare.Visible = true;
                 textBox_compare.BackColor = Color.Pink;
                 textBox_compare.Text = "计算结果框中不是MD5码,请先进行计算!";
                 return;
             }

             if (textBox_md5.Text.Trim().ToUpper().Equals(result.ToUpper())) {
                 textBox_compare.Visible = true;
                 textBox_compare.BackColor = Color.LightGreen;
                 textBox_compare.Text = "MD5码 已匹配,文件未被修改,可放心使用!";
             } else {
                 textBox_compare.Visible = true;
                 textBox_compare.BackColor = Color.Red;
                 textBox_compare.Text = "MD5码 不匹配,文件已被修改,请小心!";
             }
         }

         private void showFilesize(long size) {

             float d_size;
             string unit = "Byte";

             if (size > 1024 * 1024 * 1024) {    //大于 1G 的显示
                 d_size = size / (float)(1024 * 1024 * 1024);
                 unit = "GB";
             } else {
                 if (size > 1024 * 1024) {    //大于 1M 的显示
                     d_size = size / (float)(1024 * 1024);
                     unit = "MB";
                 } else {
                     if (size > 1024) {    //大于 1K 的显示
                         d_size = size / (float)(1024);
                         unit = "KB";
                     } else {
                         d_size = size;
                     }
                 }
             }
             textBox_filesize.Text = string.Format(" {0:F} {1} ( {2:N0}字节 )", d_size, unit, size);
         }
     }
 }

完整的 VS2010 项目下载:http://xiazai.jb51.net/201302/yuanma/FileMD5_jb51.net.rar

标签:
MD5码

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。