Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code like this :

VB.NET
Private Sub tmrProc_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrProc.Tick
       second = second + 1

       If second = 2 Then
           CekInternet()
       End If

       If second = 4 Then
           If rCek = True Then
               'cek ke server
               lblPesan.Text = "Menghubungkan ke server...."

               Dim Thread1 As New System.Threading.Thread( _
     AddressOf LoginServ)
               Thread1.Start()
           Else

           End If
       End If


   End Sub



C#
Private Sub LoginServ()

       iLogin = Server.Login(Server.ServerAddress)
       Select Case iLogin
           Case 0
               lblPesan.Text = "Sukses terkoneksi dengan server...."
               lblPesan.ForeColor = Drawing.Color.Lime
           Case 1
               lblPesan.Text = "Mengaktifkan offline mode"
               lblPesan.ForeColor = Drawing.Color.White
           Case 2
               lblPesan.Text = "Mengaktifkan offline mode"
               lblPesan.ForeColor = Drawing.Color.White
       End Select

   End Sub


when ILogin in case and update text label, error cross thread

hope you help

What I have tried:

Dim Thread1 As New System.Threading.Thread( _
AddressOf LoginServ)
Thread1.Start()
Posted
Updated 11-Aug-16 20:44pm

1 solution

What kind of Timer did you use? It is also unclear which call to a property of lblPesan fails. I assume that it happens in the LoginServ function: you explicitely use a different thread. But you must not access a control in a different thread than the thread in which it was created. If I assume correctly that lblPesan.Text = "Menghubungkan ke server...." in the timer event does not fail, I suggest to simply remove the creation of the thread and call LoginServ() directly.
If also the first call fails, you have to use lblPesan.Invoke(...) in all cases.
 
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