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. Visual Basic
  4. How output msg from thread to label windows control ?

How output msg from thread to label windows control ?

Scheduled Pinned Locked Moved Visual Basic
question
5 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.
  • A Offline
    A Offline
    ALQallaf
    wrote on last edited by
    #1

    Is there anything i have to Consider it befor,output msg from thread to label windows control ? because i made code but it give me this exception->(Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on.) i want to know why exactly this?,also what solution for it?

    J C 2 Replies Last reply
    0
    • A ALQallaf

      Is there anything i have to Consider it befor,output msg from thread to label windows control ? because i made code but it give me this exception->(Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on.) i want to know why exactly this?,also what solution for it?

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

      basically, Only the UI thread may update the UI, so you need to check whether you are on that thread, and if not delegate to it. InvokeRequired[^] Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

      A 1 Reply Last reply
      0
      • J J4amieC

        basically, Only the UI thread may update the UI, so you need to check whether you are on that thread, and if not delegate to it. InvokeRequired[^] Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

        A Offline
        A Offline
        ALQallaf
        wrote on last edited by
        #3

        is there any simple example doing this?

        1 Reply Last reply
        0
        • A ALQallaf

          Is there anything i have to Consider it befor,output msg from thread to label windows control ? because i made code but it give me this exception->(Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on.) i want to know why exactly this?,also what solution for it?

          C Offline
          C Offline
          codeadair
          wrote on last edited by
          #4

          ' This is an example from msdn of microsoft Imports System Imports System.ComponentModel Imports System.Threading Imports System.Windows.Forms Public Class Form1 Inherits Form ' This delegate enables asynchronous calls for setting ' the text property on a TextBox control. Delegate Sub SetTextCallback([text] As String) ' This thread is used to demonstrate both thread-safe and ' unsafe ways to call a Windows Forms control. Private demoThread As Thread = Nothing ' This BackgroundWorker is used to demonstrate the ' preferred way of performing asynchronous operations. Private WithEvents backgroundWorker1 As BackgroundWorker Private textBox1 As TextBox Private WithEvents setTextUnsafeBtn As Button Private WithEvents setTextSafeBtn As Button Private WithEvents setTextBackgroundWorkerBtn As Button Private components As System.ComponentModel.IContainer = Nothing Public Sub New() InitializeComponent() End Sub Protected Overrides Sub Dispose(disposing As Boolean) If disposing AndAlso Not (components Is Nothing) Then components.Dispose() End If MyBase.Dispose(disposing) End Sub ' This event handler creates a thread that calls a ' Windows Forms control in an unsafe way. Private Sub setTextUnsafeBtn_Click( _ ByVal sender As Object, _ ByVal e As EventArgs) Handles setTextUnsafeBtn.Click Me.demoThread = New Thread( _ New ThreadStart(AddressOf Me.ThreadProcUnsafe)) Me.demoThread.Start() End Sub ' This method is executed on the worker thread and makes ' an unsafe call on the TextBox control. Private Sub ThreadProcUnsafe() Me.textBox1.Text = "This text was set unsafely." End Sub ' This event handler creates a thread that calls a ' Windows Forms control in a thread-safe way. Private Sub setTextSafeBtn_Click( _ ByVal sender As Object, _ ByVal e As EventArgs) Handles setTextSafeBtn.Click Me.demoThread = New Thread( _ New ThreadStart(AddressOf Me.ThreadProcSafe)) Me.demoThread.Start() End Sub ' This method is executed on the worker thread and makes ' a thread-safe call on the TextBox control. Private Sub ThreadProcSafe() Me.SetText("This text was set safely.") End Sub ' This method demonstrates a pattern for making thread-safe ' calls on a Windows Forms control. '

          A 1 Reply Last reply
          0
          • C codeadair

            ' This is an example from msdn of microsoft Imports System Imports System.ComponentModel Imports System.Threading Imports System.Windows.Forms Public Class Form1 Inherits Form ' This delegate enables asynchronous calls for setting ' the text property on a TextBox control. Delegate Sub SetTextCallback([text] As String) ' This thread is used to demonstrate both thread-safe and ' unsafe ways to call a Windows Forms control. Private demoThread As Thread = Nothing ' This BackgroundWorker is used to demonstrate the ' preferred way of performing asynchronous operations. Private WithEvents backgroundWorker1 As BackgroundWorker Private textBox1 As TextBox Private WithEvents setTextUnsafeBtn As Button Private WithEvents setTextSafeBtn As Button Private WithEvents setTextBackgroundWorkerBtn As Button Private components As System.ComponentModel.IContainer = Nothing Public Sub New() InitializeComponent() End Sub Protected Overrides Sub Dispose(disposing As Boolean) If disposing AndAlso Not (components Is Nothing) Then components.Dispose() End If MyBase.Dispose(disposing) End Sub ' This event handler creates a thread that calls a ' Windows Forms control in an unsafe way. Private Sub setTextUnsafeBtn_Click( _ ByVal sender As Object, _ ByVal e As EventArgs) Handles setTextUnsafeBtn.Click Me.demoThread = New Thread( _ New ThreadStart(AddressOf Me.ThreadProcUnsafe)) Me.demoThread.Start() End Sub ' This method is executed on the worker thread and makes ' an unsafe call on the TextBox control. Private Sub ThreadProcUnsafe() Me.textBox1.Text = "This text was set unsafely." End Sub ' This event handler creates a thread that calls a ' Windows Forms control in a thread-safe way. Private Sub setTextSafeBtn_Click( _ ByVal sender As Object, _ ByVal e As EventArgs) Handles setTextSafeBtn.Click Me.demoThread = New Thread( _ New ThreadStart(AddressOf Me.ThreadProcSafe)) Me.demoThread.Start() End Sub ' This method is executed on the worker thread and makes ' a thread-safe call on the TextBox control. Private Sub ThreadProcSafe() Me.SetText("This text was set safely.") End Sub ' This method demonstrates a pattern for making thread-safe ' calls on a Windows Forms control. '

            A Offline
            A Offline
            ALQallaf
            wrote on last edited by
            #5

            is this simple example :omg: ? if yes , what about complex one hehehe :laugh: i searched befor , i found MSDN example but it is so long, what i want is real simple example because i dont have time for my project.

            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