网上的demo实在form类头就定义(也就是全局变量)。测试没有问题。如下:
class Tester
{
OPCGroup MyGroup;
OPCGroup MyGroup2;
这个是没问题的!!!!
但是>>>>我想要的是动态生成group,所以必须在类内容某个函数里面定义。我的代码如下:
软件开始运行,3个group的DataChange都可以触发,并且form控件显示。但是运行一段时间(大约2分钟),只好所有group的DataChange都不再触发!!!如果我把group1,group2,group3定义转移到全局,3个group的DataChange又又可以正常一直触发。。不知为何???求大神指教、、小弟分不高。。请见谅
private void button2_Click(object sender, EventArgs e)
{
OPCServer server;
label1.Text = DateTime.Now.ToString();
server = new OPCServer();
server.Connect("KingView.View", "127.0.0.1");
groups = server.OPCGroups;
OPCGroup group1 = groups.Add("group1");
group1.IsActive = true;
group1.UpdateRate = 1000;
group1.IsSubscribed = true;
group1.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange1);
item = group1.OPCItems.AddItem("$毫秒.Value", 1);
OPCGroup group2 = server.OPCGroups.Add("group2");
group2.IsActive = true;
group2.UpdateRate = 1000;
group2.IsSubscribed = true;
group2.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange2);
item = group2.OPCItems.AddItem("$秒.Value", 2);
OPCGroup group3 = server.OPCGroups.Add("group3");
group3.IsActive = true;
group3.UpdateRate = 1000;
group3.IsSubscribed = true;
group3.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange3);
item = group3.OPCItems.AddItem("$时间.Value", 3);
}
void KepGroup_DataChange1(int TransactioenID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
maskedTextBox1.Text = ItemValues.GetValue(1).ToString();
maskedTextBox2.Text = TimeStamps.GetValue(1).ToString();
}
void KepGroup_DataChange2(int TransactioenID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
maskedTextBox3.Text = ItemValues.GetValue(1).ToString();
maskedTextBox4.Text = TimeStamps.GetValue(1).ToString();
}
void KepGroup_DataChange3(int TransactioenID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
maskedTextBox5.Text = ItemValues.GetValue(1).ToString();
maskedTextBox6.Text = TimeStamps.GetValue(1).ToString();
}
1 个解决方案
#1
OPCServer server 是click事件的私有变量,一段时间后被回收
#1
OPCServer server 是click事件的私有变量,一段时间后被回收