Click here to Skip to main content
15,888,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with a GPS system that provide updates using a call back method. The call back method is on a different thread than the UI thread. I want to write the returned values to text boxes in a winform. In the code below I call an UpdateUI method from the call back metho using uiContext.Send with the uiContext set to the UI Thread. However, in the UpdateUI method I can't access the UI elements. There are only two text boxes in the WinForm but in the UpdateUI method the text box elements LonOutput.text and LatOutput.text are not recognized. Both these elements are set to public. Here is the code

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tinkerforge;
using System.Timers;
using System.Threading;

namespace Test_of_GPS_Call_Back
{
    public partial class Form1 : Form
    {
        private static string HOST = "localhost";
        private static int PORT = 4223;
        private static string UID = "CGH";
        public static long gpsLatitude,  gpsLongitude;
        public static SynchronizationContext uiContext;
        static int i = 0;
        public Form1()
        {
            InitializeComponent();
            uiContext = SynchronizationContext.Current;
            IPConnection ipcon = new IPConnection();
            BrickletGPSV2 gps = new BrickletGPSV2(UID, ipcon);
            ipcon.Connect(HOST, PORT);

            gps.CoordinatesCallback += CoordinatesCB;
            gps.SetCoordinatesCallbackPeriod(200);
            
            
         }
 
        static void CoordinatesCB(BrickletGPSV2 sender, long latitude, char ns,
            long longitude, char ew)
        {
            gpsLatitude = latitude;
            gpsLongitude = longitude;
            uiContext.Send(UpdateUI, latitude);

        }
        private static void UpdateUI(object state)
        {
            i++;
            
            

        }
    }
}


when I enter LatOutput.text in the UpdateUI method it is not recognized. I put the incremented integer i in the UpdateUI just to check that it was being accessed properly and make sure the state contained the output from the gps. In examples I found on the internet they were just able to directly access UI elements from this method.

I'm new to this so I expect I'm ,missing something simple.

What I have tried:

Have tried several ways to access the UI elements but none of the worked.
Posted
Updated 27-Aug-17 9:14am

1 solution

What you need is InvokeRequired, see this CP article: Avoiding InvokeRequired[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900