473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

How can I lock thread till execution is finished

Hello All,

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

Expand|Select|Wrap|Line Numbers
  1. protected void dataTree_AfterSelect(event params)
  2. {
  3.   code that should not be disturbed
  4. }
  5.  
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
Jun 26 '12 #1
4 3917
Aimee Bailey
197 Expert 100+
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
Jul 1 '12 #2
Plater
7,872 Expert 4TB
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. lock(mylockingObject)
  3. {
  4. //code that shouldnt be interrupted by other events(re-entry)
  5. }
  6.  
Jul 2 '12 #3
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



@Plater
Oct 26 '12 #4
Frinavale
9,735 Expert Mod 8TB
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
Oct 26 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
3
by: Kiran | last post by:
Hi, Was wondering if its possible to have a event notification in Thread when the thread is about to start and the thread has just finished doing its work and about to terminate. If its not...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
0
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
7
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
2
by: palaga | last post by:
hi I'm using QueueUserWorkItem to execute a bunch of tasks using the thread pool. Once started, I would like to wait for all of them to finish, using something like WaitAll. Is there a way I can...
2
by: Don | last post by:
How to stop a process which is running in a separate thread!!! I've got a class which performs some lengthy process in a background (separate) thread. And this lengthy process raises events...
12
by: Tomaz Koritnik | last post by:
Is it possible to asynchronously call a method from worker thread so that this method would execute in main-thread? I'm doing some work in worker thread and would like to report progress to main...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.