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. C#
  4. How to solve"Cross thread operation not valid" error when programming with C#.net

How to solve"Cross thread operation not valid" error when programming with C#.net

Scheduled Pinned Locked Moved C#
csharphelpsysadmintutorialquestion
4 Posts 4 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
    sweenySL
    wrote on last edited by
    #1

    Hi im devloping a client server program using C#. What it basically does is the Server can connect 3,4 clients and they can send messags. To do this i didnt use threads. My problem is when client send a message to server it is not receved to the server. Then it indicate a error "Cross thread operation not valid." This is the solution i found form this site. --------------------------------------------------------- Private Strt As System.Threading.Thread ; Strt = New System.Threading.Thread(AddressOf MyThread1) Strt.Start(); Sub MyThread1 ' Working code ' Working code ' Working code ' Working code ' Working code ' Working code AccessControl() End Sub Private Sub AccessControl() If Me.InvokeRequired Then Me.Invoke(New MethodInvoker(AddressOf AccessControl)) Else ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub Button2.Visible = True Button3.Visible = True Opacity = 1 ShowInTaskbar = True End If End Sub --------------------------------------------------------- but the problem is hence i do not use threads do i need to create threads to solve this problem? Im developing the program using C#, so do i need to implement it in a same way of this code? Thank you so much.. sweenySL

    O S L 3 Replies Last reply
    0
    • S sweenySL

      Hi im devloping a client server program using C#. What it basically does is the Server can connect 3,4 clients and they can send messags. To do this i didnt use threads. My problem is when client send a message to server it is not receved to the server. Then it indicate a error "Cross thread operation not valid." This is the solution i found form this site. --------------------------------------------------------- Private Strt As System.Threading.Thread ; Strt = New System.Threading.Thread(AddressOf MyThread1) Strt.Start(); Sub MyThread1 ' Working code ' Working code ' Working code ' Working code ' Working code ' Working code AccessControl() End Sub Private Sub AccessControl() If Me.InvokeRequired Then Me.Invoke(New MethodInvoker(AddressOf AccessControl)) Else ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub Button2.Visible = True Button3.Visible = True Opacity = 1 ShowInTaskbar = True End If End Sub --------------------------------------------------------- but the problem is hence i do not use threads do i need to create threads to solve this problem? Im developing the program using C#, so do i need to implement it in a same way of this code? Thank you so much.. sweenySL

      O Offline
      O Offline
      Obaid ur Rehman
      wrote on last edited by
      #2

      If you are not using Threads then are you using Asyncronous sockets? It would help a lot if you post your code.

      1 Reply Last reply
      0
      • S sweenySL

        Hi im devloping a client server program using C#. What it basically does is the Server can connect 3,4 clients and they can send messags. To do this i didnt use threads. My problem is when client send a message to server it is not receved to the server. Then it indicate a error "Cross thread operation not valid." This is the solution i found form this site. --------------------------------------------------------- Private Strt As System.Threading.Thread ; Strt = New System.Threading.Thread(AddressOf MyThread1) Strt.Start(); Sub MyThread1 ' Working code ' Working code ' Working code ' Working code ' Working code ' Working code AccessControl() End Sub Private Sub AccessControl() If Me.InvokeRequired Then Me.Invoke(New MethodInvoker(AddressOf AccessControl)) Else ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub Button2.Visible = True Button3.Visible = True Opacity = 1 ShowInTaskbar = True End If End Sub --------------------------------------------------------- but the problem is hence i do not use threads do i need to create threads to solve this problem? Im developing the program using C#, so do i need to implement it in a same way of this code? Thank you so much.. sweenySL

        S Offline
        S Offline
        shrinerainxp
        wrote on last edited by
        #3

        Try form1.CheckForIllegalCrossThreadCalls=false;:-D

        1 Reply Last reply
        0
        • S sweenySL

          Hi im devloping a client server program using C#. What it basically does is the Server can connect 3,4 clients and they can send messags. To do this i didnt use threads. My problem is when client send a message to server it is not receved to the server. Then it indicate a error "Cross thread operation not valid." This is the solution i found form this site. --------------------------------------------------------- Private Strt As System.Threading.Thread ; Strt = New System.Threading.Thread(AddressOf MyThread1) Strt.Start(); Sub MyThread1 ' Working code ' Working code ' Working code ' Working code ' Working code ' Working code AccessControl() End Sub Private Sub AccessControl() If Me.InvokeRequired Then Me.Invoke(New MethodInvoker(AddressOf AccessControl)) Else ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub Button2.Visible = True Button3.Visible = True Opacity = 1 ShowInTaskbar = True End If End Sub --------------------------------------------------------- but the problem is hence i do not use threads do i need to create threads to solve this problem? Im developing the program using C#, so do i need to implement it in a same way of this code? Thank you so much.. sweenySL

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

          Hi, you create threads when executing asynchronous stuff, e.g. with the timers (except for Windows.Forms.Timer). setting CheckForIllegalCrossThreadCalls false only disables the checking, it does not avoid the problem that may result (UI freeze) immediately or after a while... If InvokeRequired is true, you really need the Invoke (as you did), and then the code you invoke should execute; check it with debugger or with logging ! PS: is this a C# question ? :)

          Luc Pattyn [My Articles]

          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