0

I use OPC-UA SDK of treager. I have variable created be PLC S7-1200

enter image description here

enter image description here.

It displayed the value on Console.WriteLine, but I cant show it on listView

enter image description here .

Can someone help me show the variable changes on listView1 ?

If I note listView1.Items.Add(itm); It will show that

enter image description here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Opc.Ua.Client;
using Opc.UaFx;
using Opc.UaFx.Client;

namespace Test_OPC
{
    public partial class OPCUA : Form
    {
        private readonly OpcClient client;
        public static OpcValue isRunning;
        string[] arr = new string[4];
        ListViewItem itm;
        public OPCUA()
            : base()
        {
            this.client = new OpcClient("opc.tcp://192.168.1.200:4840");
            InitializeComponent();

        }
        private void OPCUA_Load(object sender, EventArgs e)
        {
            client.Connect();
           
            OpcSubscription subscription = client.SubscribeDataChange("ns=4;i=15",HandleDataChanged);
            //ListViewItem listView1 = new ListViewItem();
            //ListViewItem itemHienThi = new ListViewItem();

            
           
            //Add Item vào ListView
            arr[0] = "01";
            arr[1] = "100";
            arr[2] = "10";
            itm = new ListViewItem(arr);
            listView1.Items.Add(itm);

        }
        private void HandleDataChanged(object sender,OpcDataChangeReceivedEventArgs e)
        {
            OpcMonitoredItem item = (OpcMonitoredItem)sender;

            //Add the attribute name/value to the list view.

            arr[0] = item.NodeId.ToString();
            arr[1] = e.Item.Value.ToString();

            itm = new ListViewItem(arr);
            listView1.Items.Add(itm);

            Console.WriteLine("Data Change from NodeId '{0}': {1}",item.NodeId,e.Item.Value);
        }

    }
}
Tân
  • 1
  • 15
  • 56
  • 102
  • `HandleDataChanged` is returning on a different thread. You will need to marshal back to the`UI`, how you do this depends on what this is written in, winforms, uwp, wpf, xamarin, humptydumpty framework ect – TheGeneral Aug 17 '20 at 04:11
  • 1
    Does this answer your question? [How do I update the GUI from another thread?](https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread) – Klaus Gütter Aug 17 '20 at 04:24
  • `this.Invoke((Action)(() => listView1.Items.Add(itm)))` – aepot Aug 17 '20 at 12:13

0 Answers0