Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. how to acess Control Reference in Thread Method

how to acess Control Reference in Thread Method

Scheduled Pinned Locked Moved .NET (Core and Framework)
tutorialhelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shaina2231
    wrote on last edited by
    #1

    I hv to get control reference in thread method let suppose in this example i m setting text lable of text control but it gives exception at Statment TextBox1.Text="DFD" Exception is Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on. code is below

    Public Class data
    Private myUtil As Util = New Util()
    Private strSQL As String = ""
    Public Sub savedata()
    'Label1.Text = "DFDF"
    ' Dim d As data
    TextBox1.Text = "DFDF"
    Try
    strSQL = "Insert into tbldata(pid,pname,pdesc) values(2, 'abc', 'description1')"
    If (myUtil.ExecuteDBCommand(strSQL) = 1) Then
    'MsgBox("Account(s) Assigned Sucessfully!")
    Else
    MessageBox.Show("Could not Save Record!", "Account Information!", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Exit Sub
    End If

        Catch ex As Exception
    
        End Try
    
    End Sub
    
    Private Sub data\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim t1 As Threading.Thread = New Threading.Thread(AddressOf savedata)
        t1.Start()
       
    
    End Sub
    

    End Class

    plz help

    J 1 Reply Last reply
    0
    • S shaina2231

      I hv to get control reference in thread method let suppose in this example i m setting text lable of text control but it gives exception at Statment TextBox1.Text="DFD" Exception is Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on. code is below

      Public Class data
      Private myUtil As Util = New Util()
      Private strSQL As String = ""
      Public Sub savedata()
      'Label1.Text = "DFDF"
      ' Dim d As data
      TextBox1.Text = "DFDF"
      Try
      strSQL = "Insert into tbldata(pid,pname,pdesc) values(2, 'abc', 'description1')"
      If (myUtil.ExecuteDBCommand(strSQL) = 1) Then
      'MsgBox("Account(s) Assigned Sucessfully!")
      Else
      MessageBox.Show("Could not Save Record!", "Account Information!", MessageBoxButtons.OK, MessageBoxIcon.Information)
      Exit Sub
      End If

          Catch ex As Exception
      
          End Try
      
      End Sub
      
      Private Sub data\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          Dim t1 As Threading.Thread = New Threading.Thread(AddressOf savedata)
          t1.Start()
         
      
      End Sub
      

      End Class

      plz help

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      Short version: any time you access a control you need to do it on the thread that it was created on. The Control class has InvokeRequired, BeginInvoke and Invoke properties to allow you to accomplish this. Long Version: http://www.perceler.com/articles1.php?art=crossthreads1[^] Longer Version: What's up with BeginInvoke?[^]

      :badger:

      L 1 Reply Last reply
      0
      • J Jimmanuel

        Short version: any time you access a control you need to do it on the thread that it was created on. The Control class has InvokeRequired, BeginInvoke and Invoke properties to allow you to accomplish this. Long Version: http://www.perceler.com/articles1.php?art=crossthreads1[^] Longer Version: What's up with BeginInvoke?[^]

        :badger:

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        great reference! :-D

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        [The QA section does it automatically now, I hope we soon get it on regular forums as well]


        J 1 Reply Last reply
        0
        • L Luc Pattyn

          great reference! :-D

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          [The QA section does it automatically now, I hope we soon get it on regular forums as well]


          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          That's exactly why I used it :)

          :badger:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups