2

I am creating a program where events gets called. Here is the code

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");
        }
}

In this syntax MessageBox.Show(Thread.CurrentThread.Name,"Alert");, I was expecting to show Child since it is called from the child thread, but it is showing Main. Why the event is running in the Main Thread but not in Child Thread ?

Harshit
  • 5,147
  • 9
  • 46
  • 93
  • what would happen if you remove `Thread.CurrentThread.Name = "Main";` from `Form1_Load` – Mohit S Sep 25 '15 at 03:35
  • @siride no, I just didn't look carefully enough. – phoog Sep 25 '15 at 03:36
  • You start a random thread and then just give it a name randomly I don't think you know what you're doing – Xogle Sep 25 '15 at 03:36
  • Look at this http://www.tutorialspoint.com/csharp/csharp_multithreading.htm – Xogle Sep 25 '15 at 03:38
  • 2
    What happens if you put a `MessageBox.Show()` inside your `getData()` method? Does it show something different from what's shown if that call is inside your event method? It's possible the event is being run on the main UI thread. It really depends on what the library you're using does with the event. – siride Sep 25 '15 at 03:38
  • Yes, I put Messagebox just below `Thread.CurrentThread.Name = "Child";`, and it shown `Child`. – Harshit Sep 25 '15 at 03:40
  • Can you put a breakpoint inside your event handler and look in the threads window to see which thread it is running on? – siride Sep 25 '15 at 03:41
  • @siride, In the thread window, in the `Name` field, it is showing `Main` written. – Harshit Sep 25 '15 at 03:47
  • @HarshitShrivastava: then it seems like the library you are using is probably invoking the event handler on the UI thread. You'll need to look into that (I don't know if you own the code, or it's 3rd party). – siride Sep 25 '15 at 03:48
  • Yes, I am using `Interop.OPCAutomation.dll` library. Is there any way to call it on separate thread since events gets fired in seconds, I don't want my Main form to freeze. – Harshit Sep 25 '15 at 03:51
  • You could always have your event put information in a shared queue, which is read by the background thread. The event would quickly load the queue and then return. – siride Sep 25 '15 at 03:52
  • 3
    Check out [this](http://stackoverflow.com/questions/2459634/in-net-what-thread-will-events-be-handled-in) SO question. The top 2 answers might help explain what's happening – amura.cxg Sep 25 '15 at 03:56
  • @amura.cxg, I put `OPCAutomation.OPCGroup opcGroup` inside `getData()` method, as read from your link. Now it seems to be running in separate thread but now also it is not showing `Child` in the messagebox but it is showing blank value. – Harshit Sep 25 '15 at 04:14

0 Answers0