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 ?