Threading

  • Thread starter Thread starter Amendra
  • Start date Start date
A

Amendra

Hi,

I want a certain task to be run by multiple threads. The issue is do we have
to create a new thread object foreach new thread and specify the same
threadStart deligate or is there a different approach to this.

Thanks
Amendra.
 
Amendra,

That is exactly what you want to do. You want to create the separate
threads and then point them all to the same ThreadStart delegate. However,
make sure that if you are accessing class-level fields (or static variables,
if it is a static method), that you synchronize access to those fields.

Hope this helps.
 
Yes, I am aware of the sync issues, what if we want to make the number of
threads dynamic, then I would not know how many threads to create in
advance.... any solution..

Thanks
Threading


Nicholas Paldino said:
Amendra,

That is exactly what you want to do. You want to create the separate
threads and then point them all to the same ThreadStart delegate. However,
make sure that if you are accessing class-level fields (or static variables,
if it is a static method), that you synchronize access to those fields.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Amendra said:
Hi,

I want a certain task to be run by multiple threads. The issue is do we have
to create a new thread object foreach new thread and specify the same
threadStart deligate or is there a different approach to this.

Thanks
Amendra.
 
Amendra,

If the operation is short running, then you should use the ThreadPool
class, and have the system delegate when to do the work. If not, then you
can have a collection of threads that is added to when you create your new
thread, and removed from when the thread has completed the work.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Amendra said:
Yes, I am aware of the sync issues, what if we want to make the number of
threads dynamic, then I would not know how many threads to create in
advance.... any solution..

Thanks
Threading


message news:%[email protected]...
Amendra,

That is exactly what you want to do. You want to create the separate
threads and then point them all to the same ThreadStart delegate. However,
make sure that if you are accessing class-level fields (or static variables,
if it is a static method), that you synchronize access to those fields.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Amendra said:
Hi,

I want a certain task to be run by multiple threads. The issue is do
we
have
to create a new thread object foreach new thread and specify the same
threadStart deligate or is there a different approach to this.

Thanks
Amendra.
 
Back
Top