VB.net 2010 Backgroundworker和picturebox [英] VB.net 2010 Backgroundworker and picturebox

查看:79
本文介绍了VB.net 2010 Backgroundworker和picturebox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我在这里的第一篇文章!



我通常是一个非常自立的人,我自己找到答案。但是我一直在努力让我的BackgroundWorker做我想做的事情......



我正在开发一个从数据库中提取信息并放入它的小软件在几个datagridview控件上。 SQL请求会提取大量数据,处理大约需要40秒。



所以我想在一个图片框中放置一个正在进行中动画gif,它将在工作开始之前显示出来。



现在如果事先尝试过显示gif,但可以预见,它只是生涩,只能在两次SQL调用之间动画。



我试图将图片框放到另一个线程中,但我想我正在做它因为,虽然后台工作者似乎做了它的工作,动画本身似乎卡在主线程中...... br />


这是代码:



我调用后台工作者和委托的新实例:

Hi! First post for me here!

I am usually a pretty self-reliant guy and find my answers by myself. But I haver been struggling to make my BackgroundWorker do what I want of it...

I am working on a small software that pulls info from a database and put it on several datagridview controls. The SQL request pulls quite a lot of data and it takes about 40 seconds to process.

So. I want to put a "Work in progress" animated gif, inside a picturebox, that will be put to visible before the work starts.

Now If have tried just showing the gif beforehand, but predictably, it is jerky, only animating between two SQL calls.

I have tried to put the picturebox into another thread, but I guess I am doing it worng because, while the background worker seems to do it's job, the animation itself seems stuck in the main thread...

Here is the code:

I call a new instance of the Background worker and the delegate:

Private WithEvents wrkDeploy As New System.ComponentModel.BackgroundWorker()
Public Delegate Sub PictureVisibilityDelegate(ByVal visibility As Boolean)
Dim ChangePictureVisibility As PictureVisibilityDelegate





添加到表单加载,处理程序和委托链接的行:





Lines added to the form load, Handlers and delegate link:

AddHandler wrkDeploy.DoWork, AddressOf wrkDeploy_DoWork
AddHandler wrkDeploy.RunWorkerCompleted, AddressOf wrkDeploy_RunWorkerCompleted
ChangePictureVisibility = AddressOf ChangeVisibility





Dowork,RunworkerComplete和ChangeVisibility潜艇



The "Dowork" , "RunworkerComplete" and "ChangeVisibility" subs

Protected Sub wrkDeploy_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles wrkDeploy.DoWork
        Me.Invoke(ChangePictureVisibility, True)
End Sub

Protected Sub wrkDeploy_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles wrkDeploy.RunWorkerCompleted
        Me.Invoke(ChangePictureVisibility, False)
End Sub
    
Public Sub ChangeVisibility(ByVal visibility As Boolean)
    PicWait.Visible = visibility
End Sub





我的ODBC调用之前和之后的调用



And the calling before and after my ODBC calls

wrkDeploy = New System.ComponentModel.BackgroundWorker
wrkDeploy.WorkerSupportsCancellation = True
wrkDeploy.RunWorkerAsync()

UnifiedODBC(dgvTickets, 0)
UnifiedODBC(dgvTickets2, 1)
UnifiedODBC(dgvTickets3, 2)

wrkDeploy.CancelAsync()







这是我的唯一方式ave得到它显示...它仍然不会正确显示动画。此外,图片将在处理完成后进行相应的动画处理,但工作人员在取消图片后不会隐藏图片框。





任意建议?



谢谢




This is the only way I have gotten it to show... And it still will not show the animation correctly. Furthermore, the pic will animate corretly after the treatment is done but the worker will not hide the picturebox after I cancel it.


Any suggestions?

Thanks

推荐答案

你有正确的想法,但是把错误的东西放在了后台工作者。



您的应用程序启动的线程也称为UI线程。所有UI项目和迭代,包括PictureBox和GIF应保留在UI线程上。您的数据库查询工作应该在后台工作程序中。
You have the right idea, but are putting the wrong things in the background worker.

The thread your app starts on is also referred to as the UI thread. ALL UI items and iteraction, including your PictureBox and GIF should remain on the UI thread. Your database query work should be in the background worker.


这篇关于VB.net 2010 Backgroundworker和picturebox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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