I'm getting this error:

The calling thread cannot access this object because a different thread owns it

On this part of code:

Private Sub loginWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles loginworker.DoWork
        Try
            eFunctions._ValiAcc(My.Settings.adress, tbuser.Text, tbuser.Text & tbpwd.Password, eResult)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!")
        End Try
    End Sub

Can someone help?

Recommended Answers

All 16 Replies

Can you please make me code? I tried few ways and failed, I'm coding in wpf and always had problems with that. Please?

Let's say you want to modify the value of a label from a background thread. You can't do that directly so you create a delegate. At the class level in the maini form you do something like

Private Delegate Sub dlgUpdateStatus(text As String)

and in the main thread you have the sub

Private Sub UpdateStatus(text As String)

    'The InvokeRequired method returns True if the thread that calls
    'it is running from a different thread. In that case we create a
    'delegate to do the update of the control's text otherwise we can
    'update the text directly.                                                         
    If lblStatus.InvokeRequired Then
        Dim dlg As New dlgUpdateStatus(AddressOf UpdateStatus)
        Me.Invoke(dlg, text)
    Else
        lblStatus.Text = text
    End If

End Sub

You can call this sub from the main thread or any other thread and it will do the update properly.

Private Delegate Sub dlgMySub(addr as String, user as string, pass as string, eResult as String)

Private Sub MySub(addr as String, user as string, pass as string, eResult as String)

    If efunctions.InvokeRequired Then
        Dim dlg As New dlgMySub(AddressOf MySub)
        Me.Invoke(dlg, addr, user, pass, eResult)
    Else
        eFunctions._ValiAcc(addr, user, pass, eResult)
    End If

End Sub
Private Sub loginWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles loginworker.DoWork
        Try
            MySub(My.Settings.adress, tbuser.Text, tbuser.Text & tbpwd.Password, eResult)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!")
        End Try
    End Sub

You managed to take a look more in it?

I posted a reply but it disappeared. Is it possible either eFunctions._ValiAcc is being called from two different threads at the same time, or that the code in eFunctions._ValiAcc is trying to modify a foreground control? The second scenario is usually what causes the error you are seeing.

Want me to send you whole source to check up? Since its not changeing anything :S

That depends. Is it very large? I'm not really keen on wading through several thousand lines of possibly undocumented code. And is it something I can run on my computer without setting up a special environment? I would also have to be vb.net 2010. How about just posting the code for the eFunctions class to start?

its like 50 lines not more :)

I sent you source in pm :)

@Reverend Jim Soo,as i can see from our private messages you can't fix it?

You sent me Dragon.rar which does not contain the code that declares WebRequest. Unless I know what WebRequest is I can't say whether or not I have a solution.

How you say that it doesnt contain code to declare it? o.O
WHat is this then:

Private _Req As HttpWebRequest

 _Req = WebRequest.Create( ) 

Its in efuncs.vb ...

That tells me that _Req is of type HttpWebRequest. That does not tell me the type of WebRequest. I am assuming the error is caused by accessing WebRequest from a background thread. I am also assuming WebRequest is an instance of a control (I didn't see a declaration so I don't know) If that is the case then the following might work (and it might not). If WebRequest does not support InvokeRequired then this will not work.

Main Thread

Private Delegate Function dlgMakeWebRequest(req As String) As HttpWebRequest

Private Function MakeWebRequest(req As String)

    If WebRequest.InvokeRequired Then
        Dim dlg As New dlgMakeWebRequest(AddressOf MakeWebRequest)
        Return Me.Invoke(dlg, req)
    Else
        Return WebRequest.Create(req)
    End If

End Sub

Background thread

Public Function _ValiAcc(ByVal url As String, ByVal usrname As String, ByVal passwrd As String, ByRef result As String)
    _Req = MakeWebRequest(url & "login.php?u=" & usrname & "&p=" & MD5_encode(passwrd))
    _Resp = _Req.GetResponse
    _GStream = _Resp.GetResponseStream
    _ReadS = New StreamReader(_GStream)
    result = _ReadS.ReadToEnd()
    Return result
End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.