Resolved Help. Timer on form not working. Can't figure out why.

Mugsy

Member
Joined
Sep 30, 2020
Messages
7
Programming Experience
10+
I needed to create a Copy Dialog from scratch. It will (I hope) show the file/folder being copied with a Copy Animation (similar to the default UIDialog.)

I created a form and put a PictureBox & Timer on it. I set the Timer default to Enabled and Interval to 150. Code was added (below) to the "_Tick" event to change the picbox source image (5 frames.)

When I run my program, and perform a frmCopyAni.Show (followed by some file copy commands), the form appears and the computer starts copying in the background, but the timer never runs (I put a break in the Tick event but it's never triggered.) I added Timer.Enabled to Form_Load but it didn't help.

Timer start/end:
            frmCopy.Timer1.Start()
            BackupData()                    ' Backup settings.
            frmCopy.Timer1.Stop()

...calls...

frmCopy:
Public Class frmCopy

    Private Sub frmCopy_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        intTick += 1
        If intTick > 5 Then intTick = 1
        picCopyAni.Image = imgCopyAni.Images.Item(intTick)
    End Sub

End Class

I'm not sure what I'm doing wrong (if anything.) I think the timer won't run while FileCopy is taking place. Any help is appreciated.
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,746
Location
Sydney, Australia
Programming Experience
10+
If you are doing the copying on the UI thread then it's too busy doing that to raise and handle the Tick event. You would need to be doing the copying on a secondary thread.
 

Mugsy

Member
Joined
Sep 30, 2020
Messages
7
Programming Experience
10+
Thx for the reply. Here is my Copy code:

VB.NET:
        For Each myFile In Directory.GetFiles(strPath & "Save", "Data*.*")
            Try
                My.Computer.FileSystem.CopyFile(myFile, strBackupPath & My.Computer.FileSystem.GetName(myFile), True)   ' Must trim path from source file otherwise Crash. No idea why. Using Win copy dialog prompts to overwrite EVERY SINGLE FILE.
                intCnt += 1
                frmCopy.lblFileNum.Text = intCnt.ToString
                frmCopy.lblSrcCopy.Text = myFile.ToString
                frmCopy.Refresh()
            Catch
                ' Attempting to copy "~Temp" files causes an error after the fact. Ignore.
            End Try
        Next

I'm not sure how to perform a "secondary" thread.
 

chrisjlocke1

Member
Joined
Apr 12, 2022
Messages
18
Programming Experience
10+
Just to reverse the direction ... rather than solve the problem that you think you're having, what are you trying to solve? Why are you having a timer kick off during a copy operation?
Can you explain the 'I'm trying to ....' ?

I'm worried you're asking how to get to Brighton from Sheffield, but in reality, you're trying to get to Devon.
 

Mugsy

Member
Joined
Sep 30, 2020
Messages
7
Programming Experience
10+
I'm not sure what your question is but the problem has been resolved.

I had to create a custom copy dialog b/c the built-in Copy function that includes the animated Windows progress indicator stops & prompts to "overwrite" every duplicate file b/c there is no "overwrite" switch using this method.

I wanted my copy operation to include an animation & display filenames so the user isn't just staring at a Busy icon for seven minutes. And the only way to do this was to put the animation in a separate thread during the copy process.
 

chrisjlocke1

Member
Joined
Apr 12, 2022
Messages
18
Programming Experience
10+
> but the problem has been resolved

Oh ... it helps if you mark the thread as 'Answered' or 'Resolved' and finalise it, otherwise idiots like me think its' still open and try and help. :|
Does an animation really help when copying? Is a user going to stare at your screen constantly for seven minutes? So long as there is a progress indication (file x of y) the user is going to alt+tab and do something else, surely?
 

Mugsy

Member
Joined
Sep 30, 2020
Messages
7
Programming Experience
10+
My apologies. My question was eventually answered on another site and I forgot to close my post over here.

The animation looks nice & more professional, but more than that, if you don't use multiple threads, even just displaying the filenames doesn't work as the form does not update when it is busy copying.

People get anxious and start wondering if the program crashed if there is no indication of activity in as little as 30 seconds.
 

Latest posts

Top Bottom