-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
73 lines (65 loc) · 2.23 KB
/
Form1.cs
File metadata and controls
73 lines (65 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsTemplate
{
public partial class Form1 : Form
{
// Delegate to Update TextBox1
private delegate void UpdateTextBox1Handler(string msg);
// Delegate to Update TextBox2
private delegate void UpdateTextBox2Handler(string msg);
public Form1()
{
InitializeComponent();
// File to be placed in the same folder as the executable
string filename = "test.txt";
Thread MD5HashingThread = new Thread(() => ComputeMD5Hash(filename));
MD5HashingThread.Start();
}
public void UpdateTextBox1(string text1)
{
if (InvokeRequired)
BeginInvoke(new UpdateTextBox1Handler(UpdateTextBox1), new object[] { text1 });
else
textBox1.Text = text1;
}
public void UpdateTextBox2(string text2)
{
if (InvokeRequired)
BeginInvoke(new UpdateTextBox2Handler(UpdateTextBox2), new object[] { text2 });
else
textBox2.Text = text2;
}
private void ComputeMD5Hash(string filename)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filename))
{
var hash = md5.ComputeHash(stream);
UpdateTextBox1(BitConverter.ToString(hash).Replace("-", ""));
//return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
stopWatch.Stop();
// Ticks/Frequency gives the seconds
string elapsedtime = ((double)stopWatch.ElapsedTicks / Stopwatch.Frequency).ToString() + " sec";
UpdateTextBox2(elapsedtime);
// To gracefully end the thread
return;
}
}
}