Click here to Skip to main content
15,923,376 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: My Query get Garbaze Value Pin
Guffa7-Jun-08 3:54
Guffa7-Jun-08 3:54 
GeneralRe: My Query get Garbaze Value Pin
Anubhava Dimri9-Jun-08 0:41
Anubhava Dimri9-Jun-08 0:41 
GeneralRe: My Query get Garbaze Value Pin
Guffa9-Jun-08 6:13
Guffa9-Jun-08 6:13 
GeneralRe: My Query get Garbaze Value Pin
Chinners9-Jun-08 6:17
Chinners9-Jun-08 6:17 
Questionget price value in descending order which is in xml Pin
subbu.sk5-Jun-08 19:59
subbu.sk5-Jun-08 19:59 
AnswerRe: get price value in descending order which is in xml Pin
Mycroft Holmes8-Jun-08 14:31
professionalMycroft Holmes8-Jun-08 14:31 
QuestionEnable "Show window contents while dragging" option in code??? Pin
puffdaddy4115-Jun-08 19:39
puffdaddy4115-Jun-08 19:39 
QuestionHow can I make a event raise and update controls on a form from an asynchronous socket ? Pin
Noctris5-Jun-08 12:16
Noctris5-Jun-08 12:16 
Hi All,

First off all, I want to THANK everyone on this site for whatever contribution they delivered. As a starting Developer ( used-to-be sysadmin) I can't even count anymore how many articles here i've read which helped me out A LOT.

But now. i'm stuck...

I've written an application using an ASync SocketClient which communicates with a server. Now that it has become clear that several applications should communicate with the same TCP server using same protocol and logic, i wanted to pull the socketclient from the app, built a dll from it and "voila".. however, I'm having a problem with Cross-Thread errors..

What i've done:

My Dll
Public Class NoctrisClient

Public Event ClientConnected()
Public Delegate Sub SimpleCallBack()
Public isConnected as boolean = false


Private endpoint as new IPEndPoint(system.net.dns.GetHostEntry("my.server.com").addresslist(0),1234)
Private SocketClient as Socket

Public Sub New()

me.connect()

End sub
Public sub Connect()

 ClientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
 ClientSocket.BeginConnect(endpoint, AddressOf Connected, Nothing)

end sub

Private Sub Connected(ByVal ar As IAsyncResult)
        Try
            ClientSocket.EndConnect(ar)
            '-- Ok, we are connected
            EventClientConnected()
            '-- Start Receiving Data
            ClientSocket.BeginReceive(RecvBuffer, 0, RecvBuffer.Length, _
                SocketFlags.None, AddressOf ReceivedData, Nothing)

        Catch ex As Exception
            '-- Call DisconnectedUI
           ' Make call to raise ClientDisconnectedEvent
' EventClientDisconnected()
        End Try
    End Sub

'... and so on.. all code from the socketclient is ok..it worked before so...

Private Sub RaiseClientConnected()
        RaiseEvent ClientConnected()
        isConnected = True
    End Sub

    Private Sub EventClientConnected()
' Doing this here because i call these events on several places and did not wanted to keep repeating code.
        Dim cb As New SimpleCallback(AddressOf RaiseClientConnected)
        cb.Invoke()
    End Sub

end class


So this, in a VERY short nutshell, is the socketclient component..

So far, so good.. the events are raised as they should... But ...

Then the form does something like this:

imports my.namepace.NoctrisClient

Public Class Myform

Private WithEvents Client as new NoctrisClient
Private Delegate Sub SimpleCallBack()

Private Sub HandleClientConnected() Handles Client.ClientConnected
        Dim cb As New SimpleCallBack(AddressOf ConnectedUi)
        cb.Invoke()
End Sub

Public Sub ConnectedUi()
        Try
            LblServerStatus.Text = "Connected"
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
End Sub
end class


So as you can see, in my form HandleClientConnected handles the event from the dll and does a callback to ConnectedUi to change the control(s) on the form..

However, I keep getting Cross-thread exceptions:

System.InvalidOperationException: Cross-thread operation not valid: Control 'LblServerStatus' accessed from a thread other than the thread it was created on.
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.set_WindowText(String value)
at System.Windows.Forms.Control.set_Text(String value)
at System.Windows.Forms.Label.set_Text(String value)
at tp2player.FrmStandardPlayer.ConnectedUi() in D:\Development\solution\project\Forms\MyForm.vb:line 35


I've been reading a lot about it ( ok.. i admit, i don't always understand the articles either Wink | ;-) )
but i just can't see where i am going wrong ? Confused | :confused:

I've tryed different ways for callback and direct but they all keep ging errors :s (only messagebox, which doesn't care about threads anyways, works..)

Could someone please shine a little light on it ?

Many thanks !
AnswerRe: How can I make a event raise and update controls on a form from an asynchronous socket ? Pin
nlarson115-Jun-08 15:50
nlarson115-Jun-08 15:50 
QuestionImage in a datagrid ASP.Net VB Pin
eriditu5-Jun-08 12:14
eriditu5-Jun-08 12:14 
AnswerRe: Image in a datagrid ASP.Net VB Pin
Christian Graus5-Jun-08 17:20
protectorChristian Graus5-Jun-08 17:20 
QuestionFilterFor menu option in VBA runtime Pin
Member 14787615-Jun-08 8:28
Member 14787615-Jun-08 8:28 
QuestionMailing send error due to ... !!! Pin
John Kh5-Jun-08 8:05
John Kh5-Jun-08 8:05 
AnswerRe: Mailing send error due to ... !!! Pin
Fu Manchu5-Jun-08 10:10
Fu Manchu5-Jun-08 10:10 
GeneralRe: Mailing send error due to ... !!! Pin
John Kh5-Jun-08 19:02
John Kh5-Jun-08 19:02 
QuestionNavigation Pane Pin
felipeoroz5-Jun-08 5:16
felipeoroz5-Jun-08 5:16 
AnswerRe: Navigation Pane Pin
Christian Graus5-Jun-08 5:42
protectorChristian Graus5-Jun-08 5:42 
QuestionGetting error while using WSAStartup method Pin
Chaitanya kumar CVSS5-Jun-08 1:54
Chaitanya kumar CVSS5-Jun-08 1:54 
Questiondisplay information on list box when treeview node is selected Pin
Mamphekgo Bahula5-Jun-08 1:24
Mamphekgo Bahula5-Jun-08 1:24 
AnswerRe: display information on list box when treeview node is selected Pin
Ashfield5-Jun-08 3:21
Ashfield5-Jun-08 3:21 
AnswerRe: display information on list box when treeview node is selected Pin
Anubhava Dimri5-Jun-08 18:44
Anubhava Dimri5-Jun-08 18:44 
QuestionHOW TO CREATE YOUR OWN MEDIA PLAYER!!!!!!!!!!!!!!!! Pin
spinky5-Jun-08 1:12
spinky5-Jun-08 1:12 
AnswerRe: HOW TO CREATE YOUR OWN MEDIA PLAYER!!!!!!!!!!!!!!!! Pin
Johan Hakkesteegt5-Jun-08 3:16
Johan Hakkesteegt5-Jun-08 3:16 
GeneralRe: HOW TO CREATE YOUR OWN MEDIA PLAYER!!!!!!!!!!!!!!!! Pin
MarkB7775-Jun-08 21:05
MarkB7775-Jun-08 21:05 
AnswerRe: HOW TO CREATE YOUR OWN MEDIA PLAYER!!!!!!!!!!!!!!!! Pin
Ashfield5-Jun-08 3:24
Ashfield5-Jun-08 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.