2017-11-16 12 views
-2

5つのスレッドを同時に実行しているプログラムを作成しました。これらの5つのスレッドはそれぞれ、合計で約3時間続くことができる複数の時間のかかる操作を実行します。i5 CPUで4つ以上のスレッドを実行することはできませんか?

2時間前にUIが正常に動作しているようです。しかし、私は約2時間が経過した後、UIが応答しなくなり、時間が遅れることを発見しました。 さらに、5番目のスレッドの結果が間違ったり、偽の値になったりします。私は自分のコーディングを最適化するために最善を尽くしましたが、これはまだ起こります。

私の会社のコンピュータには、i5-6500 CPUと8 GBのRAMがあります。私は、このCPUが一度に4つのスレッドしか実行できないことをオンラインで確認しました。 5つ以上のスレッドを実行するには、ハイパースレッディングが必要なi7-7700が必要です。これは一度に8つ実行できます。

このハードウェアの制限については正しいですか?または、この問題を克服するために私のC#コーディングで何かできることはありますか?参考までに、私はマルチスレッドを処理するためにBackgroundWorkerを使用しています。

上司に新しいCPUとマザーボードに投資しなければならないことを提案する前に、ハードウェアの問題であることを確認する必要があります。

アドバイスをいただければ幸いです。 :)

private void Form1_Load(object sender, EventArgs e) 
{ 
    using (StreamReader sr = new StreamReader(string.Concat(Directory.GetCurrentDirectory(), "\\limit.txt"), true)) 
     { 
      String input; //read value limits from textfile and storing it in array 
      int i = 1; 
      while ((input = sr.ReadLine()) != null) 
      { 
       limit_memory[i]= input; 
       i++; 
      } 
     } 
    } 

} 
private void button1_Click(object sender, EventArgs e) 
{ 
    //shows some textboxes that user has to fill and press enter to continue 
} 
private void button2_Click(object sender, EventArgs e) 
{ 
    //shows some textboxes that user has to fill and press enter to continue 
} 
private void button3_Click(object sender, EventArgs e) 
{ 
    //shows some textboxes that user has to fill and press enter to continue 
} 
private void button4_Click(object sender, EventArgs e) 
{ 
    //shows some textboxes that user has to fill and press enter to continue 
} 
private void button5_Click(object sender, EventArgs e) 
{ 
    //shows some textboxes that user has to fill and press enter to continue 
} 

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == '\r') 
     { 
     if (backgroundWorker1.IsBusy != true) 
      { 
       object[] button1_var = { sentry1, terminal1, txtBox1_serial}; 
       backgroundWorker1.RunWorkerAsync(button1_var); //pass some arguments with an object 
      } 
     } 
} 

private void textBox2_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == '\r') 
     { 
     if (backgroundWorker2.IsBusy != true) 
      { 
       object[] button2_var = { sentry2, terminal2, txtBox2_serial}; 
       backgroundWorker2.RunWorkerAsync(button2_var); //pass some arguments with an object 
      } 
     } 
} 
private void textBox3_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == '\r') 
     { 
     if (backgroundWorker3.IsBusy != true) 
      { 
       object[] button3_var = { sentry3, terminal3, txtBox3_serial}; 
       backgroundWorker3.RunWorkerAsync(button3_var); //pass some arguments with an object 
      } 
     } 
} 
private void textBox4_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == '\r') 
     { 
     if (backgroundWorker4.IsBusy != true) 
      { 
       object[] button4_var = { sentry4, terminal4, txtBox4_serial}; 
       backgroundWorker4.RunWorkerAsync(button4_var); //pass some arguments with an object 
      } 
     } 
} 
private void textBox5_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == '\r') 
     { 
     if (backgroundWorker5.IsBusy != true) 
      { 
       object[] button5_var = { sentry5, terminal5, txtBox5_serial}; 
       backgroundWorker5.RunWorkerAsync(button5_var); //pass some arguments with an object 
      } 
     } 
} 
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
    try 
    { 
     //uses the arguments passed and perform long operations 
     //prints results to textFile1 
    } 
    catch 
    { 
     throw new BackgroundWorkerException(sernum, ex);//pass the unit's serial number to the error handler 
    } 
} 
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) 
{ 
    try 
    { 
     //uses the arguments passed and perform long operations 
     //prints results to textFile2 
    } 
    catch 
    { 
     throw new BackgroundWorkerException(sernum, ex);//pass the unit's serial number to the error handler 
    } 
} 
private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e) 
{ 
    try 
    { 
     //uses the arguments passed and perform long operations 
     //prints results to textFile3 
    } 
    catch 
    { 
     throw new BackgroundWorkerException(sernum, ex);//pass the unit's serial number to the error handler 
    } 
} 
private void backgroundWorker4_DoWork(object sender, DoWorkEventArgs e) 
{ 
    try 
    { 
     //uses the arguments passed and perform long operations 
     //prints results to textFile4 
    } 
    catch 
    { 
     throw new BackgroundWorkerException(sernum, ex);//pass the unit's serial number to the error handler 
    } 
} 
private void backgroundWorker5_DoWork(object sender, DoWorkEventArgs e) 
{ 
    try 
    { 
     //uses the arguments passed and perform long operations 
     //prints results to textFile5 
    } 
    catch 
    { 
     throw new BackgroundWorkerException(sernum, ex);//pass the unit's serial number to the error handler 
    } 
} 
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    //updates the UI on what operations are being run 
} 
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    //updates the UI on what operations are being run 
} 
private void backgroundWorker3_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    //updates the UI on what operations are being run 
} 
private void backgroundWorker4_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    //updates the UI on what operations are being run 
} 
private void backgroundWorker5_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    //updates the UI on what operations are being run 
} 
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    if (e.Cancelled == true) //if user cancels thread 
    { 
    //disconnect unit from PC, clears UI 
    } 
    else if(e.Error != null) //if error occurs in thread 
    { 
    string sernum = ((BackgroundWorkerException)e.Error).Sernum; 
    //logs the serial number and error into a log text file 
    } 
    else //the program ends normally 
    { 
     //disconnect unit from PC, clears UI 
    } 
} 
private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    if (e.Cancelled == true) //if user cancels thread 
    { 
    //disconnect unit from PC, clears UI 
    } 
    else if(e.Error != null) //if error occurs in thread 
    { 
    string sernum = ((BackgroundWorkerException)e.Error).Sernum; 
    //logs the serial number and error into a log text file 
    } 
    else //the program ends normally 
    { 
     //disconnect unit from PC, clears UI 
    } 
} 
private void backgroundWorker3_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    if (e.Cancelled == true) //if user cancels thread 
    { 
    //disconnect unit from PC, clears UI 
    } 
    else if(e.Error != null) //if error occurs in thread 
    { 
    string sernum = ((BackgroundWorkerException)e.Error).Sernum; 
    //logs the serial number and error into a log text file 
    } 
    else //the program ends normally 
    { 
     //disconnect unit from PC, clears UI 
    } 
} 
private void backgroundWorker4_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    if (e.Cancelled == true) //if user cancels thread 
    { 
    //disconnect unit from PC, clears UI 
    } 
    else if(e.Error != null) //if error occurs in thread 
    { 
    string sernum = ((BackgroundWorkerException)e.Error).Sernum; 
    //logs the serial number and error into a log text file 
    } 
    else //the program ends normally 
    { 
     //disconnect unit from PC, clears UI 
    } 
} 
private void backgroundWorker5_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    if (e.Cancelled == true) //if user cancels thread 
    { 
    //disconnect unit from PC, clears UI 
    } 
    else if(e.Error != null) //if error occurs in thread 
    { 
    string sernum = ((BackgroundWorkerException)e.Error).Sernum; 
    //logs the serial number and error into a log text file 
    } 
    else //the program ends normally 
    { 
     //disconnect unit from PC, clears UI 
    } 
} 
public class BackgroundWorkerException : Exception //to pass serial number to error handler 
    { 
     public string Sernum { get; set; } 

     public BackgroundWorkerException(string sernum, Exception ex) 

     { 
      Sernum = sernum; 
     } 
    } 

編集:Backgroundworkerの構造を示すためのコーディングを追加しました。

+0

コメントは議論の対象外です。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/159405/discussion-on-question-by-user107257-i-cannot-run-more-than-4-threads-on- an-i5-c)。 – Andy

答えて

2

i5-6500には4つのhardwarethreadsがあります。これは、コアの使用状況がsplittetを獲得し、4つのスレッドに最適化されていることを意味します。あなたはすべてのCPU上で好きなだけトレッドを開始することができますが、その後、彼らは次のように実行します: Singlecore例:

開始A、Aを停止、停止、再起動、Bを停止し、Bを開始します、スターブb もう一度。

+0

私はマルチスレッドを処理するためにBackgroundWorkerを使用しています。これらのスレッドのシーケンスが正しく動作する方法を最適化するとします。しかし、私のUIはまだ応答しなくなります。 – user107257

+0

これで、正常に動作する可能性があることが分かりました。あなたのコードとハンドラを投稿したい場合は...// – LenglBoy

+0

これはそのBGWをどのように使用しているかによって大きく異なります。あなたはまだ応答性を台無しにすることができます。 – Fildor

関連する問題