Hello
I have an interesting finding regarding subscription to OPC DA server item(s). With my code below: (Net Framework 4.7.2 Console app)
1) When I subscribe to a dynamic changing item let say for 10 seconds, and the time is over, and so the console app exits with Environment.Exit(0), I can subscribe once more to the same item with restarting console app, without problem. (In this case I do not unsubscribe to the items with client.UnsubscribeAllItems() after Thread.Sleep(10000))
I have tried this behaviour 8-10 times and always worked.
2) When I subscribe to the same dynamic changing item again for 10 seconds, and , interrupt the code (in my case close the console window) before the time is over, then I cannot re-subscribe to the same item any more. After waiting long time perhaps it is possible to resubscribe again.(In this case I also do not unsubscribe to all items with client.UnsubscribeAllItems() after Thread.Sleep(10000))
3) When I activate the client.UnsubscribeAllItems() after Thread.Sleep(10000), I get a very long exception. I have attached this as a file in the attachement.
My questions:
1) Why is the resubscribing working in case 1) and not in case 2) ?
2) Why is the client.UnsubscribeAllItems() causing to the error in my code?
3) What should be the correct unsubscribtion way for my code, especially for the possibility that my app could not be closed properly?
public void subscribe_items(string ip)
{
try
{
using (var client = new EasyDAClient())
{
client.ItemChanged += client_Main1_ItemChanged;
Console.WriteLine("Subscribing item changes..." + ip);
client.SubscribeMultipleItems(
new[] {
new DAItemGroupArguments(ip, "OPC.IwSCP.1", "IndraMotion_MTX_P60,SPA1,2,P-0-0440,D", 1000, null),
});
Console.WriteLine("Processing item changed events for 1 minute...");
Thread.Sleep(10 * 1000);
//client.UnsubscribeAllItems(); // This line I am just activating in case 3)
}
Console.WriteLine("Finished.");
}
catch (Exception ex)
{
// Error handling
Console.ReadLine();
}
// Instantiate the client object.
}
// Item changed event handler
public string array_item_to_web = "array_item";
public void client_Main1_ItemChanged(object sender, EasyDAItemChangedEventArgs e)
{
Console.WriteLine("item changed");
if (e.Succeeded)
{
try
{
using (StreamWriter sw = File.AppendText(@"D:\test\opcda_testlog_test.txt"))
{
// sw.WriteLine("value: " + e.Vtq + " " + Convert.ToString(DateTime.Now));
}
item_value = e.Vtq.ToString();
Console.WriteLine(e.Vtq);
Console.WriteLine(e.Vtq.Value);
}
catch (Exception ex)
{
//Error handling
}
}
else
{
//Error handling
}
}
I have an interesting finding regarding subscription to OPC DA server item(s). With my code below: (Net Framework 4.7.2 Console app)
1) When I subscribe to a dynamic changing item let say for 10 seconds, and the time is over, and so the console app exits with Environment.Exit(0), I can subscribe once more to the same item with restarting console app, without problem. (In this case I do not unsubscribe to the items with client.UnsubscribeAllItems() after Thread.Sleep(10000))
I have tried this behaviour 8-10 times and always worked.
2) When I subscribe to the same dynamic changing item again for 10 seconds, and , interrupt the code (in my case close the console window) before the time is over, then I cannot re-subscribe to the same item any more. After waiting long time perhaps it is possible to resubscribe again.(In this case I also do not unsubscribe to all items with client.UnsubscribeAllItems() after Thread.Sleep(10000))
3) When I activate the client.UnsubscribeAllItems() after Thread.Sleep(10000), I get a very long exception. I have attached this as a file in the attachement.
My questions:
1) Why is the resubscribing working in case 1) and not in case 2) ?
2) Why is the client.UnsubscribeAllItems() causing to the error in my code?
3) What should be the correct unsubscribtion way for my code, especially for the possibility that my app could not be closed properly?
public void subscribe_items(string ip)
{
try
{
using (var client = new EasyDAClient())
{
client.ItemChanged += client_Main1_ItemChanged;
Console.WriteLine("Subscribing item changes..." + ip);
client.SubscribeMultipleItems(
new[] {
new DAItemGroupArguments(ip, "OPC.IwSCP.1", "IndraMotion_MTX_P60,SPA1,2,P-0-0440,D", 1000, null),
});
Console.WriteLine("Processing item changed events for 1 minute...");
Thread.Sleep(10 * 1000);
//client.UnsubscribeAllItems(); // This line I am just activating in case 3)
}
Console.WriteLine("Finished.");
}
catch (Exception ex)
{
// Error handling
Console.ReadLine();
}
// Instantiate the client object.
}
// Item changed event handler
public string array_item_to_web = "array_item";
public void client_Main1_ItemChanged(object sender, EasyDAItemChangedEventArgs e)
{
Console.WriteLine("item changed");
if (e.Succeeded)
{
try
{
using (StreamWriter sw = File.AppendText(@"D:\test\opcda_testlog_test.txt"))
{
// sw.WriteLine("value: " + e.Vtq + " " + Convert.ToString(DateTime.Now));
}
item_value = e.Vtq.ToString();
Console.WriteLine(e.Vtq);
Console.WriteLine(e.Vtq.Value);
}
catch (Exception ex)
{
//Error handling
}
}
else
{
//Error handling
}
}