2017-12-20 10 views
0

私はいくつかの調査を行い、voidメソッドに適用するソリューションを見つけました。デリゲート 'ThreadStart'と一致する私のメソッド 'MD5'のオーバーロードがないので、私のコードをvoidのものに複製するために、voidを文字列に変換できませんでした。マルチスレッド化が複数のプロセスを可能にする方法を一度に行うことができます。私は別のスレッドに追加のプロセスを追加しようとしていますが、これが重要です。MD5ハッシュ文字列メソッドをマルチスレッドしようとしていますが、エラーが発生しました。CSC#デリゲート 'ThreadStart'に一致するオーバーロードがありません

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++; 
     } 
    } 
+0

このコードは奇妙です、あなたは 'MD5()'を連続して呼び出し、それが終了するのを待ってから、それをやり直すスレッドを開始しようとしますか?とにかく、 'ThreadStart'は引数なしのデリゲートです(あなたが知っているように)、MD5()に入力文字列を与えたいとします。上の2行と同じ呼び出しを使用するには、 'Thread HashThread = new Thread(new ThreadStart(()=> MD5(MD5(txtboxHashOne.Text + txtboxHashTwo.Text))))))));' –

+0

のようなスレッドを開始することができます。 "新しいスレッド(新しいParameterizedThreadStart(MD5)); **"スレッドHashThread =新しいスレッド(新しいThreadStart(MD5)); ** "**" **スレッドHashThread = ** "と私は同じエラーが発生しました。 – ToManyProblems

+0

はい、 'ParameterizedThreadStart'は' MD5(文字列入力) 'のような' string'ではなく 'object'を取るためです。 –

答えて

2

あなたはあなたの問題を解決し、解決に役立ついくつかの提案:

まず、私はあなたが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をチェックアウトしたい場合があります。これには、知っておくべき大部分を実行できるという例があります。

うまくいけば、これがあなたが探していたものでした。

関連する問題