Accessing form components from a thread

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

    #1

    Accessing form components from a thread

    This was easy in C++ (well, relatively.) Now I'm in the C#
    environment and everything I try doesn't work.

    I'm running a thread in which I have two accesses I need to components
    on a form. In one instance it's to simply check the text of a button
    as an indicator of whether I've got a request to end the thread. This
    is a read-only situation so I don't have to worry about
    synchronization . The other is a need to write status information to
    either a text box or a label. The thread is the only thing that
    writes to this field so, again, no conflict.

    I've tried with a static method within the form itself and with a
    non-static method contained in a class object. I've tried accessors,
    with which I'm not fully comfortable yet and might not know how or
    where to declare it (I get an "already contains definition..." error.
    I've tried fully qualified paths to the text but get an error message
    that I need an object reference for the non-static field.

    Please advise.

    --
    Lilith
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Accessing form components from a thread

    Hi,

    I would like to know how you just to do it in C++ that worked.
    Since all the windows versions the UI can only be accessed from the main
    (the UI) thread only.

    In .NET if you want to modify the interface from another thread you have to
    use Control.Invoke. For this you need to have a reference to one of the
    controls (IT DOES NOT MATTER WHICH). but Invoke will assure that the method
    is executed in the UI thread.

    "Lilith" <lilith@dcccd.e duwrote in message
    news:b3jmb3t0bj tmh1u5lkgqpct0e 5r5ukedt3@4ax.c om...
    This was easy in C++ (well, relatively.) Now I'm in the C#
    environment and everything I try doesn't work.
    >
    I'm running a thread in which I have two accesses I need to components
    on a form. In one instance it's to simply check the text of a button
    as an indicator of whether I've got a request to end the thread. This
    is a read-only situation so I don't have to worry about
    synchronization . The other is a need to write status information to
    either a text box or a label. The thread is the only thing that
    writes to this field so, again, no conflict.
    >
    I've tried with a static method within the form itself and with a
    non-static method contained in a class object. I've tried accessors,
    with which I'm not fully comfortable yet and might not know how or
    where to declare it (I get an "already contains definition..." error.
    I've tried fully qualified paths to the text but get an error message
    that I need an object reference for the non-static field.
    >
    Please advise.
    >
    --
    Lilith

    Comment

    • Lilith

      #3
      Re: Accessing form components from a thread

      In C++ I was able to pass 'this' to the thread function through the
      AfxBeginThread call. This gave me access, through the pointer, to the
      control. I could also pass messages through, IIRC, the postmessage
      and sendmessage calls.

      I'll have a look at Invoke and see what I can divine.

      Thanks,
      Lil


      On Thu, 9 Aug 2007 13:58:08 -0400, "Ignacio Machin \( .NET/ C# MVP \)"
      <machin TA laceupsolutions .comwrote:
      >Hi,
      >
      >I would like to know how you just to do it in C++ that worked.
      >Since all the windows versions the UI can only be accessed from the main
      >(the UI) thread only.
      >
      >In .NET if you want to modify the interface from another thread you have to
      >use Control.Invoke. For this you need to have a reference to one of the
      >controls (IT DOES NOT MATTER WHICH). but Invoke will assure that the method
      >is executed in the UI thread.
      >
      >"Lilith" <lilith@dcccd.e duwrote in message
      >news:b3jmb3t0b jtmh1u5lkgqpct0 e5r5ukedt3@4ax. com...
      >This was easy in C++ (well, relatively.) Now I'm in the C#
      >environment and everything I try doesn't work.
      >>
      >I'm running a thread in which I have two accesses I need to components
      >on a form. In one instance it's to simply check the text of a button
      >as an indicator of whether I've got a request to end the thread. This
      >is a read-only situation so I don't have to worry about
      >synchronizatio n. The other is a need to write status information to
      >either a text box or a label. The thread is the only thing that
      >writes to this field so, again, no conflict.
      >>
      >I've tried with a static method within the form itself and with a
      >non-static method contained in a class object. I've tried accessors,
      >with which I'm not fully comfortable yet and might not know how or
      >where to declare it (I get an "already contains definition..." error.
      >I've tried fully qualified paths to the text but get an error message
      >that I need an object reference for the non-static field.
      >>
      >Please advise.
      >>
      >--
      >Lilith
      >

      Comment

      • Lilith

        #4
        Re: Accessing form components from a thread

        On Thu, 09 Aug 2007 12:39:48 -0500, Lilith <lilith@dcccd.e duwrote:
        >This was easy in C++ (well, relatively.) Now I'm in the C#
        >environment and everything I try doesn't work.
        >
        >I'm running a thread in which I have two accesses I need to components
        >on a form. In one instance it's to simply check the text of a button
        >as an indicator of whether I've got a request to end the thread. This
        >is a read-only situation so I don't have to worry about
        >synchronizatio n. The other is a need to write status information to
        >either a text box or a label. The thread is the only thing that
        >writes to this field so, again, no conflict.
        >
        >I've tried with a static method within the form itself and with a
        >non-static method contained in a class object. I've tried accessors,
        >with which I'm not fully comfortable yet and might not know how or
        >where to declare it (I get an "already contains definition..." error.
        >I've tried fully qualified paths to the text but get an error message
        >that I need an object reference for the non-static field.
        >
        >Please advise.
        Thanks to you both for the advice. I had to look it up and do a bit
        of copying, but I think I've got the jist of what's going on. If you
        can verify that I understand what happens I think I can move on.

        In essence a delegate is set up using the footprint of the method that
        needs to be called back. Another method is called that creates an
        instance of the callback method and pass it, along with suitable
        arguments, to the Invoke function, which then invokes the callback
        method (with arguments) and, since it becomes part of the UI thread
        there's no conflict.

        Basically correct? :-/

        Thanks again,
        Lil

        Comment

        • Ignacio Machin \( .NET/ C# MVP \)

          #5
          Re: Accessing form components from a thread

          Hi,

          "Lilith" <lilith@dcccd.e duwrote in message
          news:833nb39usq ip3pptgaj4kf3h4 66j9spmnb@4ax.c om...
          On Thu, 09 Aug 2007 12:39:48 -0500, Lilith <lilith@dcccd.e duwrote:
          >
          >>This was easy in C++ (well, relatively.) Now I'm in the C#
          >>environment and everything I try doesn't work.
          >>
          >>I'm running a thread in which I have two accesses I need to components
          >>on a form. In one instance it's to simply check the text of a button
          >>as an indicator of whether I've got a request to end the thread. This
          >>is a read-only situation so I don't have to worry about
          >>synchronizati on. The other is a need to write status information to
          >>either a text box or a label. The thread is the only thing that
          >>writes to this field so, again, no conflict.
          >>
          >>I've tried with a static method within the form itself and with a
          >>non-static method contained in a class object. I've tried accessors,
          >>with which I'm not fully comfortable yet and might not know how or
          >>where to declare it (I get an "already contains definition..." error.
          >>I've tried fully qualified paths to the text but get an error message
          >>that I need an object reference for the non-static field.
          >>
          >>Please advise.
          >
          Thanks to you both for the advice. I had to look it up and do a bit
          of copying, but I think I've got the jist of what's going on. If you
          can verify that I understand what happens I think I can move on.
          >
          In essence a delegate is set up using the footprint of the method that
          needs to be called back.
          A delegate is like a function pointer in C++, only that it has a defined
          signature. In this way the compiler can check for the correct signature.
          But note a fine difference, a delegate is a type declaration, like a class
          or a struct.
          >Another method is called that creates an
          instance of the callback method and pass it, along with suitable
          arguments, to the Invoke function,
          No exactly, An instance of that type is created. The type is the delegate.
          You can say that the constructor require the name of the method it will
          call.



          Comment

          Working...