473,696 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic wait on threads

Hello,

I have two threads T1 and T2.

T2 starts thread T1 and waits for 1 minute for T1 to finish (using
Join) and if it doesn't finish within that time, it aborts the thread
T1.

Now, I want to introduce dynamic wait in this. i.e. I want T1 to
communicate to T2 that it needs more time to finish and then
accordingly I want T2 to wait.

How do I accomplish this using C#? I thought off using thread local
storage for that. However, I don't see a way for thread T2 to access
the TLS of T1 from within T2.

Any help on this would be appreciated.

-Prasad

Nov 29 '05 #1
4 1851
Prasad wrote:
Hello,

I have two threads T1 and T2.

T2 starts thread T1 and waits for 1 minute for T1 to finish (using
Join) and if it doesn't finish within that time, it aborts the thread
T1.

Now, I want to introduce dynamic wait in this. i.e. I want T1 to
communicate to T2 that it needs more time to finish and then
accordingly I want T2 to wait.

How do I accomplish this using C#? I thought off using thread local
storage for that. However, I don't see a way for thread T2 to access
the TLS of T1 from within T2.

Any help on this would be appreciated.

-Prasad


You could create a variable that holds a counter - say how much more
time T1 needs. Both threads would need access to this variable.

Then use a synchronization object to lock() access to that variable. T1
would update it as it progresses, and T2 can check that if it needs to
wait more.
-Lenard
Nov 29 '05 #2
Prasad wrote:
I have two threads T1 and T2.

Now, I want to introduce dynamic wait in this. i.e. I want T1 to
communicate to T2 that it needs more time to finish and then
accordingly I want T2 to wait.


I would recommend wrapping your T1 thread in a class and then add some
method or property that T2 could call to query T1 whether or not it
needs more time.

'Inside T2

If T1Class.QueryMo reTime() Then
'Do something
End If

Nov 29 '05 #3
Prasad wrote:
Hello,

I have two threads T1 and T2.

T2 starts thread T1 and waits for 1 minute for T1 to finish (using
Join) and if it doesn't finish within that time, it aborts the thread
T1.


While not being an answer to your question, the other two have managed
that just fine, I would recommend you find a different solution to this
"aborts the thread" part.

Everything you do inside that tread basically needs to be wrapped in a
try/catch block to make sure you can react to the outside code forcibly
terminating it at the worst possible point.

What is it that this thread is doing? Wouldn't using an Event object or
similar be a better solution? I know that you wouldn't be able to
forcibly kill the thread no matter what it does, instead you would ask
the thread to stop doing whatever it is doing, and you'd have to check
this event at specific points inside the thread... But at least the
thread could be terminated properly, from the inside.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
Nov 30 '05 #4
Hello,

Thank you all for your responses. I used a mix of solution proposed by
Lenard and Chris and got it working.

While I agree with what Lasse has to say, I cannot use it directly at
this point. The reason is, T1 thread executes different code at
different times and its huge code. It's not feasible to fix all that
code to use the event object etc. Instead I opted for creating a
framework around T1 and T2 that uses thread local storage to
communicate time requirements through a shared data structure.

One question that remains unanswered is, "Is it possible to query TLS
of thread T1 from thread T2?"

Thanks.
-Prasad

Lasse Vågsæther Karlsen wrote:
Prasad wrote:
Hello,

I have two threads T1 and T2.

T2 starts thread T1 and waits for 1 minute for T1 to finish (using
Join) and if it doesn't finish within that time, it aborts the thread
T1.


While not being an answer to your question, the other two have managed
that just fine, I would recommend you find a different solution to this
"aborts the thread" part.

Everything you do inside that tread basically needs to be wrapped in a
try/catch block to make sure you can react to the outside code forcibly
terminating it at the worst possible point.

What is it that this thread is doing? Wouldn't using an Event object or
similar be a better solution? I know that you wouldn't be able to
forcibly kill the thread no matter what it does, instead you would ask
the thread to stop doing whatever it is doing, and you'd have to check
this event at specific points inside the thread... But at least the
thread could be terminated properly, from the inside.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2


Dec 5 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2390
by: Guinness Mann | last post by:
When you guys talk about "dynamic SQL," to what exactly are you referring? Is dynamic SQL anything that isn't a stored procedure? Specifically, I use ASP.NET to communicate with my SQL Server 2000, using an SqlConnection object to open the database and an SqlCommand object to transfer my SQL text to the database. Is this the "dynamic SQL" that is such a bad thing? What is my alternative? Wait until after my program is working and...
2
13560
by: Vespasian | last post by:
notify() and wait() throw an exception if the calling thread does not own the object's monitor. Does this imply that the check is made at runtime, and if so, why can't it be made at compile time? TIA, ves
9
1712
by: Roger Down | last post by:
Lets say I have a method UpdateCache() called from a single thread. I also have a method GetCache() called from multiple threads. When UpdateCache() is called, the cache updating is being processed. This can take time. In the mean time, some of the multiple threads have called GetCache(), but because the cache is being updated, I want them to wait for the UpdateCache() to finish. Something like this, in a very simplified code:
11
20354
by: Peter Kirk | last post by:
Hi there I am looking at using a thread-pool, for example one written by Jon Skeet (http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if this pool provides the possibility to wait for all its threads to finish? For example, if I start 20 threads: CustomThreadPool pool = new CustomThreadPool("PetersThreadPool"); ThreadMethod m = new ThreadMethod(InsertThread);
12
5266
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock block protecting a resource. Thread 2 and 3 also have a SyncLock block protecting the same resource and after executing some code in their blocks they both do a Monitor.Pulse to hand of the locked resource back to thread 1. While thread 1 has...
16
30054
by: Thirsty Traveler | last post by:
I would like to create a test harness that simulates multiple concurrent users executing an individual thread. I would like this to be determined at runtime when the user specifies the number of desired threads. When this is kicked off, I would like to wait in the primary thread until all worker threads have completed and time the result... one problem... I can't figure out how to wait for all threads to complete prior to updating my...
4
1958
by: herman | last post by:
Hi, In my python program, I would to like to spwan 5 threads, for the them for 5 minutes maximum and the continue. Here is my script: threads = for j in range(5): t = MyThread() threads.append(t)
2
3483
by: greyradio | last post by:
I've recently have been given an assignment to do and it seems that notify() does notify() any of the waiting threads. The project entails 10 commuters and two different toll booths. The EZPass booth allows a commuter to pass easily while a cash booth creates a wait line. The wait line is expected to wait for the 5th commuter to pass before any commuter on the wait line can pass through the toll booth. This is where my problem lies. All the...
0
8592
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9005
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8871
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8846
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7694
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4605
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3025
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.