Parrallel Page Async Tasks not running in parrallel

N

Norm

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
 
B

bruce barker

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).
 
N

Norm

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).
















- 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.
 
B

bruce barker

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)
 
N

Norm

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)






- Show quoted text -

Thanks everyone for all your help. I didn't completely understand
these things and those articles helped. Have a good week!
 

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

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top