Hi all.
Can someone push or punch me in the right direction here Using the CF 2.0 I have an application that is doing an async poll of a TCP listener for data. When the data arrives, I want to update certain forms with the values.
In a nutshell,
The data arrives. I determine what type of data it is and where it should display. I populate properties of a class with the data.
I then raise an event to signal the data is here and the properties have been populated.
It is here where my problems start. Based on the the data I will determine what form I need to interact with. I need to call a sub from within that form to populate the controls on it.
I was using invoke to call the sub , but the data would not display because the form displayed was not the same instance as the invoked.
If I call invke on the sub of the form that is the current instanced displayed, I get a error (
{"Control.Invoke must be used to interact with controls created on a separate thread."}
)
Any suggestions
regards,
woyler

control.invoke
cbpd86
Thank you all for your responses. I did get it working. Thanks again for your input.
regards,
woyler
sobo1
You have a delegate called Search of type MyEvents.
You are calling Search.Invoke().
Search is not a Control. It is a delegate.
Delegate.Invoke and Control.Invoke are not the same.
You need an instance of your form (which derives from Control). Call Invoke on that.
JonM
Chances are: you're not actually using Control.Invoke() for at least some control interaction.
So, as Tim suggested, please post relevant code and stack trace so we can Identify the problem.
Oh, by the way, any change this form was created on a worker thread < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Kevin K.
Please read the post. I used control.invoke. The result it gives me is the problem.
DTXNA
I thank you for your responses. I tried to strip out irrelevant code for readability. I hope it makes sense.
Thanks in advance for all your help.
'Global
Delegate Sub MyEvents()
Startup form:
Private Sub frmMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim thr As New Threading.Thread(AddressOf CreateConnection)
thr.Start()
end sub
Public Sub CreateConnection()
Dim sSend As String
Try
client.Connect(gsHost, giPort)
nwStream = client.GetStream
nwStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
sSend = ds_CommRequest.Connect & "|" & System.Net.Dns.GetHostName
SendData(sSend)
.......
End Sub
Private Sub DoRead(ByVal ar As IAsyncResult)
Try
Dim numBytesRead As Integer = nwStream.EndRead(ar)
Dim returndata As String = Encoding.ASCII.GetString(readBuffer, 0, numBytesRead)
If ProcessCommands(returndata) Then
nwStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
End If
End Try
End Sub
////The ProcessCommands sub looks at the first char of the return data to determine what need to happen.
Private Function ProcessCommands(ByVal strMessage As String) As Boolean
Dim i_EventIndex As Int32 = 0
Dim dataArray() As String
Try
dataArray = strMessage.Split(Chr(124))
Select Case Convert.ToInt32(dataArray(0))
Case 'Search
MyClass.mo_ValueOne = dataarray(1)
FireEvent(1) 'Search
end select
end try
end function
////After is does that I pass an enum value to a FireEvent method of one of my classes. That code is below
////In this test my enum value denotes I am doing a search
Sub FireEvent(byval _eventID as int16)
'_eventId = Search
Dim Search As MyEvents
''' I have a class that stores the instance of the forms as they are created called Forms
''' If I use Forms.Search.HandleSearchComplete (current instance of the Search form )
''' I will get the use control.invoke error
Search = New MyEvents(AddressOf Forms.Search.HandleSearchComplete)
''' If I use frmSearch.HandleSearchComplete (just calling the sub with the form qualifier)
''' The code does not error out, but the value does not display on the loaded form
Search = New MyEvents(AddressOf frmSearch.HandleSearchComplete)
Search.Invoke()
End Sub
Class frmSearch
Sub HandleSearchComplete()
Try
lblOne.Text = MyClass.mo_ValueOne
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
end class
//////////My startup form contains just command buttons. When I click on one,
////////// I create a form from the click event. I have a class to store the instance of each form
Private Sub cmdLogSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogSearch.Click
If Forms.Search Is Nothing Then
Dim fSearch As New frmSearch
Forms.Search = fSearch
End If
Forms.Search.Show()
Forms.Search.BringToFront()
End Sub
//// The form I am testing is a search form. below is the code for the search.
Private Sub Search()
Try
SendData(ds_CommRequest.SearchByLog & "|" & txtLog.text)
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
Cursor.Current = System.Windows.Forms.Cursors.Default
End Try
End Sub
F.Costa
you are going to need to post some code that shows what you are doing.
PatK
Please use Control.Invoke(). If you don't know what it is or how to use it, please look it up on MSDN or search this forum.
Hardrock302
I don't see any Control.Invoke() in this code. That's how it supposed to look (from http://msdn2.microsoft.com/en-us/library/zyzhdc6b.aspx):
' The following code assumes a 'ListBox' and a 'Button' control are added to a form,
' containing a delegate which encapsulates a method that adds items to the listbox.
Public Class MyThreadClass
Private myFormControl1 As MyFormControl
Public Sub New(myForm As MyFormControl)
myFormControl1 = myForm
End Sub 'New
Public Sub Run()
' Execute the specified delegate on the thread that owns
' 'myFormControl1' control's underlying window handle.
myFormControl1.Invoke(myFormControl1.myDelegate)
End Sub 'Run
End Class 'MyThreadClass