Home All Groups Group Topic Archive Search About

Quick Questions on threading

Author
4 Jan 2005 10:20 PM
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.

Author
4 Jan 2005 10:34 PM
Richard Blewett [DevelopMentor]
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.
Are all your drivers up to date? click for free checkup

Author
4 Jan 2005 11:07 PM
Weston Weems
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:
>
>
http://www.ondotnet.com/pub/a/dotnet/2003/02/24/asyncdelega
tes.html
Show quoteHide quote
>
> 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.
>
>.
>
Author
4 Jan 2005 11:10 PM
John Saunders
"Weston Weems" <anonym***@discussions.microsoft.com> wrote in message
news:01bf01c4f2ab$98df0aa0$a401280a@phx.gbl...
> 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
Author
5 Jan 2005 2:07 AM
Jon Skeet [C# MVP]
<"John Saunders" <johnwsaundersiii at hotmail.com>> wrote:
> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Author
5 Jan 2005 2:09 PM
John Saunders
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1c455c1f25ee144798bb98@msnews.microsoft.com...
> <"John Saunders" <johnwsaundersiii at hotmail.com>> wrote:
>> 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.

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

John Saunders

Bookmark and Share