C# 关于在c中使用GUI进行多线程处理#

C# 关于在c中使用GUI进行多线程处理#,c#,multithreading,invoke,C#,Multithreading,Invoke,我正在阅读我将要发布的示例,但我不明白的是: 他们说,如果我想执行与GUI的交互,它必须由UI线程完成。换句话说:使用Controll类的.Invoke方法。在这个例子中,我不明白为什么要在更改输出的GenerateRandomCharacters方法中使用output.Invoke…,但是在更改输出(其颜色)的Toggle()中没有。Invoke?不一样吗 public class RandomLetters { private static Random generator = ne

我正在阅读我将要发布的示例,但我不明白的是: 他们说,如果我想执行与GUI的交互,它必须由UI线程完成。换句话说:使用Controll类的.Invoke方法。在这个例子中,我不明白为什么要在更改输出的
GenerateRandomCharacters
方法中使用
output.Invoke…
,但是在更改输出(其颜色)的
Toggle()中没有
。Invoke
?不一样吗

public class RandomLetters
{
    private static Random generator = new Random(); // for random letters
    private bool suspended = false; // true if thread is suspended
    private Label output; // Label to display output
    private string threadName; // name of the current thread

   // RandomLetters constructor
   public RandomLetters( Label label )
   {
      output = label;
   } // end RandomLetters constructor

   // delegate that allows method DisplayCharacter to be called
   // in the thread that creates and maintains the GUI
   private delegate void DisplayDelegate( char displayChar );

   // method DisplayCharacter sets the Label's Text property
   private void DisplayCharacter( char displayChar )
   {
   // output character in Label
      output.Text = threadName + ": " + displayChar;
   } // end method DisplayCharacter

   // place random characters in GUI
   public void GenerateRandomCharacters()
   {
   // get name of executing thread
      threadName = Thread.CurrentThread.Name;

      while ( true ) // infinite loop; will be terminated from outside
      {
      // sleep for up to 1 second
         Thread.Sleep( generator.Next( 1001 ) );

         lock ( this ) // obtain lock
         {
           while ( suspended ) // loop until not suspended
           {
             Monitor.Wait( this ); // suspend thread execution
           } // end while
         } // end lock
         // select random uppercase letter
         char displayChar = ( char ) ( generator.Next( 26 ) + 65 );

         // display character on corresponding Label
         output.Invoke( new DisplayDelegate( DisplayCharacter ),
         new object[] { displayChar } );
      } // end while
    } // end method GenerateRandomCharacters

    // change the suspended/running state
    public void Toggle()
    {
       suspended = !suspended; // toggle bool controlling state

       // change label color on suspend/resume
       output.BackColor = suspended ? Color.Red : Color.LightGreen;

       lock ( this ) // obtain lock
       {
          if ( !suspended ) // if thread resumed
          Monitor.Pulse( this );
       } // end lock
     } // end method Toggle
  } // end class RandsomLetters
编辑:这里是实际调用:

public partial class GUIThreadsForm : Form
{
    public GUIThreadsForm()
    {
        InitializeComponent();
    } // end constructor

    private RandomLetters letter1; // first RandomLetters object
    private RandomLetters letter2; // second RandomLetters object
    private RandomLetters letter3; // third RandomLetters object

    private void GUIThreadsForm_Load( object sender, EventArgs e )
    {
        // create first thread
        letter1 = new RandomLetters( thread1Label );
        Thread firstThread = new Thread(
            new ThreadStart( letter1.GenerateRandomCharacters ) );
        firstThread.Name = "Thread 1";
        firstThread.Start();

        continues...

这是调用Toggle()方法的方式:

private void threadCheckBox_CheckedChanged( object sender, EventArgs e )
{
    if ( sender == thread1CheckBox )
        letter1.Toggle();
    else if ( sender == thread2CheckBox )
        letter2.Toggle();
    else if ( sender == thread3CheckBox )
        letter3.Toggle();
} // end method threadCheckBox_CheckedChanged

在CheckedChanged事件中激活复选框控件并将其放入Toggle()方法后

可能调用此类方法的任何对象都只会从非UI线程调用
GenerateRandomCharacters
,而
Toggle
只会从UI线程调用,因此,为什么只有
GenerateRandomCharacters
需要显式封送到UI线程


您需要查看如何调用这些方法来验证。

“无限循环;将从外部终止”您不是在调用
线程。中止,是吗?!这很危险。@usr不,我不是。有一个复选框控件,被选中后线程必须暂停。我可以建议重新编写问题的标题吗?