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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Control Access within a Thread

Control Access within a Thread

Scheduled Pinned Locked Moved Visual Basic
questiontutorial
2 Posts 2 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.
  • V Offline
    V Offline
    VFaul
    wrote on last edited by
    #1

    I have a label control and I want to modify its text from within a thread (example shown below), but I get a runtime access exception. How do I access controls from a thread? Imports System.Threading Imports System.Threading.Thread Public Class Form1 Public thrd As New Thread(AddressOf MyThread) Public counter As Integer = 0 Public Sub MyThread() Do counter = counter + 1 lblThreadCount.Text = "Thread Count = " & counter Sleep(100) Loop While 1 End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblThreadCount.Text = "Thread Count = " & counter thrd.Start() End Sub Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed thrd.Abort() End Sub End Class

    Z 1 Reply Last reply
    0
    • V VFaul

      I have a label control and I want to modify its text from within a thread (example shown below), but I get a runtime access exception. How do I access controls from a thread? Imports System.Threading Imports System.Threading.Thread Public Class Form1 Public thrd As New Thread(AddressOf MyThread) Public counter As Integer = 0 Public Sub MyThread() Do counter = counter + 1 lblThreadCount.Text = "Thread Count = " & counter Sleep(100) Loop While 1 End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblThreadCount.Text = "Thread Count = " & counter thrd.Start() End Sub Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed thrd.Abort() End Sub End Class

      Z Offline
      Z Offline
      zacharyshroyer
      wrote on last edited by
      #2

      Add the following line to the top of the Form1_Load event: Control.CheckForIllegalCrossThreadCalls = False

      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