473,320 Members | 2,146 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,320 software developers and data experts.

ThreadPool and Controling its threads

TY
Hi Everyone,
I have a multithreading application, a typical Main thread that uses
the ThreadPool.QueueUserWorkItem 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_newThreadsPoolArr.Add(MyThreadNotify)

'create a new instance of the processfeed class
Dim ProcessSingleFeedInstance As ProcessSingleFeed =
New ProcessSingleFeed
'set the input parameters
ProcessSingleFeedInstance.ConnectionString =
_connectionString
ProcessSingleFeedInstance.siteid = dtrow("id")
ProcessSingleFeedInstance.feedUrl =
IIf(IsDBNull(dtrow("feedurl")), "", dtrow("feedurl"))
ProcessSingleFeedInstance.feedType =
IIf(IsDBNull(dtrow("feedtype")), "1", dtrow("feedtype"))

'add the thread to the pool
Dim queueStatus As Boolean
queueStatus = ThreadPool.QueueUserWorkItem(New
WaitCallback(AddressOf ProcessSingleFeedInstance.parsefeed),
MyThreadNotify)

'give it enough time to start
Thread.Sleep(500)
printAvailThreads()

constructLog("thread 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("while loop is done!")
printAvailThreads()

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

FlushToLog()

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

Nov 21 '05 #1
0 1146

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

Similar topics

5
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...
8
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...
5
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...
6
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...
13
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...
5
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...
10
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...
1
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:...
3
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.