How can I lock thread till execution is finished

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zacksoniar
    New Member
    • Sep 2007
    • 45

    How can I lock thread till execution is finished

    Hello All,

    I have a event called dataTree_AfterS elect in which I have some code which I want to execute as atomic operation like this:

    Code:
    protected void dataTree_AfterSelect(event params)
    {
      code that should not be disturbed
    }
    What is happening now is, I am able to select another node of tree & in that case another execution of above function starts causing a garbled result.

    What I want is, user should be able to click on tree node but execution of code block should only start after first execution is totally over.

    How can I do this using thread? or non-thread solution is also appreciated.

    Please note that my function is UI event.

    Thanks in advance.

    zacksoniar
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    Threading seems like overkill to me, why don't you just disable the control at the beginning of the method, then re-enable it once the code has complete? Seems more elegant, and would let the user know that the control was doing something.

    Aimee

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Hmm, if your goal is to block until completion, threading might not be what you want.

      However, i *think* on the thread that CALLs the thread that does work you can call a .Join() to halt the current thread until the child thread completes.
      Just make sure the child thread DOES complete.

      If you thread does not complete (i.e. it sits there checking if there is work and then performs it) You also might be able to use those wait signallers thing(Some sort of Semaphore implimentation)

      OR OR OR possibly using a ThreadSafe locking object.
      [CODE=c#]

      lock(mylockingO bject)
      {
      //code that shouldnt be interrupted by other events(re-entry)
      }
      [/CODE]

      Comment

      • zacksoniar
        New Member
        • Sep 2007
        • 45

        #4
        Hmmmmm .. tyring same kind of solution.. "Join()" concept looks more appealing in my case... will try that out & let u know...


        Thanks for ur help



        Originally posted by Plater
        Hmm, if your goal is to block until completion, threading might not be what you want.

        However, i *think* on the thread that CALLs the thread that does work you can call a .Join() to halt the current thread until the child thread completes.
        Just make sure the child thread DOES complete.

        If you thread does not complete (i.e. it sits there checking if there is work and then performs it) You also might be able to use those wait signallers thing(Some sort of Semaphore implimentation)

        OR OR OR possibly using a ThreadSafe locking object.
        [CODE=c#]

        lock(mylockingO bject)
        {
        //code that shouldnt be interrupted by other events(re-entry)
        }
        [/CODE]

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          In C# lock is used to ensure that a block of code runs to completion without interruption by other thread.

          VB.NET it's SyncLock.

          These statements typically surround any shared resources that can only be accessed by one thread at a time. But there's no reason why you can't put it around a large part of your thread's code.

          For more information Thread Synchronization.

          -Frinny

          Comment

          Working...