在vb.net中使用后台工作人员 [英] using back ground worker in vb.net

查看:79
本文介绍了在vb.net中使用后台工作人员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的应用程序中,我尝试每5秒对数据库进行一次ping操作,以查看是否已插入任何数据.

我正在使用计时器和后台工作程序,但是由于我是vb.net的新手,所以我完全弄乱了代码,请帮忙.

检查代码

Hi,

In my application I''m trying to ping the DB after every 5 sec to see if any data has been inserted.

I''m using timer and background worker but as I''m new to vb.net I''m totally messed up with the code, please help.

check the code

Private Sub tmrPrfrmDBLd_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrPrfrmDBLd.Tick

    tmrPrfrmDBLd.Enabled = False
    bgwPrfrmDBLd.RunWorkerAsync(2000)
    tmrPrfrmDBLd.Enabled = True

End Sub
Private Sub bgwPrfrmDBLd_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwPrfrmDBLd.DoWork
    Try
        Dim sqlSelNewIss As String = "select IssueID from routTo where visible = ''True'' and currownr = " & empID.ToString
        Dim DataSt As New DataSet
        DataSt = DataStore.GetDataset(sqlSelNewIss, sqlConnStr)

        For Each row As DataRow In DataSt.Tables(0).Rows


        Next
''Doing some task
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
Private Sub bgwPrfrmDBLd_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgwPrfrmDBLd.ProgressChanged
    System.Threading.Thread.Sleep(15000)
End Sub
Private Sub bgwPrfrmDBLd_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwPrfrmDBLd.RunWorkerCompleted

End Sub

推荐答案

您可以尝试阅读文档. RunWorkerAsync是您启动线程所需要的.睡觉是错误的方式.只需使用内置在.NET中的真实计时器即可.线程和计时器是两回事.我怀疑问题在于禁用线程时RunWorkAsync可能无法工作,但您没有说出问题所在.

ProgressChanged不在工作线程上运行,而是在主线程上运行,这就是它的作用.线程很复杂,如果您不是"VB.NET的新手",则不能通过猜测来解决它,并且真的不应该使用SQL或线程.
You could try reading the documentation. RunWorkerAsync is what you need to start a thread. Sleeping is the wrong way to do it. Just use a real timer, that''s built into .NET. A thread and a timer are two different things. I suspect the issue is that RunWorkAsync may not work when the thread is disabled, but you don''t say what is going wrong.

ProgressChanged does not run on the worker thread, it runs on the main thread, that''s what it is for. Threading is complicated, you can''t work it out by guessing and really shouldn''t be working with SQL, or threads, if you''re ''new to VB.NET''.


这篇关于在vb.net中使用后台工作人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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