Threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jakub Cermak

    #1

    Threads

    Hi all,
    sorry for my beginner question.
    How can I change a form property from a thread (I don't think primary
    application thread, but the instance of Thread class)? If I do it i get
    an exception InvalidOperatio nException - Cross-thread
    operation not valid: Control 'lblStatusGame' accessed from a thread
    other than the thread it was created on.
    --
    Best regards,

    Jakub Cermak
    ICQ 159971304

  • Jon Skeet [C# MVP]

    #2
    Re: Threads

    Jakub Cermak <ja.cermi@centr um.cz> wrote:[color=blue]
    > sorry for my beginner question.
    > How can I change a form property from a thread (I don't think primary
    > application thread, but the instance of Thread class)? If I do it i get
    > an exception InvalidOperatio nException - Cross-thread
    > operation not valid: Control 'lblStatusGame' accessed from a thread
    > other than the thread it was created on.[/color]

    You can't - you need to use Control.Invoke or Control.BeginIn voke.

    See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Threads

      Jakub Cermak <ja.cermi@centr um.cz> wrote:[color=blue]
      > sorry for my beginner question.
      > How can I change a form property from a thread (I don't think primary
      > application thread, but the instance of Thread class)? If I do it i get
      > an exception InvalidOperatio nException - Cross-thread
      > operation not valid: Control 'lblStatusGame' accessed from a thread
      > other than the thread it was created on.[/color]

      You can't - you need to use Control.Invoke or Control.BeginIn voke.

      See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Dmytro Lapshyn [MVP]

        #4
        Re: Threads

        Hi Jakub,

        All operations on forms and controls must be done on the main (UI) thread.
        If you need to do them from other threads, use the Control.InvokeR equired
        and Control.Invoke (see MSDN docs on these for detailed information).

        --
        Sincerely,
        Dmytro Lapshyn [Visual Developer - Visual C# MVP]


        "Jakub Cermak" <ja.cermi@centr um.cz> wrote in message
        news:u3cILbJXFH A.3280@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Hi all,
        > sorry for my beginner question.
        > How can I change a form property from a thread (I don't think primary
        > application thread, but the instance of Thread class)? If I do it i get an
        > exception InvalidOperatio nException - Cross-thread
        > operation not valid: Control 'lblStatusGame' accessed from a thread
        > other than the thread it was created on.
        > --
        > Best regards,
        >
        > Jakub Cermak
        > ICQ 159971304
        > http://cermi.wz.cz[/color]

        Comment

        • Dmytro Lapshyn [MVP]

          #5
          Re: Threads

          Hi Jakub,

          All operations on forms and controls must be done on the main (UI) thread.
          If you need to do them from other threads, use the Control.InvokeR equired
          and Control.Invoke (see MSDN docs on these for detailed information).

          --
          Sincerely,
          Dmytro Lapshyn [Visual Developer - Visual C# MVP]


          "Jakub Cermak" <ja.cermi@centr um.cz> wrote in message
          news:u3cILbJXFH A.3280@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Hi all,
          > sorry for my beginner question.
          > How can I change a form property from a thread (I don't think primary
          > application thread, but the instance of Thread class)? If I do it i get an
          > exception InvalidOperatio nException - Cross-thread
          > operation not valid: Control 'lblStatusGame' accessed from a thread
          > other than the thread it was created on.
          > --
          > Best regards,
          >
          > Jakub Cermak
          > ICQ 159971304
          > http://cermi.wz.cz[/color]

          Comment

          Working...