Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 9-May-11 6:53am
hamzah1532
Edited 9-May-11 7:16am
v2
Comments
Marc A. Brown at 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 at 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
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
hamzah1 at 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.
SAKryukov at 9-May-11 21:22pm
   
This is another cross-thread problem...
--SA
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

"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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,769
1 Sergey Alexandrovich Kryukov 6,338
2 DamithSL 5,408
3 Manas Bhardwaj 4,816
4 Maciej Los 4,195


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 9 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100