Invalid VB.NET transverse operation
I went through the site and the questions I found related to this question were for C # (the app I maintain is written in VB.NET), so I apologize if I'm not missing out.
This is where I call 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 very well, but I get a cross-flow error when the program tries to switch the cursor to the default. What can I do to fix this problem?
source to share
Your way of calling the method on the owner thread (GUI) is wrong. If a call is required, you don't have to execute the rest of the code in the method. If you do this, you will be executing it in both the GUI thread and the background thread, and when you try to access the GUI elements from the background thread, you will get a cross thread error.
The call 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 do you run the method on a background thread when it should still call itself on the GUI thread?
source to share
You have created a helper thread that is not allowed to be called into the user interface. You can set the cursor directly from the UI-Thread.
To do this, you need to fire an event from your helper thread, which tells the UI thread that your work is complete and that it can set the cursor back, or alternatively, do it using Invoking on the UI-Thread:
private void ResetCursor()
{
this.Cursor = Cursor.Default;
}
private delegate void UpdateCursor();
private void SaveData()
{
//Do your work here
if(this.InvokeRequired)
{
this.Invoke(new UpdateCursor(ResetCursor));
}
else
{
ResetCursor();
}
}
source to share
In a multithreaded Windows Forms application, it is illegal to call a method or property on a control from any thread other than the one that created it. All cross-threading calls must be explicitly bound to the thread that created the control (usually the main thread) using the Control.Invoke or Control.BeginInvoke method.
Here is a web page you can use to help you with this problem:
http://www.dreamincode.net/forums/showtopic35616.htm C #
http://www.codeproject.com/KB/vb/ISinchronizedInvoke.aspx VB.NET
source to share
This webpage has the stripped down code required to access the form control from a separate thread: http://www.databatrix.com/2009/09/cross-thread-operation-not-valid-net_4280.html?q= cross + thread