Answered *.gif image in picturebox "freezes" when searching for file on server.

dbtryout

Member
Joined
Nov 14, 2018
Messages
14
Programming Experience
Beginner
Hey People,

New question.

I have a verification screen where all the buttons pressed-information is stored in text-boxes.
Once pressed the "save to database" button, he's looking up on a server in a folder if a particular excel file with current year and week-number exist.
2 options:
  1. The file exist: write the information from the textboxes in the last row of the existing excel file.
  2. The file doesn't exist: create a file and add information from textboxes to the first free line available.

No problems with that.

The problem is because of the wireless internet connection searching does take a bit longer.

So i'm adding a Picturebox with a gif image that would show a please wait animation.
I call the gif animation to hide when the verification screen opens.
I call the gif animation to show when the "save to database" button is pressed.

So it hangs when looking on the server for the file-search.

I thought, let me introduce a timer with 2 sec before looking on the server and see how that works out. Unfortunately when the 2 seconds are out and the code starts, the gif file stops again untill the code stops.

Any help?

This is the code:

VB.NET:
Public Class VerificationScreen
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        PictureBox1.Show()
        Timer1.Interval = 2000 'sets the timer's tick interval to 2 sec
        Timer1.Enabled = True 'the timer is disabled on form load so you have to enable it
    End Sub
    Private Sub Timer1_tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = False
        Dim TargetWorkbook As Excel.Workbook = Nothing
        Dim SourceWorkbook As Excel.Workbook = Nothing
        Dim Target_Path As String
        Dim strFile As String
        strFile = TextBox6.Text
        If Len(strFile) = 0 Then Exit Sub


        '''''Assign the Workbook File Name along with its Path
        '''''Change path of the Target File name
        Target_Path = "S:\Quality\~SIPSTATION~\BACKUP FILES\PC1\" & Date.Today.Year & strFile & ".xlsx"
        If Len(Dir(Target_Path)) = 0 Then
            MsgBox("File does not exist, creating new file...")
            Call NewWb()
        Else
            Call AdjustWb()
        End If
    End Sub
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
If you're doing other work on the UI thread, the UI thread cannot animate the image. You should always be doing long-running tasks in the background so the UI thread can look after the UI.
 

dbtryout

Member
Joined
Nov 14, 2018
Messages
14
Programming Experience
Beginner
The problem I'm dealing with is that I can't close the main form from another thread while using the backgroundworker method
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
So do it on the UI thread. The BackgroundWorker has a ReportProgress method that can be called in the DoWork event handler to raise the ProgressChanged event on the UI thread. Any code you put in the ProgressChanged event handler is executed on the UI thread. Despite the name, you can use that method and that event for doing anything you want. If the only thing you want to do is close the form then just call ReportProgress and pass zero as the progress value, then call Close in the ReportProgress event handler. If you want to be able to do different things in the ReportProgress event handler, pass a second parameter to ReportProgress that identifies what you want to do. You might define an Enum for that. You can then test that value in the ReportProgress event handler to decide what to do. You can check out my own examples here:

http://www.vbforums.com/showthread.php?471889

Post #4 in that thread uses the UserState to pass data from the DoWork event handler to the ProgressChanged event handler. The data is different in that case but you can pass whatever you want via that parameter.
 
Top Bottom