Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
Posted 27-Jan-13 1:37am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v4
Comments
kfsehgal at 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 at 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 at 27-Jan-13 10:05am
   
I revised the solution. Please review.
Mike Meinz at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 446
1 OriginalGriff 380
2 Yogesh Kumar Tyagi 304
3 Prakriti Goyal 200
4 Maciej Los 165
0 OriginalGriff 6,472
1 Sergey Alexandrovich Kryukov 5,404
2 Maciej Los 3,434
3 Peter Leow 3,259
4 DamithSL 2,495


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100