C# 它完全冻结,不响应输入,也不重新绘制自身。使用BackgroundWorker运行GetFileMD5Hash()方法。您的函数GetFileMD5Hash(openFileDialog1.FileName)正在阻止一个。您应该在另一个线程中运行它。您需要
C# 它完全冻结,不响应输入,也不重新绘制自身。使用BackgroundWorker运行GetFileMD5Hash()方法。您的函数GetFileMD5Hash(openFileDialog1.FileName)正在阻止一个。您应该在另一个线程中运行它。您需要,c#,progress-bar,C#,Progress Bar,它完全冻结,不响应输入,也不重新绘制自身。使用BackgroundWorker运行GetFileMD5Hash()方法。您的函数GetFileMD5Hash(openFileDialog1.FileName)正在阻止一个。您应该在另一个线程中运行它。您需要将密集的工作转移到一个单独的线程,这样UI线程就可以自由地管理UI。调查 using System; using System.Collections.Generic; using System.ComponentModel; using Sy
它完全冻结,不响应输入,也不重新绘制自身。使用BackgroundWorker运行GetFileMD5Hash()方法。您的函数
GetFileMD5Hash(openFileDialog1.FileName)
正在阻止一个。您应该在另一个线程中运行它。您需要将密集的工作转移到一个单独的线程,这样UI线程就可以自由地管理UI。调查
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calcula_MD5_BAR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Stopwatch timer = new Stopwatch();
var openFileDialog1 = new OpenFileDialog();
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 30;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
timer.Start();
textBox1.Text = GetFileMD5Hash(openFileDialog1.FileName);
timer.Stop();
lblTime.Text = timer.Elapsed.ToString();
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.Increment(100);
}
progressBar1.MarqueeAnimationSpeed = 0;
}
protected string GetFileMD5Hash(string nomeArquivo)
{
FileStream file = new FileStream(nomeArquivo, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Shown += new EventHandler(Form1_Shown);
// To report progress from the background worker we need to set this property
backgroundWorker1.WorkerReportsProgress = true;
// This event will be raised on the worker thread when the worker starts
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
// This event will be raised when we call ReportProgress
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
void Form1_Shown(object sender, EventArgs e)
{
// Start the background worker
backgroundWorker1.RunWorkerAsync();
}
// On worker thread so do our thing!
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Your background task goes here
for (int i = 0; i <= 100; i++)
{
// Report progress to 'UI' thread
backgroundWorker1.ReportProgress(i);
// Simulate long task
System.Threading.Thread.Sleep(100);
}
}
// Back on the 'UI' thread so we can update the progress bar
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// The progress percentage is a property of e
progressBar1.Value = e.ProgressPercentage;
}
}
private async void button1_Click(object sender, EventArgs e)
{
Stopwatch timer = new Stopwatch();
var openFileDialog1 = new OpenFileDialog();
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 30;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
timer.Start();
textBox1.Text = await Task.Run(() =>
GetFileMD5Hash(openFileDialog1.FileName));
timer.Stop();
lblTime.Text = timer.Elapsed.ToString();
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.Increment(100);
}
progressBar1.MarqueeAnimationSpeed = 0;
}