Threadpool doubt

  • Thread starter Thread starter rajesh
  • Start date Start date
R

rajesh

Hi,
I have some doubts about threadpool.

I have a function which does some calculation for long time and writes
the results to a file. Since I have to do multiple call to the same
functions, I used threadpool.

here is the snap shot of the function in brief...

private void DoMath(object Items)
{
//calculate another object and gets some value
.....
//writes the value to the file whose defination is defined as
global
........
//calculation here
........
Writes the value to the file
//calculate another object and gets some value
.....
//writes the value to the file whose defination is defined as
global
........
File.Flush()
}

I have to open 5 to 15 simultaneous calls to DoMath object.

I still couldn't make it work but I have couple of questions here.

1. File.Flush -- Will this writes all the value for the current
component or will it write all the values from other component ?.
2. if the above solution doesn't work...how do i organize the data in
this scenario ?.
3. How do I check all the threads excecuted successfully ?.
I used in main

int mWorkerThreads;
int mPortThreads;
int aWorkerThreads;
int aPortThreads;
ThreadPool.GetMaxThreads(out mWorkerThreads, out mPortThreads);
ThreadPool.GetAvailableThreads(out aWorkerThreads, out
aPortThreads);

if (mWorkerThreads == aWorkerThreads && mPortThreads ==
aPortThreads)
// Thread worked successfully...

I dont think it works fine though...

It will be helpful if you give some pointer to any website which
explains this..

Thanks.....
 
rajesh,

See inline:
I have a function which does some calculation for long time and writes
the results to a file. Since I have to do multiple call to the same
functions, I used threadpool.

If the operations take a long time, then the thread pool might not be
the best solution for you. The thread pool is meant for short running
operations.
1. File.Flush -- Will this writes all the value for the current
component or will it write all the values from other component ?.

I assume that File is a FileStream instance. If so, then a call to
Flush will only flush the contents of the current stream.
2. if the above solution doesn't work...how do i organize the data in
this scenario ?.

If you need to flush all the streams, then you need to explicitly call
Flush on each stream. Also, make sure you are calling Close (or
IDisposable.Dispose) on the Stream.
3. How do I check all the threads excecuted successfully ?.

You should set a flag somewhere at the end of the code that executes in
the thread, and check the collection of flags. Counting the number of
threads in the threadpool is not going to do anything, because there are
many areas of the .NET framework that use them.

Hope this helps.
 
Nicholas Paldino said:
If the operations take a long time, then the thread pool might not be
the best solution for you. The thread pool is meant for short running
operations.

Agreed. That doesn't mean you can't use *a* thread pool though - it's
just that the system thread pool probably isn't a good choice.

I have a thread pool class you can use in the library available at
http://www.pobox.com/~skeet/csharp/miscutil

Feel free to adapt it as you see fit, of course.
 
Back
Top