我做了一些研究并找到了适用于void方法的解决方案,但是我无法将我的代码复制到void的代码,因为我的方法'MD5'没有与代理'ThreadStart'匹配的重载,而我我们无法将void转换为字符串,这个程序的目的是展示多线程如何允许一次完成多个进程。我打算在不同的线程上添加其他进程,但是,这很有用。
using System.Security.Cryptography;//Added to allow for UTF8 encoding
using System.Threading;//Added to allow for multi-threading
namespace MTSTask5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//MD5 Hash method
public void MD5(string input)
{
MD5 md5 = new MD5CryptoServiceProvider();
//Convert the input string to a byte array and computer the hash, return the hexadecimal
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
string result = BitConverter.ToString(bytes).Replace("-", string.Empty);
return result.ToLower();
}
private void btnStartHash_Click(object sender, EventArgs e)
{
int loopQty = Int32.Parse(txtboxLoopQty.Text);
int i = 0;
//Create a while loop for the MD5 method below
while (i < loopQty)
{
//loop output
string HashOutput = MD5(MD5(txtboxHashOne.Text + txtboxHashTwo.Text));
txtboxHashOutput.Text = HashOutput + " " + i;
Thread HashThread = new Thread(new ThreadStart(MD5));
HashThread.Start();
i++;
}
}
一些可能有助于您排除故障并解决问题的建议:
首先,我相信你可能会尝试从你的方法result.ToLower()
返回string
数据类型MD5
,我猜你试图使用它而不是返回void
,(即什么都没有):
//MD5 Hash method
public string MD5(string input)
{
MD5 md5 = new MD5CryptoServiceProvider();
//Convert the input string to a byte array and computer the hash, return the hexadecimal
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
string result = BitConverter.ToString(bytes).Replace("-", string.Empty);
return result.ToLower();
}
这可能不是整个问题,所以让我们检查以确保您的方法正常工作,将您在btnStartHash_Click
方法中的代码复制到一个安全的地方,然后用一个简单的消息替换它给自己。
private void btnStartHash_Click(object sender, EventArgs e)
{
//Convert the input string to a byte array and computer the hash, return the hexadecimal and display it in a message box
MessageBox.Show(MD5("abcdefg"));//parse whatever known value test
}
如果您仍然不确定MD5方法的哈希结果,那么请逐个开始分类。
一旦确定所需的MD5方法输出,再次构建按钮再次单击:
private void btnStartHash_Click_(object sender, EventArgs e)
{
txtboxHashOne.Text = MD5(txtboxHashInput.Text);
string hashOfHash = MD5(txtboxHashOne.Text);
txtboxHashTwo.Text = hashOfHash;
}
在上面的情况下,我使用MD5方法散列输入文本框txtboxHashInput.Text
,然后更改txtboxHashOne
文本框以反映表单上的更改。然后对txboxHashOne
的整个字符串进行哈希处理以确保制作hashOfHash
字符串。
而不是让每个实例txtboxHashOne
,txtboxHashTwo
,人们可能认为只需在表单上以编程方式创建文本框就可以做得更好:
//lets say the loopqty input is the number of times I wanted to hash this
int numberOfTimesToHash = Int32.Parse(txtboxLoopQty.Text);
//x and y represent where you want them to start appearing on your form..
int x = 10;
int y = 100;
int howeverManyThreadsIWant = numberOfTimesToHash;
for (int i = 0; i < howeverManyThreadsIWant; i++)
{
TextBox textBox = new TextBox();
textBox.Location = new Point(x, y);
//Could go into a recursive function such as` MD5(Input,recursionDepth)
//But instead going to reprint same hash for demonstration purposes
textBox.Text = MD5(txtboxHashInput.Text);
//MessageBox.Show(textBox.Text);
this.Controls.Add(textBox);
y += 30;
}
然后,程序员可能想尝试采用多线程方法来降低复杂性,我们必须做更多事情。
例如,不幸的是,这样做并不是那么简单:
//##!!Don't do this!!
var thread = new Thread(() =>
{
int x = 10;
int y = 100;
int howeverManyThreadsIWant = numberOfTimesToHash;
for (int i = 0; i < howeverManyThreadsIWant; i++)
{
TextBox textBox = new TextBox();
textBox.Tag = i;
textBox.Location = new Point(x, y);
//Could go into a recursive function such as MD5(Input,recursionDepth)
//But instead going to simply reprint same hash
textBox.Text = MD5(txtboxHashInput.Text);
//MessageBox.Show(textBox.Text);
this.Controls.Add(textBox);//<--invalid operations error
y += 30;
}
});
thread.Start();
会导致:
System.InvalidOperationException:'跨线程操作无效:控制'Form1'从其创建的线程以外的线程访问。'<<
强烈考虑是否真的需要多线程来解决这个任务。
微软建议:
何时使用多个线程
多线程可用于许多常见情况,以显着提高应用程序的响应能力和可用性。
您应该强烈考虑使用多个线程:
#Communicate over a network, for example to a Web server, database, or remote object.
#Perform time-consuming local operations that would cause the UI to freeze.
#Distinguish tasks of varying priority.
#Improve the performance of application startup and initialization.
更详细地研究这些用途是有用的。
通过网络进行通信
#Smart-clients may communicate over a network in a number of ways, including:
#Remote object calls, such as DCOM, RPC or .NET remoting
#Message-based communications, such as Web service calls and HTTP requests
#Distributed transactions
考虑到这一点,如果你真的需要做这些事情,现在你有无错误的代码以你想要的方式进行散列,请访问Microsoft的Using Multiple Threads。
您也可以查看Threading in Windows Forms,这里有一个例子,您可以运行大部分需要知道的内容。
希望其中一些是您正在寻找的。