confusion regarding threading

A

archana

Hi all,

I have one confusion regarding threading in windows service which is
developed in c#.

What i am doing is on 'onstart' event i am starting one thread.

In thread procedure i am processing some URL's asynchronously.

Say in thread procedure for validating url through webrequest i am
calling beginwebrequest for procesing 5 urls at a time.

Does it means my thread is spawn into 5 threads. Means invoking
webrquest asynchronouly create new thread? And if yes does it create
thread in threadpool.

Secondly in beginwebrequest i am passing one callback fuction whcih
needs to get invoke after processing completed. This callback i am
handling in outer thread.

After after this process is over i am enabling one timer which is again
doing same things on its elapsed event. Means start processing of 5
webrequest asynchronously.

Can some one tell me is anything wrong in this code.

Becuase this code is hanging suddenly after working properly for say
3-4 times.

Please correct me if i am wrong.

thanks in advance.
 
J

James

hi,
Yes calling Any 'beginAsync' method creates a new thread to execute on.
As for it being created in the ThreadPool - no it is not. What I think you
are missing is the fact that you should use the Async callback to maybe call
the remainding urls. The hang does sound like you have a thread locking
problem, it has not been released. But I think a snippet of the code would
help.


James Jenkins
http://www.tamarsolutions.co.uk
 
B

Brian Gideon

archana said:
Hi all,

I have one confusion regarding threading in windows service which is
developed in c#.

What i am doing is on 'onstart' event i am starting one thread.

In thread procedure i am processing some URL's asynchronously.

Say in thread procedure for validating url through webrequest i am
calling beginwebrequest for procesing 5 urls at a time.

Does it means my thread is spawn into 5 threads. Means invoking
webrquest asynchronouly create new thread? And if yes does it create
thread in threadpool.

I'm pretty sure it won't spawn 5 new threads. It's more likely that
those requests are placed in the ThreadPool, use IO completion ports,
or something of the like. Threads are not necessarily created when
queueing work items into the ThreadPool anyway. If an idle thread
exists in the pool then the work item will likely be dispatched to that
thread.
Secondly in beginwebrequest i am passing one callback fuction whcih
needs to get invoke after processing completed. This callback i am
handling in outer thread.

That's impossible unless you are marshaling the execution of the
callback onto the outer thread. Callbacks from asynchronous operations
(especially those involing the BeginXXX and EndXXX pattern) are
generally executed on the same thread that processed the operation.
After after this process is over i am enabling one timer which is again
doing same things on its elapsed event. Means start processing of 5
webrequest asynchronously.

Which timer are you using (there are 3 in the base class library)?
Specifically, when and where are you enabling the timer? Is it
possible that the you're kicking off asynchronous requests faster than
they are completing?
Can some one tell me is anything wrong in this code.

Becuase this code is hanging suddenly after working properly for say
3-4 times.

Can you post some code?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

See inline
What i am doing is on 'onstart' event i am starting one thread.

The way to go.
In thread procedure i am processing some URL's asynchronously.

How many URLs are you doing it the same time? IMO this is the part where
the problem may be, depending of how you do it. Post this piece of the code.

Does it means my thread is spawn into 5 threads. Means invoking
webrquest asynchronouly create new thread? And if yes does it create
thread in threadpool.

Yes, each Async will create a new thread, don't really remember if it gets
them from threadpool , a search in MSDN will tell though.
Secondly in beginwebrequest i am passing one callback fuction whcih
needs to get invoke after processing completed. This callback i am
handling in outer thread.

Also a good candidate for a race condition, post the code , or just wrap the
entire method in a critical section.
After after this process is over i am enabling one timer which is again
doing same things on its elapsed event. Means start processing of 5
webrequest asynchronously.

It's possible the some of the previous one are still running so you have to
take that into account.

post some code .
 

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