Threading newbie: Threads on instance methods

J

Jan

Hi Group,

are there any issues I should be aware of when executing a thread on an
*instance* method as opposed to a *static* method?

My code samples seems to work fine, and theoretically using an instance
method should be better, since there will be less code pieces contending for
the ressources. However I can imagine all kinds of weird stuff going on
underneath that I am unaware of.

(The reason I'm using an instance method is to access instance specific
information as per:
http://msdn2.microsoft.com/en-us/library/ts553s52(VS.80).aspx)
(The reason I'm asking this question is that I have found 100+ samples using
static methods and only one using an instance method, and this makes me
nerveous)

Kind Regards
Jan
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jan said:
are there any issues I should be aware of when executing a thread on an
*instance* method as opposed to a *static* method?

Not really.

Just be very carefully when deciding to lock on a
static (shared) object or an instance specific object.

C#/.NET behaves just as expected for both. But you
still need to determine what is the correct behavior.

Arne
 
W

William Stacey [C# MVP]

As Arne said, this is ok and needed many times. As normal you need to sync
shared state vars (but you need to do that with static method also, so the
concept is the same).

--
William Stacey [C# MVP]

| Hi Group,
|
| are there any issues I should be aware of when executing a thread on an
| *instance* method as opposed to a *static* method?
|
| My code samples seems to work fine, and theoretically using an instance
| method should be better, since there will be less code pieces contending
for
| the ressources. However I can imagine all kinds of weird stuff going on
| underneath that I am unaware of.
|
| (The reason I'm using an instance method is to access instance specific
| information as per:
| http://msdn2.microsoft.com/en-us/library/ts553s52(VS.80).aspx)
| (The reason I'm asking this question is that I have found 100+ samples
using
| static methods and only one using an instance method, and this makes me
| nerveous)
|
| Kind Regards
| Jan
|
|
|
 
J

Jan

Thanks William. I wonder why all the samples I encounter is on statics,
though.

- Jan
 
W

William Stacey [C# MVP]

Probably easier to document cause you can just show the static method and
people just get it. With an instance method, the tendancy is to want to see
the whole class - I am guessing.

--
William Stacey [C# MVP]

| Thanks William. I wonder why all the samples I encounter is on statics,
| though.
|
| - Jan
|
|
| "William Stacey [C# MVP]" <[email protected]> skrev i en meddelelse
| | > As Arne said, this is ok and needed many times. As normal you need to
| > sync
| > shared state vars (but you need to do that with static method also, so
the
| > concept is the same).
| >
| > --
| > William Stacey [C# MVP]
| >
| > | > | Hi Group,
| > |
| > | are there any issues I should be aware of when executing a thread on
an
| > | *instance* method as opposed to a *static* method?
| > |
| > | My code samples seems to work fine, and theoretically using an
instance
| > | method should be better, since there will be less code pieces
contending
| > for
| > | the ressources. However I can imagine all kinds of weird stuff going
on
| > | underneath that I am unaware of.
| > |
| > | (The reason I'm using an instance method is to access instance
specific
| > | information as per:
| > | http://msdn2.microsoft.com/en-us/library/ts553s52(VS.80).aspx)
| > | (The reason I'm asking this question is that I have found 100+ samples
| > using
| > | static methods and only one using an instance method, and this makes
me
| > | nerveous)
| > |
| > | Kind Regards
| > | Jan
| > |
| > |
| > |
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top