两次阻止 运行 子例程以提高性能?

Prevent running sub routines twice for improve performance?

我有一个 winform 可以进行文件搜索并在 datagridview 中显示结果。我也有显示文件的图片框。过程:输入文件名点击搜索,结果以网格显示,图片以图片框显示。

我希望能够再次单击搜索,但这次如果是同一个文件,我不想 运行 我的图片处理因此会产生更好的性能。不确定如何实现这一点?

您可以使用一个静态变量,并且 set/check 每次调用该方法时都使用它:

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

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