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. Cross-thread operation not valid

Cross-thread operation not valid

Scheduled Pinned Locked Moved Visual Basic
helpcsharptutorialquestion
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.
  • R Offline
    R Offline
    RJGCarey
    wrote on last edited by
    #1

    I have migrated a VB.Net 2003 forms project to 2005 Express Edition. This went surprisingly well. One problem remains. The main form sets up a delegate system to update it's text property from a background thread. Public Class FormMain Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'other stuff frmTxt = New FormText(AddressOf WriteFormText) End Sub #Region "Delegates" Public frmTxt As FormText Public Delegate Sub FormText(ByVal inText As String) Private Sub WriteFormText(ByVal inText As String) Try Me.Text = inText Catch ex As Exception Shw.Err("frmMain.WriteFormText " & ex.Message) End Try End Sub #End Region End Class A class "Clock" runs on a background thread and updates the main form's text once a second to show the time. Public Class clsClock Private Sub MainLoop() '8/9/05 Do 'determine if it is a new second using Now.Second and set isNewSecond accordingly if isNewSecond = True Then frmMain.frmTxt.BeginInvoke("TheCore" & Space(100) & Now.ToString, Nothing, Nothing) End If 'do all kinds of things depending on the time Thread.Sleep(100) Loop End Sub End Class Running in .Net 1.1, this works fine, in .Net 2.0 it throws this error "Cross-thread operation not valid: Control 'FormMain' accessed from a thread other than it was created on." I have a system of delegates to update a progress bar from background threads and a system of delegates to write to a picture box from background threads. Both of these work correctly in 2005. Help and my books have not solved the problem. Several hours of Google has only found other folks with the same problem. Does anyone know how to fix it? Thanks RCarey

    RCarey

    G 1 Reply Last reply
    0
    • R RJGCarey

      I have migrated a VB.Net 2003 forms project to 2005 Express Edition. This went surprisingly well. One problem remains. The main form sets up a delegate system to update it's text property from a background thread. Public Class FormMain Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'other stuff frmTxt = New FormText(AddressOf WriteFormText) End Sub #Region "Delegates" Public frmTxt As FormText Public Delegate Sub FormText(ByVal inText As String) Private Sub WriteFormText(ByVal inText As String) Try Me.Text = inText Catch ex As Exception Shw.Err("frmMain.WriteFormText " & ex.Message) End Try End Sub #End Region End Class A class "Clock" runs on a background thread and updates the main form's text once a second to show the time. Public Class clsClock Private Sub MainLoop() '8/9/05 Do 'determine if it is a new second using Now.Second and set isNewSecond accordingly if isNewSecond = True Then frmMain.frmTxt.BeginInvoke("TheCore" & Space(100) & Now.ToString, Nothing, Nothing) End If 'do all kinds of things depending on the time Thread.Sleep(100) Loop End Sub End Class Running in .Net 1.1, this works fine, in .Net 2.0 it throws this error "Cross-thread operation not valid: Control 'FormMain' accessed from a thread other than it was created on." I have a system of delegates to update a progress bar from background threads and a system of delegates to write to a picture box from background threads. Both of these work correctly in 2005. Help and my books have not solved the problem. Several hours of Google has only found other folks with the same problem. Does anyone know how to fix it? Thanks RCarey

      RCarey

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      I think the following might help: http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx[^] http://www.mikedub.net/mikeDubSamples/SafeReallySimpleMultithreadingInWindowsForms20/SafeReallySimpleMultithreadingInWindowsForms20.htm[^] Geo

      "We make a living by what we get, we make a life by what we give." --Winston Churchill

      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