Help Man Help Man - 6 months ago 48 Question

Prevent running sub routines twice for improve performance?

I have a winform that does file searching and display results in a datagridview. I also have picture boxes that display the file as well. The process: enter file name and click search, displays results in grid and pictures in picture box.

I want to be able to click search again but this time if it is the same file, i don't want to run my picture process therefore resulting in better performance. Not sure on how to achieve this?


You could use a Static variable and set/check it each time the method is called:

Public Sub SearchForFile(filename As String)
    Static lastFile As String = Nothing
        'don't run again if the same file is searched for
        If lastFile = filename Then Return

        'do your file searching here
        'always set the last filename to the one we just searched for
        lastFile = filename
    End Try
End Sub