在执行代码时显示动画gif [英] Displaying animated gif while code is being executed

查看:304
本文介绍了在执行代码时显示动画gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一些代码需要很长时间才能执行。当我按下表单上的按钮时,代码开始执行。

我想显示动画gif显示系统正在运行。当应用程序运行时,gif会一直显示进度,但是当我按下Button1时,代码开始执行,动画停止,然后在执行完成后,动画再次启动。



如何在执行代码时运行动画。



代码为:



Hi,
I have some code that takes quite long to execute. That codes begins executing when I press the button on my form.
I want to display the animated gif showing system is running. When the application runs, gif keeps showing the progress but when I press Button1, code begins to execute, animation stops and then after execution is finished, animation starts again.

How to make animation run while code is being executed.

The code is:

Dim sqldatasourceenumerator1 As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
        Dim datatable1 As DataTable = sqldatasourceenumerator1.GetDataSources()

        DataGridView1.DataSource = datatable1

Thanks

推荐答案

您好,



我会做它是一种不同的方式。在长时间操作开始之前打开表单并在之后关闭它没有任何问题,但在某些情况下,您可能会遇到一些冻结的UI问题。



我建议你有看一下backgroundworker类,它是一种非常简单的线程管理方式。

http://msdn.microsoft.com/en-us/library/4852et58.aspx [ ^ ]



下面的代码也使用委托来更新UI线程。

这是一篇文章解释你如何:

初学者代表指南 [ ^ ]



并适用于您的问题:

Hello,

I would do it a different way. There is nothing wrong in opening a form before your long operation starts and closing it after, but in some cases you may get some frozen UI issues.

I recommend you have a look at the backgroundworker class it is a very easy way to manage threading.
http://msdn.microsoft.com/en-us/library/4852et58.aspx[^]

The code below also uses a delegate to update the UI thread.
Here is a article explaining how to you this:
A Beginner's Guide to Delegates[^]

And applied to your issue that looks like that:
Dim bw As BackgroundWorker = New BackgroundWorker
Public Delegate Sub PictureVisibilityDelegate(ByVal visibility As Boolean)
Dim ChangePictureVisibility As PictureVisibilityDelegate

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AddHandler bw.DoWork, AddressOf bw_DoWork
    AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
    ChangePictureVisibility = AddressOf ChangeVisibility
End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not bw.IsBusy = True Then
            bw.RunWorkerAsync()
        End If
    End Sub


Public Sub ChangeVisibility(ByVal visibility As Boolean)
    PictureBox1.Visible = visibility
End Sub

Private Sub bw_DoWork(sender As Object, e As DoWorkEventArgs)
    Me.Invoke(ChangePictureVisibility, True)
    Dim sqldatasourceenumerator1 As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
    Dim datatable1 As DataTable = sqldatasourceenumerator1.GetDataSources()
    DataGridView1.DataSource = datatable1
End Sub

Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    Me.Invoke(ChangePictureVisibility, False)
End Sub











Valery。






Valery.


Windows窗体解决方案



1.添加一个Picture Box控件到您的Windows窗体。

2.居中图片Windows窗体上的框。

3.将动画GIF放入图片框。在属性窗口中使用InitialImage属性导入动画GIF。



在Form Load事件处理程序中,

put:
Windows Form solution

1. Add a Picture Box control to your Windows Form.
2. Center the Picture Box on your Windows Form.
3. Put your Animated GIF into the Picture Box. Use InitialImage property in the properties window to import your animated GIF.

In the Form Load event handler,
put:
pbSearching.Visible = False
pbSearching.Enabled = True
Me.BringToFront()





准备好了,使动画GIF可见:



When ready, make Animated GIF visible:

pbSearching.Visible = True
pbSearching.BringToFront()





完成活动,使动画GIF不可见:



When done with activity, make Animated GIF invisible:

pbSearching.Visible = False
Me.BringToFront()





使用代码的示例:



Example using your code:

pbSearching.Visible = True
pbSearching.BringToFront()
Dim sqldatasourceenumerator1 As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
Dim datatable1 As DataTable = sqldatasourceenumerator1.GetDataSources()
DataGridView1.DataSource = datatable1
pbSearching.Visible = False
Me.BringToFront()





您可以在 AnimatedGIF.net


也许网络活动使线程保持忙碌(持有锁,例如)所以动画GIF没有时间运行。如果这是原因,那么您可能必须创建一个新表单来保存带有动画GIF的PictureBox并实例化然后将该表单显示为非模态窗口,如下所示。



----在您的代码之前-----
Perhaps the network activity keeps the thread busy (holding a lock, for example) so the animated GIF does not get any time to run. If this is the cause, then you may have to create a new form to hold the PictureBox with the animated GIF and instantiate then show that form as a non-modal window as demonstrated below.

----Before your code-----
Dim GifForm as New AnimatedGifForm
GifForm.Show



----您的代码----


----Your code----

Dim sqldatasourceenumerator1 As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
Dim datatable1 As DataTable = sqldatasourceenumerator1.GetDataSources()
DataGridView1.DataSource = datatable1



----代码后-----


----After your code-----

GifForm.Close





这样动画Gif就会在一个单独的线程上运行。



That way the animated Gif is running on a separate thread.


这篇关于在执行代码时显示动画gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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