473,772 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ThreadPool and Controling its threads

TY
Hi Everyone,
I have a multithreading application, a typical Main thread that uses
the ThreadPool.Queu eUserWorkItem method to add new threads to the
Thread POOL.
I need a way to control the threads created by the thread pool because
some created threads go indefinilty for a long period of time and I
would like to terminate these threads.

I know that I can set a timeout for a thread but that's only when I use
the Thread class shared methods like Thread.Start, but with threadpool
I don't really know how to do that.

I also use AutoResetEvent object to wait for unfinished threads. does
this sound like a way to suspend threads started by the threadpool
class?
here is a snippet of the code: notice at the bottom of the code, I save
crete AutoResetEvent objects in arraylist, so that I can iterate
through it and wait for unfinished threads.

While True
If rowCounter > sitesDataTable. Rows.Count - 1 Then 'we
have reached the end of the datatable
Exit While
End If

'make sure you don't exceed the maximum of the pool
(default is always 25 thread per pool)
WaitForThreads( )

'get the current row from the datarow
dtrow = sitesDataTable. Rows(rowCounter )

'prepare for the new thread
Dim MyThreadNotify As AutoResetEvent = New
AutoResetEvent( False)

m_newThreadsPoo lArr.Add(MyThre adNotify)

'create a new instance of the processfeed class
Dim ProcessSingleFe edInstance As ProcessSingleFe ed =
New ProcessSingleFe ed
'set the input parameters
ProcessSingleFe edInstance.Conn ectionString =
_connectionStri ng
ProcessSingleFe edInstance.site id = dtrow("id")
ProcessSingleFe edInstance.feed Url =
IIf(IsDBNull(dt row("feedurl")) , "", dtrow("feedurl" ))
ProcessSingleFe edInstance.feed Type =
IIf(IsDBNull(dt row("feedtype") ), "1", dtrow("feedtype "))

'add the thread to the pool
Dim queueStatus As Boolean
queueStatus = ThreadPool.Queu eUserWorkItem(N ew
WaitCallback(Ad dressOf ProcessSingleFe edInstance.pars efeed),
MyThreadNotify)

'give it enough time to start
Thread.Sleep(50 0)
printAvailThrea ds()

constructLog("t hread added to the pool")
'advance to the next row for the next while loop
rowCounter = rowCounter + 1

If rowCounter Mod 25 = 0 Then 'every 25 loops, write
the string builder to the log file.
FlushToLog()
End If
End While

constructLog("w hile loop is done!")
printAvailThrea ds()

'make sure that all threads are done before quiting
constructLog("w aiting for unfinished threads.")

FlushToLog()

Dim o As AutoResetEvent
For Each o In m_newThreadsPoo lArr
Dim myt As AutoResetEvent
myt = o
'myt.WaitOne(15 * 1000, False)
myt.WaitOne()
Next

Nov 21 '05 #1
0 1166

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

Similar topics

5
12345
by: Dan Battagin | last post by:
Is there a known bug with the interaction between the HttpWebRequest and the ThreadPool? I current spawn several HttpWebRequest's using BeginGetResponse, and they work for a while, using worker threads from the ThreadPool. However, eventually (relatively quickly) there become fewer and fewer available worker threads in the pool, until there are 0 and a System.InvalidOperationException occurs with the message: "There were not enough free...
8
4810
by: memememe | last post by:
We are queueing stuff on the thread pool and what we are queueing gets done pretty quick (the method returns fine) but yet its only allowed to run around 25 times, is there anything I need to do to tell the threadpool that my method is done running and the thread is to be disposed of?
5
2761
by: Duane Pressley | last post by:
I'm looking for someone to help me make sense of the results I'm observing when using the ThreadPool class in a COM-Interop scenario. Here's the set up: 1.. A classic ASP page instantiates and calls MethodA on COM+ Class (C1). 2.. MethodA of COM+ class (C1) instantiates and calls MethodB of .NET class (N1). (Note: N1 has been registered using RegASM and is present in the GAC). 3.. MethodB of .NET class (N1) uses the...
6
2758
by: Max Adams | last post by:
Threads and ThreadPool's If I use a ThreadPool how can I tell when a thead in the threadpool has exited? I don't want to set a global member variable I would much rather be able to act on an event Also (failing this ThreadPool issue) is it possible to create an array of Worker Threads, can anyone illustrate with code please Thanks
13
2522
by: orekin | last post by:
Hi There I have been programming C# for a couple of months and am trying to master Threading. I understand that ThreadPool uses background threads (see code example in MSDN page titled 'ThreadPool Class ') .... however I would like to ensure that all my ThreadPool threads have completed before I exit my Main function.
5
1703
by: Peter Kirk | last post by:
Hi, I see in the ThreadPool documentation that the pool has a default limit of 25 threads. Is it correctly understood that this limit is for my entire application? So if I have several worker-threads each using "ThreadPool.QueueUserWorkItem" then the limit is spread over all my worker-threads - so if "worker-thread-1" currently has 20 calls in progress then there are only 5 thread-pool threads available for "worker-thread-2" ?
10
8780
by: Lenn | last post by:
Hello, I have always used a certain design pattern for multithreaded Windows app; Start new worker thread from UI thread, use events to notify UI threads when something happens, update UI controls using delegates through .BeginInvoke. I came across some code samples recently where ThreadPool class is used to start new threads. My questions; 1. what's the difference? 2. Any performance benefits?
1
3431
by: Sam | last post by:
Hi All, I'm trying to write a simple multi-threaded windows service application using ThreadPool and I'm running into the same problem as described by MS article: http://support.microsoft.com/default.aspx?scid=kb;en-us;815637#appliesto . The sample code down here is from the above article. Would anyone give me some directions how to overcome the deadlock issue with thread pull. The exception that I get after all threads in the...
3
2498
by: UltimateBanoffee | last post by:
Hi, I'm using asp.net 2.0 and I have an understanding issue here! I don't quite understand when the available threads in the ThreadPool are ever used. The application I have running doesn't use ThreadPool.QueueWorkItem, and doesn't create any other Threads. So say I have 100 users access my application at the exact same time and they all call on an aspx that takes a couple of seconds to process. I gather 100 sessions are active, but...
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10106
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
10039
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
9914
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
8937
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...
0
5355
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
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
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
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.