启动连续运行的子程序后,将控件返回到UI [英] Return control to UI after launching a continuously running sub
问题描述
您好,我的应用程序有一个带有多个按钮控件的表单.其中之一会启动一个连续运行的sub run(),当然,它将冻结该窗体. 其他按钮旨在设置标志以与sub run()进行交互.我尝试过 将该例程放入线程中以将控制权释放回表单,但似乎不太正确.另外,在sub run()中,我正在访问其他形式的控件,并将数据传递给其他子&功能.有什么想法吗? 我被卡住了.
Hi, My app has a form with several button controls. One of them launches a continuously running sub run(), which, of course, freezes the form. The other buttons are designed to set flags to interact with sub run(). I have tried to put this routine in a thread in order to release control back to the form, but can't quite seem to get it right. Also, within sub run(), I am accessing controls on other forms, as well as passing data to other subs & functions. Any ideas? I'm stuck.
谢谢!
RUN SUB内的活动示例
SAMPLE OF ACTIVITY WITHIN THE RUN SUB
私有 Sub btnRun_Click( ByVal 发件人 按系统.对象, ByVal e As 系统. EventArgs ) 句柄 btnRun.单击
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
'初始化AKD状态文本框
frmGripper.tbxStatus.Text = "
frmGripper.tbxStatus.Text = ""
frmGripper.tbxStatus.Refresh()
'等待CNC OK卸载/加载
sRet<> "1" '等待就绪";位
While sRet <> "1" 'wait for "ready" bit
sCmd = "DIN3.STATE"
sCmd = "DIN3.STATE"
frmGripper .SendAKD(sServerAddress,iPortNumber,sCmd)
frmGripper.SendAKD(sServerAddress, iPortNumber, sCmd)
sRet = frmGripper .ReceivedData
sRet = frmGripper.ReceivedData
sRet = Microsoft.VisualBasic.Left(sRet,1)
结束 同时
End While
sRet = "0"
sRet = "0"
'等待机器人准备好
'wait Robot ready
pval0 = 0
pval0< 128 'DO [25] = ON
While pval0 < 128 'DO[25] = ON
睡眠(100)
iError0 = RP_OpenDIO(SN,hDIO)
iError1 = RP_ReadPort(hDIO,1,pval0,errMsg) '读取端口1
iError1 = RP_ReadPort(hDIO, 1, pval0, errMsg) 'read port 1
RP_CloseDIO(hDIO)
pval0 = 255-pval0
结束 同时
End While
推荐答案
为什么在使用时总是看到如此困难的代码来完成这些简单的事情,后台工作程序,多线程等等在这种情况下,计时器非常简单.
Why do I see always such difficult code to do these simple things, backgroundworkers, multithreading and whatever and that while using a timer in these cases is so easy.
这不是您的解决方案,而是给您一个想法.
Not that it is your solution but to give you an idea.
Private WithEvents tim As New Timer With {.Interval = 100, .enabled = true}
Private Sub Whatever() Handles tim.Tick
If pval0 < 128 Then 'DO[25] = ON
iError0 = RP_OpenDIO(SN, hDIO)
iError1 = RP_ReadPort(hDIO, 1, pval0, errMsg) 'read port 1
RP_CloseDIO(hDIO)
pval0 = 255 - pval0
End If
End Sub
这篇关于启动连续运行的子程序后,将控件返回到UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!