Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
697 views
in Technique[技术] by (71.8m points)

multithreading - Cross-Thread operation not valid VB.NET

I looked around the site and the questions I found relating to this subject were for C# (the application that I am maintaining is written in VB.NET), so I apologize if I overlooked one.

Here is where I am calling my thread:

Private Sub saveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveBtn.Click
    If Not LoadedFilePath = String.Empty Then
        Dim oTrd = New Threading.Thread(AddressOf SaveData)
        oTrd.Start()
    End If
End Sub

And here are the methods:

Private Sub SaveData()
    SaveData(LoadedFilePath)
End Sub
Private Sub SaveData(ByVal filepath As String)
    If InvokeRequired Then
        Me.Invoke(New MethodInvoker(AddressOf SaveData))
    End If
    Try
        Me.Cursor = Cursors.WaitCursor
        Dim oSettings As New SettingsClass(filepath)
        Dim oEnc As New AES
        With oSettings
            //' Code removed for brevity
        End With
        oEnc = Nothing
        oSettings.SaveSettings()
        savedLbl.Visible = True
        If SavedTimeout IsNot Nothing Then
            Try
                SavedTimeout.StopEvent()
            Catch
            End Try
        End If
        SavedTimeout = New TimedEvent(Now.AddSeconds(5))
        SavedTimeout.StartEvent()
        Me.Cursor = Cursors.Default
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

The save function works just fine, but I get the cross-thread error when the program tries to switch the cursor back to default. What can I do to fix this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your way of invoking the method in the owner (GUI) thread is wrong. If invocation is required, you should not execute the rest of the code in the method. If you do, you will be executing it both in the GUI thread and the background thread, and when you try to access the GUI elements from the background thread you get the cross-thread error.

The invocation should look like this:

Private Sub SaveData(ByVal filepath As String)
   If InvokeRequired Then
      Me.Invoke(New MethodInvoker(AddressOf SaveData))
   Else
      ... the actual code
   End If
End Sub

But why are you starting the method in a background thread, when it has to invoke itself in the GUI thread anyway?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...