jrscribner 0 Newbie Poster

First off let me start by saying I'm not a programmer, but envy those who can. My programming style consists of find someone elses code or tutorial and making it fit my own. Currently I'm working on a project where I can send a command from another computer thru the network and control the client program(Setting Control Properties mostly) and the UDP networking code runs as a seperate thread, and I'm trying to pass the command from the thread back to the main thread and then execute a subroutine to process the command. As you can see I pass my command to my ProcessCommand sub routine, and some command pass thru fine and others throw a "Cross-thread operation not vaild: Control 'frmPresenter' access from a thread other than the thread it was created on' error. If read that in order to pass information from one thread to another you need to sync the threads or raise an event but I'm not sure on how to do it. Any help you be greatly appreciated.

Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Windows.Forms
Public Class frmPresenter

    Dim udpClient As New UdpClient
    Dim GLOIP As IPAddress
    Dim GLOINTPORT As Integer
    Dim bytCommand As Byte() = New Byte() {}
    Dim pRet As Integer
    Dim Debug As Boolean = 1
    Public receivingUdpClient As UdpClient
    Public RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
    Public ThreadReceive As System.Threading.Thread
    Dim SocketNo As Integer
    Dim RemoteCommands As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            SocketNo = 40009
            receivingUdpClient = New System.Net.Sockets.UdpClient(SocketNo)
            ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages)
            ThreadReceive.Start()
            '         MsgBox("Receiving")
        Catch x As Exception
            MsgBox(x.Message)
        End Try

    End Sub

    Public Sub ReceiveMessages()
        Try
            Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint)
            Dim ReceivingIP As String = RemoteIpEndPoint.Address.ToString
            Dim BitDet As BitArray
            'Dim Command As String
            BitDet = New BitArray(receiveBytes)

            Dim strReturnData As String = System.Text.Encoding.Unicode.GetString(receiveBytes)
            
            RemoteCommands = Encoding.ASCII.GetChars(receiveBytes)
            MsgBox(RemoteCommands)
            ProcessCommand(RemoteCommands)
          
            NewInitialize()
        Catch e As Exception
            MsgBox(e.Message)
        End Try
    End Sub
    Public Sub ProcessCommand(ByVal Command As String)
        Dim Commands() As String

        Commands = Command.Split("&")
        MessageBox.Show("Processing Command: " & Commands(0))
        If Commands(0) = "AxisCAM" Then AxisCam(Commands(1))
        If Commands(0) = "AxisCAMHide" Then AxisCamHide()
        If Commands(0) = "AxisCAMShow" Then AxisCamShow()
       End Sub
   
    Sub AxisCam(ByVal ipText As String)
        ax.MediaURL = "http://" & ipText & "/axis-cgi/mjpg/video.cgi"
        ax.MediaType = "mjpeg"
        ax.AutoStart = True
        ax.Play()
        ax.EnableAreaZoom = True
    End Sub

    Sub AxisCamShow()
        ax.Visible = True
        ax.Play()
    End Sub
    Sub AxisCamHide()
        ax.Visible = False
        ax.Stop()
    End Sub

    Public Sub NewInitialize()
        MessageBox.Show("Initialize")
        ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages)
        ThreadReceive.Start()
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Try
            receivingUdpClient.Close()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class