Quick Questions on threading

  • Thread starter Thread starter Weston Weems
  • Start date Start date
W

Weston Weems

I've been toying with threads the past couple days and I
was curious if anyone could answer a few basic questions.

1) I'd like to take a method call I make currently
successfully, and simply throw it into a new thread to run
until the method finishes, at which point in time the
thread would terminate.

What would be the best approach to do this. I dont ind
doing some reading... I tried to be resourcefull ahead of
time.
 
Hmm I counted only one question ;-)

How to make the call on another thread depends on what that thread is going to do. If its a long running activity or you want full control over te thread then create a new instance of the Thread class and run the method on that (there are issues of how you supply parameters but we'll come to that if you neeed that facility). if you simply want to run the method on another thread and its not particularly long running then use the CLR threadpool by creating a delegate that wraps the method and call BeginInvoke on the delegate instance. Youo will have to call EndInvoke on the instance too but there are a few ways to do that. I write an article on async delegate invocation a while back:

http://www.ondotnet.com/pub/a/dotnet/2003/02/24/asyncdelegates.html

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I've been toying with threads the past couple days and I
was curious if anyone could answer a few basic questions.

1) I'd like to take a method call I make currently
successfully, and simply throw it into a new thread to run
until the method finishes, at which point in time the
thread would terminate.

What would be the best approach to do this. I dont ind
doing some reading... I tried to be resourcefull ahead of
time.
 
Well the thread is going to fire off a method to populate
webcache.

It normally takes about 1-2minutes, which I dont know if
you'd consider is a long running task.

I'd like to use the fire and forget method, then fire
off "LoadData(var)" and have it fetcht he data into the
cache.

Heres what I do now:

new Thread (new ThreadStart
(BusAllPromoCF.LoadCacheItem)).Start();

from my Application_Start
and that does succesffully spawn a thread and load data
into the cache. When the CacheItemRemovedCallBack gets
fired, I need to reload data on a seperate thread.

I want to make sure I am not spawing a buncha threads only
to have them take up unnecessary resources.

Thanks.,


-----Original Message-----
Hmm I counted only one question ;-)

How to make the call on another thread depends on what
that thread is going to do. If its a long running activity
or you want full control over te thread then create a new
instance of the Thread class and run the method on that
(there are issues of how you supply parameters but we'll
come to that if you neeed that facility). if you simply
want to run the method on another thread and its not
particularly long running then use the CLR threadpool by
creating a delegate that wraps the method and call
BeginInvoke on the delegate instance. Youo will have to
call EndInvoke on the instance too but there are a few
ways to do that. I write an article on async delegate
invocation a while back:
 
Weston Weems said:
I've been toying with threads the past couple days and I
was curious if anyone could answer a few basic questions.

1) I'd like to take a method call I make currently
successfully, and simply throw it into a new thread to run
until the method finishes, at which point in time the
thread would terminate.

What would be the best approach to do this. I dont ind
doing some reading... I tried to be resourcefull ahead of
time.

The first thing you should do is to determine whether your existing method
uses any instance data. In particular, if you can set the method to be
static (in C#) or Shared (in VB), then it doesn't use any instance data, and
you're ok.

If it _does_ use instance data, then you may need to synchronize access to
that data betweeen the main thread and your new thread.

John Saunders
 
The first thing you should do is to determine whether your existing
method uses any instance data. In particular, if you can set the
method to be static (in C#) or Shared (in VB), then it doesn't use
any instance data, and you're ok.

If it _does_ use instance data, then you may need to synchronize
access to that data betweeen the main thread and your new thread.

Just because a method is static doesn't mean it doesn't need to do any
synchronization - it could easily still be using shared data.
 
Jon Skeet said:
Just because a method is static doesn't mean it doesn't need to do any
synchronization - it could easily still be using shared data.

Yes, Jon is correct. Synchronization may be needed for accessing anything
which can be reached from multiple threads.

John Saunders
 
Back
Top