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. Illegal Cross thread Error

Illegal Cross thread Error

Scheduled Pinned Locked Moved C#
help
7 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.
  • R Offline
    R Offline
    ramdil
    wrote on last edited by
    #1

    Hi All Initially i had two applications which access a dll for its method for sending and recieving data.So i got a error saying illegal thread cross operation .So in order to avoid it , i created a delegate and then add a if condition if (this.InvokeRequired) { MyDelegateMethod theDelegateMethod = new MyDelegateMethod (this.DisplayMessage); this.Invoke(theDelegateMethod, new object[] { message }); } else { ReceiveMessage(message); } by doing this i could eliminate the error.Now when i changed one application which was in windows to a webserivce , i am not getting this invoke method ..so what will i do .. Can any one help me in this regard to overcome this situation And second one which i heard to overcome above situation was using System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; In window appn i am getting CheckForIllegalCrossThreadCalls properties but in webservice i am not getting it.. pls help me if any one has idea in it

    Regards DilipRam

    L 1 Reply Last reply
    0
    • R ramdil

      Hi All Initially i had two applications which access a dll for its method for sending and recieving data.So i got a error saying illegal thread cross operation .So in order to avoid it , i created a delegate and then add a if condition if (this.InvokeRequired) { MyDelegateMethod theDelegateMethod = new MyDelegateMethod (this.DisplayMessage); this.Invoke(theDelegateMethod, new object[] { message }); } else { ReceiveMessage(message); } by doing this i could eliminate the error.Now when i changed one application which was in windows to a webserivce , i am not getting this invoke method ..so what will i do .. Can any one help me in this regard to overcome this situation And second one which i heard to overcome above situation was using System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; In window appn i am getting CheckForIllegalCrossThreadCalls properties but in webservice i am not getting it.. pls help me if any one has idea in it

      Regards DilipRam

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

      Hi, the cross-thread problem is inherent in how controls work; they are not thread- safe, so the right approach is for each control to have only one thread ever working on it, normally that's the main thread aka GUI thread. there was no checking before .NET 2.0 and an app would work, partially work, suddenly hang, whatever if you did it wrong. .NET 2.0 checks and throws InvalidOperationException (as you know), unless you set CheckForIllegalCrossThreadCalls false, in which case you return to the pre 2.0 situation. I cant recommend that. The normal solution is similar to what you have, with one difference: the delegate and the else part of the if normally call the same method. In your code it is DisplayMessage and ReceiveMessage !? BTW: I am not sure what "this" is refering to in your web service, does it has controls while not having a GUI ?? :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      R 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, the cross-thread problem is inherent in how controls work; they are not thread- safe, so the right approach is for each control to have only one thread ever working on it, normally that's the main thread aka GUI thread. there was no checking before .NET 2.0 and an app would work, partially work, suddenly hang, whatever if you did it wrong. .NET 2.0 checks and throws InvalidOperationException (as you know), unless you set CheckForIllegalCrossThreadCalls false, in which case you return to the pre 2.0 situation. I cant recommend that. The normal solution is similar to what you have, with one difference: the delegate and the else part of the if normally call the same method. In your code it is DisplayMessage and ReceiveMessage !? BTW: I am not sure what "this" is refering to in your web service, does it has controls while not having a GUI ?? :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        R Offline
        R Offline
        ramdil
        wrote on last edited by
        #3

        Thanks for the reply regarding the DisplayMessage and ReceiveMessage first one is event handler delegate for recieving messages and second one is local method for processing the receiving message..just for readability..then regarding this i was just pasting the code in my windows applications..So what is ur suggestion..What i understand from your reply is that 2003 will not throw the illegal operation error and so i dont want to check it..Am i right..If i am wrong then what is best solution.

        Regards DilipRam

        M L 2 Replies Last reply
        0
        • R ramdil

          Thanks for the reply regarding the DisplayMessage and ReceiveMessage first one is event handler delegate for recieving messages and second one is local method for processing the receiving message..just for readability..then regarding this i was just pasting the code in my windows applications..So what is ur suggestion..What i understand from your reply is that 2003 will not throw the illegal operation error and so i dont want to check it..Am i right..If i am wrong then what is best solution.

          Regards DilipRam

          M Offline
          M Offline
          martin_hughes
          wrote on last edited by
          #4

          Here's how you do it in .Net 2.0: http://www.codeproject.com/csharp/winformthreading.asp[^]

          R 1 Reply Last reply
          0
          • M martin_hughes

            Here's how you do it in .Net 2.0: http://www.codeproject.com/csharp/winformthreading.asp[^]

            R Offline
            R Offline
            ramdil
            wrote on last edited by
            #5

            thanks for the reply i have done it in 2.0 but i want it in 1.1 Anyway thanks for replying me

            Regards DilipRam

            1 Reply Last reply
            0
            • R ramdil

              Thanks for the reply regarding the DisplayMessage and ReceiveMessage first one is event handler delegate for recieving messages and second one is local method for processing the receiving message..just for readability..then regarding this i was just pasting the code in my windows applications..So what is ur suggestion..What i understand from your reply is that 2003 will not throw the illegal operation error and so i dont want to check it..Am i right..If i am wrong then what is best solution.

              Regards DilipRam

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

              Hi, if you get cross-thread exceptions, something is wrong in your app. Going back to 1.x or setting CheckCrossThread=false are two ways to hide the symptoms, they dont cure the disease. The cross-thread exceptions 2.0 and later are given are extra symptoms to make you aware there is a disease which you should fix. the right way is with Control.InvokeRequired and Control.Invoke; yuou normally have two choices: - make the delegate and the else part point to the same method; - make the delegate point to the method it is in (this is kind of a recursive call, but the second time around InvokeRequired will be false hence the else part will execute). :)

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              R 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, if you get cross-thread exceptions, something is wrong in your app. Going back to 1.x or setting CheckCrossThread=false are two ways to hide the symptoms, they dont cure the disease. The cross-thread exceptions 2.0 and later are given are extra symptoms to make you aware there is a disease which you should fix. the right way is with Control.InvokeRequired and Control.Invoke; yuou normally have two choices: - make the delegate and the else part point to the same method; - make the delegate point to the method it is in (this is kind of a recursive call, but the second time around InvokeRequired will be false hence the else part will execute). :)

                Luc Pattyn


                try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                R Offline
                R Offline
                ramdil
                wrote on last edited by
                #7

                Hi thanks for the reply,but the problem i am facing is that i was initially working with windows application ,now i want same method to implemented in webservice .but in webservice i am not getting invoke properties..Hope u understand my problem.Since i am not getting the invoke properties, i am not able to check that condition.. Any way to play with it.

                Regards DilipRam

                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