试图在两个线程之间使用事件

本文关键字:之间 线程 事件 两个 | 更新日期: 2023-09-27 17:54:46

我试图使我的第一个应用程序,我使用事件两个线程之间的通信。我不太了解与代表一起工作,所以我需要建议如何完成我的申请,我的错误是什么(如果有的话)?

我使用两个类。类1,包含两个线程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
public class MyThreadTest : EventArgs
{
private string _threadOutput = "";
private bool _stopThreads = false;

            /// <summary>
            /// Thread 1: Loop continuously,
            /// Thread 1: Displays that we are in thread 1
            /// </summary>
            void DisplayThread1()
            {
                  while (_stopThreads == false)
                  {
                        Console.WriteLine("Display Thread 1");
                        // Assign the shared memory to a message about thread #1
                        _threadOutput = "Hello Thread1";

                        Thread.Sleep(1000);  // simulate a lot of processing 
                        // tell the user what thread we are in thread #1, and display shared memory
                        Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);

                  }
            }

            /// <summary>
            /// Thread 2: Loop continuously,
            /// Thread 2: Displays that we are in thread 2
            /// </summary>
            void DisplayThread2()
            {
                  while (_stopThreads == false)
                  {
                    Console.WriteLine("Display Thread 2");

                   // Assign the shared memory to a message about thread #2
                    _threadOutput = "Hello Thread2";

                    Thread.Sleep(1000);  // simulate a lot of processing
                   // tell the user we are in thread #2
                    Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);

                  }
            }

            void CreateThreads()
            {
                // construct two threads for our demonstration;
                Thread thread1 = new Thread(new ThreadStart(DisplayThread1));
                Thread thread2 = new Thread(new ThreadStart(DisplayThread2));
                // start them
                thread1.Start();
                thread2.Start();
            }
            public static void Main()
            {
                MyThreadTest StartMultiThreads = new MyThreadTest();
                StartMultiThreads.CreateThreads();
            }
}

我知道有一些额外的代码,但通常我的目标是模拟两个线程。

第二个类,我尝试实现我的委托,并最终使用自定义事件发送消息从第一个线程到第二个(至少这是我的最终目标):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
   class Class1
   {
       public delegate void ShowMyMessage(object sender, EventArgs e);
       public event ShowMyMessage ShowIt;
       // Invoke the ShowIt event; called whenever I like it :)
       protected virtual void OnShowIt(EventArgs e)
       {
           if (ShowIt != null)
               ShowIt(this, e);
       }
   }
   class EventListener
   {
       private Class1 msg;
       public EventListener(Class1 msg)
       {
           Class1 Message = msg;
           // Add "ListChanged" to the Changed event on "List".
           Message.ShowIt += new ShowMyMessage(MessageShowIt);
       }
       // This will be called whenever the list changes.
       private void ListChanged(object sender, EventArgs e)
       {
           Console.WriteLine("This is called when the event fires.");
       }
   }

我知道这不仅仅是一个愚蠢的错误,但我需要知道这是否是创建事件的方式,以及我如何才能成功完成我的工作?

试图在两个线程之间使用事件

当您订阅一个事件时,您提供了有关该方法和对象实例的信息。但不是关于一条线。

事件总是在引发事件的同一个线程中处理。同步。

事件不是线程间通信的机制。