あなたはあなたの問題を解決し、解決に役立ついくつかの提案:
まず、私はあなたがresult.ToLower()
を返すようにしようとすることができると信じて、MD5
という名前のメソッドからstring
のデータ型、私は推測していますあなたは(つまり何も)、これを使用しようとする代わりに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();
}
をNOこと問題全体を解決するために、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メソッドの出力の一定だ後は再びボタンをクリックしてビルドします。私は、入力テキストボックスをハッシュするMD5メソッドを使用しています上記の状況で
private void btnStartHash_Click_(object sender, EventArgs e)
{
txtboxHashOne.Text = MD5(txtboxHashInput.Text);
string hashOfHash = MD5(txtboxHashOne.Text);
txtboxHashTwo.Text = hashOfHash;
}
を、 txtboxHashInput.Text
を入力し、フォームの変更を反映するようにテキストボックスをtxtboxHashOne
に変更します。 txboxHashOne
の文字列全体をハッシュして、hashOfHash
文字列を作成してください。
代わりに各インスタンス
txtboxHashOne
を持つの
、txtboxHashTwo
、1 はそれだけで、フォーム上のプログラムでテキストボックスを作成することによって、もっと良いと思うことがあります。
//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;
}
その後、プログラマを複雑な作業を減らすためにマルチスレッドのアプローチを採用しようとするかもしれません。
のSystem.InvalidOperationException:
//##!!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();
がもたらすであろう。例えば
、
が、残念ながらそれはこれを行うにはそれほど単純ではない "クロススレッド操作ではありません有効な:コントロール 'Form1' スレッド以外のスレッドからアクセスしましたに作成されました。 ' < <
あなたは本当にこのタスクを解決するために、マルチスレッドを必要とする場合に強く考えます。
Microsoftが示唆:
複数のスレッドを使用する
マルチスレッディングが大幅にあなたの応答性と使いやすさを向上させるために多くの一般的な状況で使用することができます応用。より詳細にこれらの使用を検討するのに便利です
#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
を介して通信
マイクロソフトでUsing Multiple Threadsにアクセスしてください。
また、Threading in Windows Formsをチェックアウトしたい場合があります。これには、知っておくべき大部分を実行できるという例があります。
うまくいけば、これがあなたが探していたものでした。
このコードは奇妙です、あなたは 'MD5()'を連続して呼び出し、それが終了するのを待ってから、それをやり直すスレッドを開始しようとしますか?とにかく、 'ThreadStart'は引数なしのデリゲートです(あなたが知っているように)、MD5()に入力文字列を与えたいとします。上の2行と同じ呼び出しを使用するには、 'Thread HashThread = new Thread(new ThreadStart(()=> MD5(MD5(txtboxHashOne.Text + txtboxHashTwo.Text))))))));' –
のようなスレッドを開始することができます。 "新しいスレッド(新しいParameterizedThreadStart(MD5)); **"スレッドHashThread =新しいスレッド(新しいThreadStart(MD5)); ** "**" **スレッドHashThread = ** "と私は同じエラーが発生しました。 – ToManyProblems
はい、 'ParameterizedThreadStart'は' MD5(文字列入力) 'のような' string'ではなく 'object'を取るためです。 –