using lock

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

    using lock

    Hi
    I'am writing a multithread application. This application has let say label1.
    This label of course has it's own thread.
    Then the question is:
    Can i read label1.Text without invoking it just by using such a syntax in
    other threads?:
    lock(label1)
    {
    .....
    }

    Does it prevent from cross-read/write operations?
    And one more short question is if i have to use this.invoke when i want to
    write to this label1 ( if in previous example it wasn't necessary )?

    Thanks or any suggestions
    PK


  • Brian Gideon

    #2
    Re: using lock

    PK,

    You must use Invoke or BeginInvoke to initiate any access on a Form or
    Control from a thread other than the main UI thread. The only
    exceptions to that rule are those members that are safe for
    multithreaded operations and there aren't many. Not only is the Text
    property unsafe for multithreaded operations, but it has
    thread-affinity requirements as well which pretty much eliminates the
    possibility of using any of the synchronization primitives to guard its
    access.

    Brian

    PiotrKolodziej wrote:
    Hi
    I'am writing a multithread application. This application has let say label1.
    This label of course has it's own thread.
    Then the question is:
    Can i read label1.Text without invoking it just by using such a syntax in
    other threads?:
    lock(label1)
    {
    ....
    }
    >
    Does it prevent from cross-read/write operations?
    And one more short question is if i have to use this.invoke when i want to
    write to this label1 ( if in previous example it wasn't necessary )?
    >
    Thanks or any suggestions
    PK

    Comment

    • PiotrKolodziej

      #3
      Re: using lock

      You must use Invoke or BeginInvoke to initiate any access on a Form or
      Control from a thread other than the main UI thread. The only
      exceptions to that rule are those members that are safe for
      multithreaded operations and there aren't many. Not only is the Text
      property unsafe for multithreaded operations, but it has
      thread-affinity requirements as well which pretty much eliminates the
      possibility of using any of the synchronization primitives to guard its
      access.
      >
      Brian
      >
      What if the other thread is the MainThread event? Do i really have to invoke
      the reads on label1?


      Comment

      • Brian Gideon

        #4
        Re: using lock


        PiotrKolodziej wrote:
        You must use Invoke or BeginInvoke to initiate any access on a Form or
        Control from a thread other than the main UI thread. The only
        exceptions to that rule are those members that are safe for
        multithreaded operations and there aren't many. Not only is the Text
        property unsafe for multithreaded operations, but it has
        thread-affinity requirements as well which pretty much eliminates the
        possibility of using any of the synchronization primitives to guard its
        access.

        Brian
        >
        What if the other thread is the MainThread event? Do i really have to invoke
        the reads on label1?
        I'm not sure what you mean by the MainThread event. But, yes you have
        to invoke reads when InvokeRequired is true. InvokeRequired will be
        false when execute on the main UI thread. Otherwise it will be true.

        Brian

        Comment

        Working...