为什么我的Prog工作如果我有无意义的MsgBox而不是如果我没有... [英] Why does my Prog work if I have pointless MsgBox and not if I don't...

查看:112
本文介绍了为什么我的Prog工作如果我有无意义的MsgBox而不是如果我没有...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个问题,我认为Serial Comms线程和UI线程都没有了。我正在读回一个值,如果我输入一个MsgBox它可以工作,如果我有一个计时器启用来检查传入文本框的内容(它是一个丰富的!)

 私有  Sub  TmrNoDataAtPort_Tick( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs)句柄 TmrNoDataAtPort.Tick 


< span class =code-keyword> while (rtbIncoming.Text =

结束
TmrNoDataAtPort.Enabled = False
' Thread.Sleep(200)

结束 Sub

但这不起作用如果我加入计时器

 txtReadFreqA.Text = rtbIncoming.Text 



这些数字出现在他们应该的位置然而,这会把工具箱计时器的负担放在过去(我现在工作得很好但是当某些PC的负载是这样的时候)。我觉得我需要更加确定的方法才能做到这一点......

格伦

解决方案

这是一个< b>使用计时器的坏主意进行通信。您可以更可靠,更轻松地使用流。你已经在使用一个线程更难以理解。你没有解释你使用什么样的计时器以及为什么。



您无法从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。计时器处理程序(不使用计时器)也是如此,除非这是计时器 System.Windows.Forms.Timer 。但是这种类型的计时器对于大多数目的来说太糟糕了,除了一些我们不需要在这里讨论。 无论如何都不要使用任何计时器。



你会在我过去的答案中找到它的工作原理和代码示例的详细说明:

Control.Invoke()与Control。 BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的线程上运行在vb.net [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA

Hi All,
I have a problem caused I think by the Serial Comms thread and UI thread being out. I am reading back a value, if I put in a MsgBox it works if I have a timer Enabled to check the contents of the incoming text box (its a Rich one!)

Private Sub TmrNoDataAtPort_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrNoDataAtPort.Tick


       While (rtbIncoming.Text = "")

       End While
       TmrNoDataAtPort.Enabled = False
       'Thread.Sleep(200)

   End Sub

That does not work however if I put into the timer

txtReadFreqA.Text = rtbIncoming.Text


The numbers appear where they should be however that puts the burden on the tool box timer which I have been caught out by in the past (it works fine now but when the load of the certain PC(s) that this is for. I think I need a more certain way of doing it before I get too far with this...
Glenn

解决方案

It's a bad idea to use timers for communication. You could use stream much more reliably and easier. It even less understandable that you are already using a thread. You did not explain what kind of timer do you use and why.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only). The same goes for the timer handler (don't use the timer), unless this is the timer System.Windows.Forms.Timer. But this type of timer is too bad for most purposes except some we don't need to discuss here. Don't use any timers at all, anyway.

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于为什么我的Prog工作如果我有无意义的MsgBox而不是如果我没有...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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