Hi all,
I'm writing a small desktop app that resizes images for posting online and i've hit a small problem with the interface freezing when I am performing a calculation to determine the combined converted filesize of images to be processed.
I'm performing the calculation whenever the trackbar on my app is changed with a timer and a backgroundworker.
The backgroundworker then performs the calculation and changes the value of a label on the form in a threadsafe manner.
The boolean runFileSizeCalculation
is then reset back to true in the bwCalculator_RunWorkerCompleted
routine.
Whilst the worker is doing it's thing though, my interface freezes. Isn't the purpose of the backgroundworker to stop this?
I'm new to desktop programming so be gentle. I'm a web developer normally I just thought this would be a bit of fun and maybe something useful I could share afterwards.
Many thanks in advance.
Ok ..... First, Thanks Rick and John for your info.
I was already using invoke to allow the update to the label and I've had a bit of a read of the other info. Here's what I've thrown together.
Private Sub tbRatio_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbRatio.ValueChanged
lblRatio.Text = String.Format("{0}%", tbRatio.Value)
reductionRatio = tbRatio.Value
If filteredList IsNot Nothing Then
If runFileSizeCalculation Then
runFileSizeCalculation = False
Dim t As New Thread(AddressOf ThreadingTest)
t.Start()
End If
End If
End Sub
Private Sub ThreadingTest()
Dim dueTimeSpan As New TimeSpan(0, 0, 3)
latencyTimer = New Threading.Timer(AddressOf SetOutputFolderSize, Nothing, dueTimeSpan, infiniteTimeSpan)
End Sub
Delegate Sub SetOutputFolderSizeCallback()
Private Sub SetOutputFolderSize()
If lblOutputSize.InvokeRequired Then
Dim d As New SetOutputFolderSizeCallback(AddressOf SetOutputFolderSize)
Invoke(d)
Else
lblOutputSize.Text = (String.Format("{0} MB", GetOutPutFolderSize(filteredList).ToString))
runFileSizeCalculation = True
End If
End Sub
The
GetOutPutFolderSize
function is the one that performs the actual calculation.
I'm still getting the same freezing problem.... Am I missing something obvious?
Thanks again.
***********************************************************************
Thanks again Rick, your working was much more sensible and has stopped the UI freeze.
The reason for using the timer was to run the function after a set delay so that when you change the trackbar value more than once in less than the time it took to calculate the foldersize it would hopefully catch only the last value and run the calculation based upon that.
Can you think of a better way of doing that?