使用synchronizationcontext从线程引用UI文本框 [英] Reference a UI textbox from a thread using synchronizationcontext

查看:107
本文介绍了使用synchronizationcontext从线程引用UI文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GPS系统,该系统使用回叫方法提供更新。回调方法与UI线程位于不同的线程上。我想将返回的值写入winform中的文本框。在下面的代码中,我使用uiContext.Send从回调方法调用UpdateUI方法,并将uiContext设置为UI Thread。但是,在UpdateUI方法中,我无法访问UI元素。 WinForm中只有两个文本框,但在UpdateUI方法中,无法识别文本框元素LonOutput.text和LatOutput.text。这两个元素都设置为公开。这是代码



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

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++;
            
            

        }
    }
}





当我在UpdateUI方法中输入LatOutput.text时,无法识别它。我将增量整数i放在UpdateUI中,只是为了检查它是否被正确访问并确保状态包含gps的输出。在我在互联网上发现的例子中,他们只能通过这种方法直接访问UI元素。



我是新手,所以我希望我失踪了简单的事情。



我尝试过的事情:



尝试过几次访问UI元素但没有工作的方法。



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.

推荐答案

您需要的是 InvokeRequired ,请参阅此CP文章:避免InvokeRequired [ ^ ]
What you need is InvokeRequired, see this CP article: Avoiding InvokeRequired[^]


这篇关于使用synchronizationcontext从线程引用UI文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆