2017-12-20 3 views
0

일부 연구를 수행했으며 void 메서드에 적용되는 솔루션을 찾았습니다. 대리자 'ThreadStart'과 일치하는 내 메서드 'MD5'에 과부하가 없으므로 코드를 무효로 복제하려면 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()에 입력 문자열을주고 싶습니다. 위의 두 라인과 같은 호출을 사용하려면'Thread HashThread = new Thread (new ThreadStart (() => MD5 (MD5 (txtboxHashOne.Text + txtboxHashTwo.Text))))))))); ' –

+0

과 같은 스레드를 시작할 수 있습니다. ** Thread HashThread = new Thread (new ParameterizedThreadStart (MD5)); ** "와 같은 에러로 만났습니다. ** Thread HashThread = new Thread (new ThreadStart (MD5)); – 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(); 
} 

그 않을 수 없습니다 전체적인 문제가 없으므로 btnStartHash_Click 메소드에있는 코드를 안전한 장소에 복사 한 다음 간단한 메시지로 바꾸어 메소드가 제대로 작동하는지 확인합시다. 당신은 여전히 ​​당신의 MD5 방식에서 해시 결과의 확실하지 않은 경우


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 
} 
다음 하나 하나에서 부품을 복용 시작합니다. 위의 상황에서

private void btnStartHash_Click_(object sender, EventArgs e) 
{ 
    txtboxHashOne.Text = MD5(txtboxHashInput.Text); 
    string hashOfHash = MD5(txtboxHashOne.Text); 
    txtboxHashTwo.Text = hashOfHash; 
} 

내가 입력 텍스트 상자를 해시하는 MD5 방법을 사용하고 있습니다 : 당신이 원하는 MD5 방식의 출력의 특정있어 일단


다시 버튼을 클릭 빌드가 txtboxHashInput.Text 다음 폼의 변경 내용을 반영하도록 txtboxHashOne 텍스트 상자를 변경하십시오. 그런 다음 txboxHashOne의 전체 문자열을 해시하여 hashOfHash 문자열을 만듭니다.

//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; 
} 
다음

, 프로그래머 :


대신 각 인스턴스 txtboxHashOne, txtboxHashTwo을 필요없이 하나의 그냥 양식에 프로그래밍 방식으로 텍스트 상자를 만들어 더 잘 할 수 있다고 생각 할 수있다 복잡성을 줄이기 위해 멀티 스레드 접근 방식을 채택하려는 시도가 필요할 수 있습니다.

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 

를 통해 통신

원하는대로 Microsoft Using Multiple Threads을 방문하십시오.

또한 Threading in Windows Forms을 확인하고 싶을 수도 있습니다.이 예제는 사용자가 알아야 할 대부분의 것을 실행할 수있는 예입니다.

잘하면이 중 일부는 당신이 찾고 있었으면합니다.