刚刚接触C#,想做一个校验文件MD5和SHA1值的程序,可是文件过大的时候,程序会出现一段时间的无响应,过一会就好了,怎么解决啊,求助……using System;
using System.IO;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Security.Cryptography;namespace FileCalibration
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            textBox1.Text = "";
            textBox3.Text = "";
            textBox5.Text = "";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                TimeSpan timeSpan1 = new TimeSpan(DateTime.Now.Ticks);
                progressBar1.Value = 10;
                String fileName = dialog.FileName;
                progressBar1.Value = 20;
                setFileInfo(fileName);
                textBox1.Text = getMD5(fileName);
                textBox3.Text = getSHA1(fileName);
                TimeSpan timeSpan2 = new TimeSpan(DateTime.Now.Ticks);
                String totalSeconds = timeSpan2.Subtract(timeSpan1).TotalSeconds.ToString(); //计算整个程序的运行时间
                String[] timeSeconds = totalSeconds.Split('.');
                toolStripLabel2.Text = timeSeconds[0] + "秒" + timeSeconds[1].Substring(0, 3) + "毫秒";
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "您可以将文件直接拖入这里...";
            progressBar1.Value = 0;
        }        //取得文件信息
        private void setFileInfo(String fileName)
        {
            textBox5.Text = "";  
            FileInfo fi = new FileInfo(fileName);
            textBox5.Text += "文件名称:" + fi.Name + "\r\n";
            if (fi.Length / 1024.00 < 1024.00)
                textBox5.Text += "文件大小:" + (fi.Length / 1024.00).ToString("0.00") + "K (" + fi.Length + "字节 )\r\n";
            else if (fi.Length / 1024.00 / 1024.00 < 1024.00)
                textBox5.Text += "文件大小:" + (fi.Length / 1024.00 / 1024.00).ToString("0.00") + "M (" + fi.Length + "字节 )\r\n";
            else if (fi.Length / 1024.00 / 1024.00 /1024.00 < 1024.00)
                textBox5.Text += "文件大小:" + (fi.Length / 1024.00 / 1024.00 / 1024.00).ToString("0.00") + "G (" + fi.Length + "字节 )\r\n";
            textBox5.Text += "创建时间:" + fi.CreationTime + "\r\n";
            textBox5.Text += "访问时间:" + fi.LastAccessTime + "\r\n";
            textBox5.Text += "修改时间:" + fi.LastWriteTime + "\r\n\r\n";
            textBox5.Text += "文件路径:" + fi.FullName.Replace(fi.Name, "") + "\r\n";
           
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fileName);
            textBox5.Text += "文件版本:" + fvi.FileVersion + "\r\n";
            textBox5.Text += "文件描述:" + fvi.FileDescription + "\r\n";
            textBox5.Text += "公司名称:" + fvi.CompanyName + "\r\n";
            textBox5.Text += "版权声明:" + fvi.LegalCopyright;
            progressBar1.Value = 30;
        }        //计算文件的MD5码
        private String getMD5(String pathName)
        {
            FileStream fileStream = null;
            String MD5String = null;
            Byte[] MD5byte = null;
            MD5 md5 = new MD5CryptoServiceProvider();
            try
            {
                fileStream = new FileStream(pathName, FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
                progressBar1.Value = 40;
                MD5byte = md5.ComputeHash(fileStream);//计算指定Stream 对象的哈希值
                progressBar1.Value = 50;
                fileStream.Close();
                //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”
                MD5String = BitConverter.ToString(MD5byte);
                //替换-
                MD5String = MD5String.Replace("-", "");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            progressBar1.Value = 60;
            return MD5String;
        }        //计算文件的SHA1码
        private String getSHA1(String pathName)
        {
            FileStream fileStream = null;
            String SHA1String = null;
            Byte[] SHA1byte = null;
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            try
            {
                progressBar1.Value = 70;
                fileStream = new FileStream(pathName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                progressBar1.Value = 80;
                SHA1byte = sha1.ComputeHash(fileStream);//计算指定Stream 对象的哈希值
                progressBar1.Value = 90;
                fileStream.Close();
                //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”
                SHA1String = BitConverter.ToString(SHA1byte);
                //替换-
                SHA1String = SHA1String.Replace("-", "");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            progressBar1.Value = 100;
            return SHA1String;
        }        private void textBox5_DragDrop(object sender, DragEventArgs e)
        {
            TimeSpan timeSpan1 = new TimeSpan(DateTime.Now.Ticks);
            textBox5.Text = "正在计算MD5和SHA1值,请稍等……";
            String fileName = ((String[])e.Data.GetData(DataFormats.FileDrop))[0];
            progressBar1.Value = 20;
            setFileInfo(fileName);
            textBox1.Text = getMD5(fileName);
            textBox3.Text = getSHA1(fileName);
            TimeSpan timeSpan2 = new TimeSpan(DateTime.Now.Ticks);
            String totalSeconds = timeSpan2.Subtract(timeSpan1).TotalSeconds.ToString(); //计算整个程序的运行时间
            String[] timeSeconds = totalSeconds.Split('.');
            toolStripLabel2.Text = timeSeconds[0] + "秒" + timeSeconds[1].Substring(0,3) + "毫秒";
        }        private void textBox5_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text.Length == 32)
            {
                if (textBox1.Text.Equals(textBox2.Text.ToUpper()))
                {
                    textBox2.ForeColor = Color.Lime;
                }
                else
                {
                    textBox2.ForeColor = Color.Red;
                }
            }
            else
            {
                textBox2.ForeColor = Color.Black;
            }
        }        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (textBox4.Text.Length == 40)
            {
                if (textBox3.Text.Equals(textBox4.Text.ToUpper()))
                {
                    textBox4.ForeColor = Color.Lime;
                }
                else
                {
                    textBox4.ForeColor = Color.Red;
                }
            }
            else
            {
                textBox4.ForeColor = Color.Black;
            }
        }        private void button3_Click(object sender, EventArgs e)
        {
            new AboutBox1().ShowDialog();
        }
    }
}

解决方案 »

  1.   

    ThreadStart start = new ThreadStart(DoSomething);
    Thread thread = new Thread(start);
    thread.Start();private void DoSomething()
    {
        //耗时操作
    }
      

  2.   


    问题就是我一旦这么用了 我计算的结果没有办法放到textbox里面,不然就出现什么线程控件的错误。怎么解决啊,拜托了
      

  3.   

    http://blog.csdn.net/love_beibei/archive/2010/10/27/5969568.aspx
      

  4.   

    Form.CheckForIllegalCrossThreadCalls = false;
    放在构造函数里面
      

  5.   

    ThreadStart start = new ThreadStart(DoSomething1);
    Thread thread = new Thread(start);
    thread.Start();        private void DoSomething()
    {
         //耗时操作
          //在另外一个线程不能直接调用主线程的控件,这么写
         setTextSafe(this.TextBox1, "abc");
    }
    private delegate void setValue(TextBox ctrl, object value);
            private void setTextSafe(TextBox ctrl, object value)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new setValue(setTextSafe), ctrl, value);
                }
                else
                {
                    ctrl.AppendText((string)value);
                }
            }
      

  6.   

    我一直想知道 :
    ThreadStart start = new ThreadStart(DoSomething1);
    Thread thread = new Thread(start);
    thread.Start();       和
    Thread thread = new Thread(DoSomething1);
    thread.Start();  有什么区别?