2

我正在创建一个调用事件的程序。这是代码

public void Form1_Load(Object sender, EventArgs e)
{
      Thread thread = new Thread(new ThreadStart(getData));
      thread.IsBackground = true;
      thread.Start();
      Thread.CurrentThread.Name = "Main";
}
private void getData()
{
       try
       {
            int count = 1;
            opcServer.Connect("OPCTechs.SiemensNet30DA", "");
            Thread.CurrentThread.Name = "Child";

            opcGroup = opcServer.OPCGroups.Add("MP");
            opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange);

            //Get First String
            for (int i = 40; i <= 47; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Second String
            for (int i = 80; i <= 91; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            opcGroup.OPCItems.DefaultIsActive = true;
            opcGroup.UpdateRate = 1000;
            opcGroup.IsSubscribed = opcGroup.IsActive;
      }
      catch (Exception exc)
      {
                 MessageBox.Show(exc.Message, "Alert");
      }
}
private void opcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
     try
     {
            MessageBox.Show(Thread.CurrentThread.Name,"Alert");
            string temp = "";
            int count = 1;
            for (count = 1; count <= NumItems; count++)
            {
               if (Convert.ToInt32(ClientHandles.GetValue(count)) == 47)
                    temp += ItemValues.GetValue(count).ToString();
            }
            Textbox1.Text = temp.ToString();
            temp = "";
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "Alert");
        }
}

在这种语法MessageBox.Show(Thread.CurrentThread.Name,"Alert");中,我期待显示,Child因为它是从子线程调用的,但它正在显示Main。为什么事件在主线程中运行而不在子线程中运行?

4

0 回答 0