2016-03-28 15 views
0

私はスレッドで2つの機能を実行しています。 1つはループを持ち、もう1つは入力を待っているので、入力が発生すると、最初のループスレッドを一時停止し、同じ第2のスレッドでスレッドを再開したり、終了させたりする必要があります。別のスレッドからスレッドを一時停止して再開するC#

あなたが、私はこのロジックを実装する場所を、以下の私のコードを見ることができます...

ここ
class Program 
    { 
     public static int flag = 0; 
     public static int process; 
     public static int quantum; 
     public static int[] execTime = new int[10]; 
     public static string[] P0 = new string[100]; 
     public static string[] P1 = new string[100]; 
     public static string[] P2 = new string[100]; 
     public static string[] P3 = new string[100]; 
     public static string[] P4 = new string[100]; 
     public static string[] P5 = new string[100]; 
     public static string[] P6 = new string[100]; 
     public static string[] P7 = new string[100]; 
     public static string[] P8 = new string[100]; 
     public static string[] P9 = new string[100]; 
     public static void Main(string[] args) 
     { 
      Console.ForegroundColor = ConsoleColor.White; // White Color Text Below This   
      //************************// 
      //*** Filling Process ***// 
      //************************// 
      for (int i = 0; i < P0.Length; i++) 
      { 
       P0[i] = "P0." + i;     
      } 
      for (int i = 0; i < P1.Length; i++) 
      { 
       P1[i] = "P1." + i; 
      } 
      for (int i = 0; i < P2.Length; i++) 
      { 
       P2[i] = "P2." + i; 
      } 
      for (int i = 0; i < P3.Length; i++) 
      { 
       P3[i] = "P3." + i; 
      } 
      for (int i = 0; i < P4.Length; i++) 
      { 
       P4[i] = "P4." + i; 
      } 
      for (int i = 0; i < P5.Length; i++) 
      { 
       P5[i] = "P5." + i; 
      } 
      for (int i = 0; i < P6.Length; i++) 
      { 
       P6[i] = "P6." + i; 
      } 
      for (int i = 0; i < P7.Length; i++) 
      { 
       P7[i] = "P7." + i; 
      } 
      for (int i = 0; i < P8.Length; i++) 
      { 
       P8[i] = "P8." + i; 
      } 
      for (int i = 0; i < P9.Length; i++) 
      { 
       P9[i] = "P9." + i; 
      } 

      //************************// 
      //*** Creating Process ***// 
      //************************// 
      do 
      { 
       Console.Write("How Many Process You Want To Create? (0-10) "); 
       process = Convert.ToInt16(Console.ReadLine()); 
       if (process > 10) 
       { 
        Console.ForegroundColor = ConsoleColor.Red; // Red Color Text Below This 
        Console.WriteLine("Error => Your Process Should Be Less Then 10."); 
        Console.ForegroundColor = ConsoleColor.White; // White Color Text Below This 
       } 
      } while (process > 10); 
      Console.ForegroundColor = ConsoleColor.Green; // Green Color Text Below This 
      Console.WriteLine("Success =>" + process + " Process Have Been Generated."); 
      Console.ForegroundColor = ConsoleColor.White; // White Color Text Below This 

      //***************************************// 
      //*** Creating Process Executing TIme ***// 
      //***************************************// 
      for (int i = 0; i < process; i++) 
      {     
       do 
       { 
        Console.Write("What\'s The Executing Time Your Process # " + i + "? (0-100) "); 
        execTime[i] = Convert.ToInt16(Console.ReadLine()); 
        if (execTime[i] > 100) 
        { 
         Console.ForegroundColor = ConsoleColor.Red; // Red Color Text Below This 
         Console.WriteLine("Error => Your Executing Time Should Be Less Then 100."); 
         Console.ForegroundColor = ConsoleColor.White; // White Color Text Below This 
        } 
       } while (execTime[i] > 100); 
      } 
      Console.ForegroundColor = ConsoleColor.Green; // Green Color Text Below This 
      Console.WriteLine("Success =>" + process + " Executing Time Have Been Generated."); 
      Console.ForegroundColor = ConsoleColor.White; // White Color Text Below This 

      //**************************************// 
      //*** Resizing Process Array's Steps ***// 
      //**************************************// 
      Array.Resize(ref P0, execTime[0]); 
      Array.Resize(ref P1, execTime[1]); 
      Array.Resize(ref P2, execTime[2]); 
      Array.Resize(ref P3, execTime[3]); 
      Array.Resize(ref P4, execTime[4]); 
      Array.Resize(ref P5, execTime[5]); 
      Array.Resize(ref P6, execTime[6]); 
      Array.Resize(ref P7, execTime[7]); 
      Array.Resize(ref P8, execTime[8]); 
      Array.Resize(ref P9, execTime[9]); 
      Array.Resize(ref P0, execTime[0]); 

      //********************// 
      //*** Quantum Size ***// 
      //********************// 
      do 
      { 
       Console.Write("What's Your Quantum Size? "); 
       quantum = Convert.ToInt16(Console.ReadLine()); 
       if (quantum > execTime.Sum()) 
       { 
        Console.ForegroundColor = ConsoleColor.Red; // Red Color Text Below This 
        Console.WriteLine("Error => Your Quantum Size Should Be Less Then Total Execution Time."); 
        Console.ForegroundColor = ConsoleColor.White; // White Color Text Below This 
       } 
      } while (quantum > execTime.Sum()); 
      Console.ForegroundColor = ConsoleColor.Green; // Green Color Text Below This 
      Console.WriteLine("Success =>" + quantum + " Quantum Size Have Been Generated."); 
      Console.ForegroundColor = ConsoleColor.White; // White Color Text Below This 

      Thread obj1 = new Thread(processRunning); 
      Thread obj2 = new Thread(interrupt); 
      obj1.Start(); // Start This Thread 
      obj2.Start(); // Start This Thread 

     } 

     static public void processRunning() 
     { 
      //***********************// 
      //*** Running Process ***// 
      //***********************// 

      Console.ForegroundColor = ConsoleColor.Green; // Green Color Text Below This 
      Console.WriteLine("Process Has Been Started Running...!!!"); 
      Console.ForegroundColor = ConsoleColor.White; // White Color Text Below This 

      int i = 0; 
      while (i < execTime.Sum()) 
      { 
       if (i < P0.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 0"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P0.Length) 
         { 
          Console.WriteLine(P0[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       }     
       if (i < P1.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 1"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P1.Length) 
         { 
          Console.WriteLine(P1[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       } 
       if (i < P2.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 2"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P2.Length) 
         { 
          Console.WriteLine(P2[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       } 
       if (i < P3.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 3"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P3.Length) 
         { 
          Console.WriteLine(P3[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       } 
       if (i < P4.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 4"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P4.Length) 
         { 
          Console.WriteLine(P4[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       } 
       if (i < P5.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 5"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P5.Length) 
         { 
          Console.WriteLine(P5[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       } 
       if (i < P6.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 6"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P6.Length) 
         { 
          Console.WriteLine(P6[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       } 
       if (i < P7.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 7"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P7.Length) 
         { 
          Console.WriteLine(P7[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       } 
       if (i < P8.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 8"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P8.Length) 
         { 
          Console.WriteLine(P8[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       } 
       if (i < P9.Length) 
       { 
        Thread.Sleep(1000); 
        int j; 
        Console.WriteLine("----------------------------------------"); 
        Console.WriteLine("Process Identification ID : Process # 9"); 
        for (j = i; j < i + quantum; j++) 
        { 
         if (j < P9.Length) 
         { 
          Console.WriteLine(P9[j]); 
         } 
         else 
         { 
          break; 
         } 
        } 
        Console.WriteLine("IR --> " + i); 
        Console.WriteLine("PC --> " + i); 
        Console.WriteLine("Flag --> " + flag); 
        Console.WriteLine("Last Instruction Address --> " + (j - 1)); 
        Console.WriteLine("Resume Instruction Address --> " + j); 
        Console.WriteLine("Algorithum --> Round Robin"); 
        Console.WriteLine("Quantum --> " + quantum); 
        Console.WriteLine("----------------------------------------"); 
       } 
       // Increment 
       i = i + quantum; 
      } 

      Console.ForegroundColor = ConsoleColor.Green; // Green Color Text Below This 
      Console.WriteLine("Process Has Been Completed Running...!!!"); 
      Console.ForegroundColor = ConsoleColor.White; // White Color Text Below This 
      Console.ReadLine(); 
     } 
     static public void interrupt() 
     { 
      // Intruppt 
      ConsoleKeyInfo c = Console.ReadKey(); 
      if (c.Key == ConsoleKey.Enter) 
      { 
       flag = 1; 
       // Should Pause The First Thread Here And Ask To Continue/Resume Or Kill 
      } 
      // Intruppt 
     } 
    } 

私はprocessRunning();それまでは誰かがinterrupt();に入力を与える実行し続けるようにしたいし、それがprocessRunning();を一時停止と再開を待つ必要がありますそれはまた、interrupt();関数によって許可されるので、それの背後にあるロジック/コードは何か... ???

+0

が、いくつかの論理はまた、バックグラウンドから来ているので、私はそれを明確にしたい... –

+0

は、私が欲しいもの、それを移動します。次回は、私はポイントになるでしょう... –

答えて

1

はあなたがここに心を持っている必要があります物事のカップルがあります。

  1. それはないThread.suspendメカニズムがhereを説明存在しますが、以来、時代遅れと正当な理由のためである:

Thread.SuspendとThread.Resumeは、制御されているスレッドの協力関係に依存しないため、非常に侵入し、デッドロックのような深刻なアプリケーションの問題を引き起こす可能性があります(たとえば、リソースを保持するスレッドを中断する別のスレッドはn eed)。

だから、そのメカニズムを使用し、可能なデッドロックまたは他の同期バグや成り行きを見守ることができます:

を<オール開始=「2」>
  • あなたが欲しいのスレッドにその同期ロジックを移動することができますあなたのケースでは、processRunning()方法を中断し、このような何かします

    ながら、(私は< execTime.Sum()& &フラグ== 0)//まだ停止していない

  • 細かい粒度が必要な場合は、繰り返しのたびにループの途中で停止することさえありますので、必要に応じてチェックを追加してください。そのフィールドは、二つのスレッドによって使用され、そのようにマークされる必要があるので、

    またvolatileとして、あなたの旗を宣言する必要があります:

    public static volatile int flag = 0

    これは簡単な例です:うん

    private static AutoResetEvent signal = new AutoResetEvent(false); 
        private volatile static bool interruptFlag; 
        private volatile static bool abortFlag; 
    
        private static void Process() 
        { 
         //replace true with your condition 
         while (true) 
         { 
          if (interruptFlag) 
          { 
           if (signal.WaitOne()) 
           { 
            if (abortFlag) 
            { 
             Console.WriteLine("exiting"); 
             return; 
            } 
           } 
          } 
    
          Console.WriteLine("doing work"); 
    
          //.. important work here 
         } 
        } 
    
        private static void Interrupt() { 
         ConsoleKeyInfo c = Console.ReadKey(); 
         if (c.Key == ConsoleKey.Enter) { 
          interruptFlag = true; 
          // Should Pause The First Thread Here And Ask To Continue/Resume Or Kill 
    
          c = Console.ReadKey(); 
    
          if (c.Key == ConsoleKey.Escape) 
          { 
           Console.WriteLine("Interrupting"); 
           abortFlag = true; 
           signal.Set(); 
          } 
          else 
          { 
           Console.WriteLine("Continuing"); 
           interruptFlag = false; 
           abortFlag = false; 
           signal.Set(); 
          } 
         } 
        } 
    
        static void Main() 
        { 
         Thread process = new Thread(Process); 
         process.Start(); 
    
         Thread interrupt = new Thread(Interrupt); 
         interrupt.Start(); 
    
        } 
    
    +0

    良いヒントですが再開しません:-) –

    +0

    答えが更新されました:Enterキーを押す=>スレッドを一時停止します。後でEscapeを押す=>スレッドを停止する。エスケープ=>続行以外は何も押さないでください。 –

    +0

    @MuhammadHassanは、この解決策があなたのために働くかどうか教えてください。 –

    -1

    は、あなたが)obj1.Reset()を呼び出すことにより、スレッドの実行を一時停止し、obj1.Set(経て再開してもよいのBackgroundWorkerオブジェクト(System.ComponentModel)

    BackgroundWorker obj1 = new BackgroundWorker(); 
    obj1.DoWork += (dowork_s, dowork_e) => { 
        ProcessRunning(); 
    }; 
    obj1.RunWorkerCompleted += (completed_s, completed_e) => { 
        Thread.Sleep(1000); 
        if(StillRunning){ 
         worker.RunWorkerAsync(); 
        } 
        //if not still running, the thread will now terminate 
    }; 
    

    を使用してみてください。

    +0

    私の要件として理解するのは簡単にできますか? –

    +0

    私は、実行をもう少し明確にするために自分の投稿を更新しました。ThreadオブジェクトをBackgroundWorkerオブジェクトで置き換えることができ、フラグが指定されるまで無限ループに設定することができます。この時点で、Reset()を呼び出してSetを一時停止して再開することができます。 – Jace

    +0

    私はあなたに 'interrupt();'のようにユーザーに尋ねると思います**再開したい、あるいは終了しますか? [Y(Resume)/ N(Terminate)] **何をやるべきか...どこですか?あなたは私の更新されたコードを共有できますか? –

    関連する問題