Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Parrallel Page Async Tasks not running in parrallel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Norm - 18 Mar 2008 00:36 GMT
Hi,

I have an page that accesses a lot of different tables in a database.
I am upgrading this page to use async tasks to increase performance.
The problem is that through tracing, I see that the callbacks are
running on different threads, but they are not running in parrallel.
Here is an example/psuedo-code.

Page PreRender
Public Conn as new SqlConnection("Data Source=***;Initial
Catalog=***;Integrated Security=True;Asynchronous
Processing=True;MultipleActiveResultSets=True;")
Conn.open()

UserControl
Private Cmd as SqlCommand

UserControl PreRender
Cmd = new SqlCommand("...",Page.Conn)
Page.RegisterAsyncTask(New PageAsyncTask(AddressOf BeginGetItems,
AddressOf EndGetItems, AddressOf GetItemsTimeout, Nothing, True))

Public Function BeginGetItems(ByVal sender As Object, ByVal e As
System.EventArgs, ByVal cb As System.AsyncCallback, ByVal extraData As
Object) As System.IAsyncResult
           Trace.Write("ShowItemControls:" & Me.ID, "Async Start.
Thread ID:" & Threading.Thread.CurrentThread.ManagedThreadId)
           Return Cmd(cb, Nothing)
       End Function

Public Sub EndGetItems(ByVal ar As System.IAsyncResult)
           Trace.Write("ShowItemControls:" & Me.ID, "Async Return.
Thread ID:" & Threading.Thread.CurrentThread.ManagedThreadId)
           Dim reader As SqlDataReader
           Dim InventoryDT As New CartDataSet.InventoryDataTable
           reader = RelatedItemsCommand.EndExecuteReader(ar)
           InventoryDT.Load(reader)
           reader.close
           DataList1.DataSource = InventoryDT
           DataList1.DataBind
           Trace.Write("ShowItemControls:" & Me.ID, "Async Finished.
Thread ID:" & Threading.Thread.CurrentThread.ManagedThreadId)
End Sub

Page PreRenderComplete
Conn.Close()

Please note that this code is not complete, it is a summary. There are
no errors, no timeouts, its just that the callback functions wait for
a previous callback function to execute. Here is a copy of the trace
for this page:

ShowItem:ShowItem1 Async Start. Thread ID:17 49.5234514 0.000250
ShowItemControls:RelatedItems1 Async Start. Thread ID:17 51.8805972
2.357146
ShowItemControls:RelatedItems2 Async Start. Thread ID:17 51.88228508
0.001688
ShowItemControls:ItemNotes1 Async Start. Thread ID:17 51.88273976
0.000455
ShowItemControls:ItemNotes2 Async Start. Thread ID:17 51.88332104
0.000581
ShowItemControls:Applications Async Start. Thread ID:17 51.88357552
0.000254
ShowItem:ShowItem1 Async Return. Thread ID:17 51.88891436 0.005339
ShowItem:ShowItem1 Async Finished. Thread ID:17 54.955182 3.066268
ShowItemControls:RelatedItems1 Async Return. Thread ID:16 54.95535484
0.000173
ShowItemControls:RelatedItems1 Async Finished. Thread ID:16
54.96175108 0.006396
ShowItemControls:RelatedItems2 Async Return. Thread ID:15 54.96197228
0.000221
ShowItemControls:RelatedItems2 Async Finished. Thread ID:15
54.96671028 0.004738
ShowItemControls:ItemNotes1 Async Return. Thread ID:18 54.9668546
0.000144
ShowItemControls:ItemNotes1 Async Finished. Thread ID:18 54.96698548
0.000131
ShowItemControls:ItemNotes2 Async Return. Thread ID:19 54.9670866
0.000101
ShowItemControls:ItemNotes2 Async Finished. Thread ID:19 54.96721528
0.000129
ShowItemControls:Applications Async Return. Thread ID:20 54.967299
0.000084
ShowItemControls:Applications Async Finished. Thread ID:20 54.97696712
0.009668

Note: There is a thread.sleep(3000) immediatly after
ShowItem:ShowItem1 Async Return. This shows that all threads are
waiting on that thread.

I would appreciate any help or information. The only thing that I can
think of is some lock behind the scenes that is blocking these
threads, but what lock? Anyway, thanks in advance!

Norm
Norm - 18 Mar 2008 00:52 GMT
Btw, the page "async" directive is set to true. Forgot to say that.
bruce barker - 18 Mar 2008 01:28 GMT
you are not understanding async tasks. they do not allocate a thread per
task (that would be a thread pool). an async operation does not block
the current thread:

  startAsync()
  dosomework()
  waitForAsyncComplete()
  handleAsyncCompleteEvent()

this is all on one thread. asp.net allows the thread to return to the
pool, while the page async events occur, and at completion pluck a
thread from the pool to handle the complete routine. the complete
routines are placed in queue, and only 1 thread is used to dequeue
(though thread agility allows thread switching).

the point of async in asp.net is to release the thread while the async
opertions happen (you can also overlap async operations)

in your case I don't see any async code in the first place (all sql is
done sync) and a sleep ties up a thread (not async).

> Hi,
>
[quoted text clipped - 91 lines]
>
> Norm
Norm - 18 Mar 2008 01:49 GMT
> you are not understanding async tasks. they do not allocate a thread per
> task (that would be a thread pool). an async operation does not block
[quoted text clipped - 114 lines]
>
> - Show quoted text -

@bruce barker:
I want to double check and make sure that I understand this. What you
are saying is that when the sql command calls back, the async task
completion delegate is placed into a queue and executed synchronously
even though they are separate threads? Thanks for the help. From what
I can see, you are one of the most active and helpful posters here.
Peter Bromberg [C# MVP] - 18 Mar 2008 03:47 GMT
Norm,
Bruce is right. Here is a short article I put together to try and illustrate
the asych page processing mechanism. This does *not* use SQL calls in an
effort to show the asynch page processing paradigm without confusing asynch
SQL calls into the mix:

http://www.eggheadcafe.com/articles/20060918.asp

HTH,
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net

> > you are not understanding async tasks. they do not allocate a thread per
> > task (that would be a thread pool). an async operation does not block
[quoted text clipped - 121 lines]
> even though they are separate threads? Thanks for the help. From what
> I can see, you are one of the most active and helpful posters here.
bruce barker - 18 Mar 2008 16:52 GMT
yes, thats the advantange of async vs thread pool, it does not use any extra
threads.

note: you should use the sqlcommand BeginExecuteReader in BeginGetItems to
make the sql async.

-- bruce (sqlwork.com)

> > you are not understanding async tasks. they do not allocate a thread per
> > task (that would be a thread pool). an async operation does not block
[quoted text clipped - 121 lines]
> even though they are separate threads? Thanks for the help. From what
> I can see, you are one of the most active and helpful posters here.
Norm - 18 Mar 2008 21:31 GMT
On Mar 18, 8:52 am, bruce barker
<brucebar...@discussions.microsoft.com> wrote:
> yes, thats the advantange of async vs thread pool, it does not use any extra
> threads.
[quoted text clipped - 131 lines]
>
> - Show quoted text -

Thanks everyone for all your help. I didn't completely understand
these things and those articles helped. Have a good week!
msdngroup - 18 Mar 2008 04:27 GMT
See this site for true asycnhronous pages in parallel at the site
http://www.udaparts.com/document/articles/asyniis.htm with all of sources
available for both C# and vb.net

> Hi,
>
[quoted text clipped - 91 lines]
>
> Norm

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.