Vb.net 迭代未按预期工作
我正在使用一个DO交互来循环一个用于测试internet连接的函数。代码运行良好,只是当其中一个测试满足时,循环停止。我希望在程序运行时在后台继续此操作。我怎样才能让它工作Vb.net 迭代未按预期工作,vb.net,Vb.net,我正在使用一个DO交互来循环一个用于测试internet连接的函数。代码运行良好,只是当其中一个测试满足时,循环停止。我希望在程序运行时在后台继续此操作。我怎样才能让它工作 Private Sub checkInternet() Dim InetChecker As Boolean InetChecker = CheckForInternetConnection() Do While LabelCount.Text <> "" Thread.
Private Sub checkInternet()
Dim InetChecker As Boolean
InetChecker = CheckForInternetConnection()
Do While LabelCount.Text <> ""
Thread.Sleep(10)
If InetChecker = True Then
Dim image = My.Resources.greenbar
PictureBox4.Image = image
Else
Thread.Sleep(10)
Dim image = My.Resources.redbar
PictureBox4.Image = image
'NoInetConnError.Show()
End If
Loop
End Sub
Private Sub-checkInternet()
作为布尔值的Dim-InetChecker
InetChecker=CheckForInternetConnection()
在LabelCount.Text“”时执行此操作
线程。睡眠(10)
如果InetChecker=True,则
Dim image=My.Resources.greenbar
PictureBox4.Image=图像
其他的
线程。睡眠(10)
Dim image=My.Resources.redbar
PictureBox4.Image=图像
'NoInetConnError.Show()
如果结束
环
端接头
非常感谢您的帮助。最好在
计时器中执行类似操作,而不是在循环中执行
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
If CheckForInternetConnection Then
PictureBox4.Image = My.Resources.greenbar
Else
PictureBox4.Image = My.Resources.redbar
End If
End Sub
最好在计时器中执行类似操作,而不是在循环中执行
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
If CheckForInternetConnection Then
PictureBox4.Image = My.Resources.greenbar
Else
PictureBox4.Image = My.Resources.redbar
End If
End Sub
如果您有权访问.Net framework 3+,那么您可以使用该类,该类基本上创建了一个间隔(设置为您需要的任何长度),您可以为该间隔处理勾号事件。当勾号事件触发时,您可以进行internet连接检查
根据您的情况修改MSDN示例,可以执行以下操作:
' DispatcherTimer setup
dispatcherTimer = New Threading.DispatcherTimer()
AddHandler dispatcherTimer.Tick, AddressOf dispatcherTimer_Tick
dispatcherTimer.Interval = New TimeSpan(0,0,1) ' Or however long you want
dispatcherTimer.Start()
Private Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
' Checks to see whether an internet connection is still available etc
checkInternet()
' Forcing the CommandManager to raise the RequerySuggested event
CommandManager.InvalidateRequerySuggested()
End Sub
如果您有权访问.Net framework 3+,那么您可以使用该类,该类基本上创建了一个间隔(设置为您需要的任何长度),您可以为该间隔处理勾号事件。当勾号事件触发时,您可以进行internet连接检查
根据您的情况修改MSDN示例,可以执行以下操作:
' DispatcherTimer setup
dispatcherTimer = New Threading.DispatcherTimer()
AddHandler dispatcherTimer.Tick, AddressOf dispatcherTimer_Tick
dispatcherTimer.Interval = New TimeSpan(0,0,1) ' Or however long you want
dispatcherTimer.Start()
Private Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
' Checks to see whether an internet connection is still available etc
checkInternet()
' Forcing the CommandManager to raise the RequerySuggested event
CommandManager.InvalidateRequerySuggested()
End Sub
在表单上添加BackgroundWorker(您可以在工具箱的组件部分找到它)
在属性窗口中,为BackgroundWorker设置WorkerReportsProgress
至True
在表单中插入以下代码
Private connected As Boolean
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) _
Handles BackgroundWorker1.DoWork
While True
Dim online = CheckForInternetConnection()
If online <> connected Then
connected = online
BackgroundWorker1.ReportProgress(CInt(online))
End If
Thread.Sleep(500)
End While
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _
Handles BackgroundWorker1.ProgressChanged
Dim online As Boolean = CBool(e.ProgressPercentage)
If online Then
PictureBox4.Image = My.Resources.greenbar
Else
PictureBox4.Image = My.Resources.redbar
End If
End Sub
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
' Start the background worker
BackgroundWorker1.RunWorkerAsync()
End Sub
Private作为布尔连接
私有子BackgroundWorker1_DoWork(ByVal sender作为对象,ByVal e作为DoWorkerVentargs)_
处理BackgroundWorker1.DoWork
虽然是真的
Dim online=检查Internet连接()
如果在线连接,则
已连接=在线
BackgroundWorker1.报告进度(CInt(在线))
如果结束
线程。睡眠(500)
结束时
端接头
Private Sub BackgroundWorker1_ProgressChanged(ByVal发件人作为对象,ByVal e作为ProgressChangedEventArgs)_
处理BackgroundWorker1.ProgressChanged
联机变暗为布尔值=CBool(例如百分比)
如果在线的话
PictureBox4.Image=My.Resources.greenbar
其他的
PictureBox4.Image=My.Resources.redbar
如果结束
端接头
私有子表单加载(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)_
处理MyBase.Load
'启动后台工作程序
BackgroundWorker1.RunWorkerAsync()
端接头
请注意,Sub-BackgroundWorker1\u-DoWork
在单独的线程上运行,并且在窗体运行时不会冻结窗体。在窗体上放置BackgroundWorker(您将在工具箱的组件部分找到它)
在属性窗口中,为BackgroundWorker设置WorkerReportsProgress
至True
在表单中插入以下代码
Private connected As Boolean
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) _
Handles BackgroundWorker1.DoWork
While True
Dim online = CheckForInternetConnection()
If online <> connected Then
connected = online
BackgroundWorker1.ReportProgress(CInt(online))
End If
Thread.Sleep(500)
End While
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _
Handles BackgroundWorker1.ProgressChanged
Dim online As Boolean = CBool(e.ProgressPercentage)
If online Then
PictureBox4.Image = My.Resources.greenbar
Else
PictureBox4.Image = My.Resources.redbar
End If
End Sub
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
' Start the background worker
BackgroundWorker1.RunWorkerAsync()
End Sub
Private作为布尔连接
私有子BackgroundWorker1_DoWork(ByVal sender作为对象,ByVal e作为DoWorkerVentargs)_
处理BackgroundWorker1.DoWork
虽然是真的
Dim online=检查Internet连接()
如果在线连接,则
已连接=在线
BackgroundWorker1.报告进度(CInt(在线))
如果结束
线程。睡眠(500)
结束时
端接头
Private Sub BackgroundWorker1_ProgressChanged(ByVal发件人作为对象,ByVal e作为ProgressChangedEventArgs)_
处理BackgroundWorker1.ProgressChanged
联机变暗为布尔值=CBool(例如百分比)
如果在线的话
PictureBox4.Image=My.Resources.greenbar
其他的
PictureBox4.Image=My.Resources.redbar
如果结束
端接头
私有子表单加载(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)_
处理MyBase.Load
'启动后台工作程序
BackgroundWorker1.RunWorkerAsync()
端接头
请注意,Sub-BackgroundWorker1\u-DoWork
在一个单独的线程上运行,并且在表单运行时不会冻结表单。谢谢您的建议。但是,它起作用了,每次代码运行时都会导致应用程序无响应。所以我想看看我是否可以使用下面@davidsleeps建议的背景工作方法。再次感谢。@KismetAgbasi请确保您不经常调用代码。在你发布的代码中,你在两次检查之间睡了10毫秒。那很可能是为了……而解雇你的支票。。。方法过于频繁。10-30秒可能更实用,因为您只是试图向最终用户显示状态。我从你的另一个问题中查看了你的CheckForInternetConnection代码:如果你每10毫秒运行一次代码,谷歌(或任何网站)可能不会感激你对他们所做的一切。谢谢,我没有想到这一点。我会调整等待/睡眠时间。谢谢你的建议。但是,它起作用了,每次代码运行时都会导致应用程序无响应。所以我想看看我是否可以使用下面@davidsleeps建议的背景工作方法。再次感谢。@KismetAgbasi请确保您不经常调用代码。在你发布的代码中,你在两次检查之间睡了10毫秒。那很可能是为了……而解雇你的支票。。。方法过于频繁。10-30秒可能更实用,因为您只是试图向最终用户显示状态。我从你的另一个问题中查看了你的CheckForInternetConnection代码:如果你每10毫秒运行一次代码,谷歌(或任何网站)可能不会感激你对他们所做的一切。谢谢,我没有想到这一点。我要登广告