Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
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:

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

        DataGridView1.DataSource = datatable1

Thanks
Posted
Updated 12-Mar-18 6:36am

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:
VB
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.
 
Share this answer
 
v3
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()


When ready, make Animated GIF visible:
pbSearching.Visible = True
pbSearching.BringToFront()


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()


You can find some animated GIFs on AnimatedGIF.net
 
Share this answer
 
v4
Comments
Furqan Sehgal 27-Jan-13 9:42am    
Thanks ! but the picture box is visible only when process has ended. It does not show while process is running.
Mike Meinz 27-Jan-13 9:59am    
Make it visible and BringToFront before you start the activity. I use this code and it works fine. If you still have problems, post your code.
Mike Meinz 27-Jan-13 10:05am    
I revised the solution. Please review.
Mike Meinz 27-Jan-13 10:18am    
The documentation for GetDataSources (http://msdn.microsoft.com/EN-US/library/vstudio/system.data.sql.sqldatasourceenumerator.getdatasources(v=vs.100).aspx) indicates that GetDataSources searches the network for SQL Servers. Perhaps the network activity keeps the thread busy (holding a lock, for example) so the animated GIF does not get allocated any time to run.
sampath1750 31-Oct-17 8:25am    
Hi, I have added same code in button click, but Picture box is visible after complete the button click process.

Thanks
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


That way the animated Gif is running on a separate thread.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900