Results 1 to 5 of 5

Thread: cross thread problem

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    6

    cross thread problem

    hi .
    i have 2 application clinet/server the server broadcast media files to client i used tcp/ip protocol the server code is work good but i have a problem in client code i need help , ineed someone to correct the code .iwll show where the error is in the code
    note: i call the class in the main form dim a as recive in the starter of the main form class to be gloable .
    thanks
    Code:
    Imports System.Net.Sockets
    Imports System.Windows.Forms
    Imports System.IO
    Imports System.Threading
    Imports System.Windows.Forms.Control
    Imports System.Runtime.Remoting
    Imports System.Runtime.Remoting.Channels
    Imports System.Net
    
    
    
    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.TrySetApartmentState(ApartmentState.STA)
            readthred.SetApartmentState(ApartmentState.STA)
    
    
    
    
    
    
            ' If readthred.TrySetApartmentState(ApartmentState.STA) Then
            readthred.Start()
            '   End If
    
    
    
    
    
        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'Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on.
    
                    frmObject.Playlist.SelectedItem = frmObject.ListBox.SelectedItem  'Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on.
                    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
    Last edited by hamzahkh; May 13th, 2011 at 12:55 PM. Reason: improve question

  2. #2
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: cross thread problem

    http://social.msdn.microsoft.com/For...-2958d7415eb3/


    A background worker would probably be the best choice here.
    Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".


    VS 2008 .NetFW 2.0

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    6

    Re: cross thread problem

    can you till me the functionalty of background worker ? and in my problem what i should type inside it ??

  4. #4
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: cross thread problem

    Did you read the MSDN thread? There's a few links in there that explain everything you need to know about background workers.
    Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".


    VS 2008 .NetFW 2.0

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: cross thread problem

    You can follow the CodeBank link in my signature and check out my thread on Using The BackgroundWorker.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width