Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this code is for client/server application it give me an exception in the client while i run the port between these 2 application i need help to solve this problem
"the 2 application are broadcast media player broadcast media over Lan to Client"
the sever code ;
Iwill type the place of exception in code in client
Imports System.Net.Sockets

Imports System.Threading
Imports System.Windows.Forms
Imports System.IO



Public Class broadcast



    Private thread As Thread
    Private listener As New TcpListener(5234)
    Private writers As New ArrayList
    Private name As String



    Public Sub New(ByVal name As String)
        MyBase.New()
        Me.name = name

    End Sub

    Public Sub start()
        listener.Start()
        thread = New Thread(AddressOf RunServer)
        thread.Start()
    End Sub


    Public Sub RunServer()
        Try
            While True
                Dim writer As New BinaryWriter(New NetworkStream(listener.AcceptSocket))
                writer.Write(name)

                writers.Add(writer)

            End While

        Catch inpitoutputException As IOException
            MessageBox.Show("Server application Closing")

        End Try
    End Sub


    Public Sub sendCommand(ByVal command As String)
        For i As Integer = 0 To writers.Count
            Try
                Dim writer As BinaryWriter
                writer = CType(writers.Item(i), BinaryWriter)
                writer.Write(command)
                writer.Flush()
            Catch inputputputexception As Exception
            End Try
        Next
    End Sub

    Public Sub stopAll()
        For i As Integer = 1 To writers.Count
            Try

                Dim writer As BinaryWriter
                writer = CType(writers.Item(1), BinaryWriter)
                writer.Close()
                writers.Remove(1)
            Catch inputputputexception As Exception
            End Try
        Next
        listener.Stop()



    End Sub
End Class

the Client code :
Imports System.Net.Sockets
Imports System.Windows.Forms
Imports System.IO
Imports System.Threading
Public Class recieve

    Private reader As BinaryReader
    Private message As String = ""
    Private frmObject As frmMain
    ' Dim k As String = ""C:\Users\hamzah\Desktop\BVP\BVP_client\BVP.txt
    Private readthred As Thread
    Public Sub New(ByVal frmObject As frmMain)
        MyBase.New()
        Me.frmObject = frmObject
        readthred = New Thread(AddressOf runClinet)
        readthred.Start()

    End Sub

    Public Sub runClinet()
        Dim client As TcpClient
        Try
            client = New TcpClient()
            client.Connect("127.0.0.1", 5234)
            reader = New BinaryReader(client.GetStream())
            Try
                Dim Path As String
                Path = reader.ReadString
                frmMain.ListBox1.SelectedItem = Path' it give me exception here
               
                frmObject.Playlist.SelectedItem = frmObject.ListBox1.SelectedItem' and sometime here
                frmMain.AxWindowsMediaPlayer1.URL = Path
                frmObject.ListBox1.Items.Add(Path)
                frmObject.Playlist.Items.Add(Path)
                Try
                    While True
                        Path = reader.ReadString
                        Select Case Path
                            Case "1"
                                frmObject.AxWindowsMediaPlayer1.Ctlcontrols.pause()
                            Case "2"
                                frmObject.AxWindowsMediaPlayer1.Ctlcontrols.play()
                            Case "3"
                                frmObject.AxWindowsMediaPlayer1.Ctlcontrols.stop()
                        End Select
                    End While
                Catch ex As Exception
                End Try
            Catch inputoutputException As IOException
            Finally
            End Try
            Try
                frmObject.AxWindowsMediaPlayer1.Ctlcontrols.stop()
                reader.Close()
                client.Close()
            Catch exx As Exception
            End Try
        Catch inputoutputException As IOException
        End Try
    End Sub
End Class
Posted
Updated 9-May-11 6:16am
v2
Comments
Marc A. Brown 9-May-11 11:56am    
You need to provide information about the exception you're getting, as well as where in the code you're getting it.
hamzah1 9-May-11 12:08pm    
An error occurred creating the form. See Exception.InnerException for details. The error is: ActiveX control '6bf52a52-394a-11d3-b153-00c04f79faa6' cannot be instantiated because the current thread is not in a single-threaded apartment. this is the inrenal exception marc
Sergey Alexandrovich Kryukov 9-May-11 12:48pm    
No! Also provide exception stack, all inner exceptions, recursively and mark line number in exception stack in your code; show relevant part of this code.
--SA

API required the thread to be single-apartment. To comply, you need to do the operation in question in a single-apartment thread. Create a thread for the operation and start it. Before starting, make sure it is in a single-apartment mode. Use Thread.TrySetApartmentState(ApartmentState.STA), in case of success, start your thread, if not — throw some exception in the calling thread — something wrong. Just in case, make sure you create and use the relevant instance(s) in the same thread.

—SA
 
Share this answer
 
Comments
hamzah1 9-May-11 13:25pm    
It's works for the first exception know it show me this exception:
Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on.
Sergey Alexandrovich Kryukov 9-May-11 21:22pm    
This is another cross-thread problem...
--SA
Sergey Alexandrovich Kryukov 9-May-11 21:29pm    
This problem is well-known. You cannot do the calls. What do you will find in my other solution. The technique is 100% standard and absolutely important to know. Nothing else can be done, just UI thread invocation. I have code samples showing how to do this.
--SA
"Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on."
This is a follow-up question raised when a fix I suggested was applied. This is another cross-threading problem which is easy to solve. Yes, UI cannot be controlled by any calls to any methods or properties a non-UI threads. The thread invocation mechanism should be used.

The delegate use to control UI should be invoked to be called on the UI thread. Use the method Invoke or BeginInvoke of System.Threading.Dispatcher (can work with both WPF and Forms) or System.Windows.Forms.Control (Forms only, does not matter what instance of control; it just has to participate in currently running System.Windows.Forms.Application).

For detailed explanation on how it works and usage samples, please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900