Multithreading in asp.net - pls reply

P

Pradnya Patil

hi ,
I am trying to draw ' html div-tag ' on the screen which will
resemble a rectangle through vb.net code.
I want it to be drawn faster...so I introduced multithreading
using Threadpool. I divided the complete drawing into 3 parts..1st will
be done by main thread and other two are done in these procedures -
<1> LongTimeTask
<2> LongTimeTask2
I have invoked the threads using below method.
**************
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
LongTimeTask), "hi")
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
LongTimeTask2), "hi"
**************

Now ,the problem is...sometimes I can't see the html generated
by the functions LongTimeTask and LongTimeTask2.
While debugging, I have observed that these procedures
sometimes execute after Page_load is completely executed.
Can anybody identify where the problem is ? I am just exploring
multithreading and don't know in depth abt it .Am i missing some code
for multithreading ?
Any help will be appreciated.
This is the code:
'---------------------------------------------------------------------------------------------------

Imports System.Threading

Partial Class TestSpdThreadPool
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

MainFun()

End Sub

Public Sub MainFun()
Dim intCnt2 As Integer
Dim intB As Integer = 0
Dim strb As New StringBuilder

ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
LongTimeTask), "hi")
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
LongTimeTask2), "hi")

For intCnt2 = 0 To 10000 Step 40
If intB = 500 Then
Me.Form.InnerHtml &= strb.ToString
strb.Remove(0, strb.Length)
intB = 0
End If

strb.Append(" <div style='width: 6px; height:6px;
background-color: transparent; z-index: 1;left: " & intCnt2 & "px;
position: absolute; top: " & 100 & "px;'><b>M</b></div>")

Next
Me.Form.InnerHtml &= strb.ToString

End Sub

Public Sub LongTimeTask(ByVal str As Object)
'Thread.Sleep(100)
Dim intCnt3 As Integer
Dim intB3 As Integer
Dim strb As New StringBuilder

For intCnt3 = 0 To 10000 Step 40
If intB3 = 500 Then
Me.Form.InnerHtml &= strb.ToString

strb.Remove(0, strb.Length)
intB3 = 0
End If
strb.Append("<div style='width: 6px; height: 40px;
background-color: red; z-index: 2;left: " & intCnt3 + 500 & "px;
position: absolute; top: " & 200 & "px;'><b>T----------P</b></div>")
'strb.Append("thread 1 ----->")
Next
Me.Form.InnerHtml &= strb.ToString
End Sub

Public Sub LongTimeTask2(ByVal str As Object)
Dim intCnt3 As Integer
Dim intB3 As Integer
Dim strb As New StringBuilder
'Thread.Sleep(100)
For intCnt3 = 0 To 10000 Step 40
If intB3 = 500 Then
Me.Form.InnerHtml &= strb.ToString

strb.Remove(0, strb.Length)
intB3 = 0
End If
strb.Append("<div style='width: 6px; height: 40px;
background-color: blue; z-index: 2;left: " & intCnt3 + 10 & "px;
position: absolute; top: " & 300 & "px;'><b>T----------P</b></div>")
'strb.Append("thread 1 ----->")
Next
Me.Form.InnerHtml &= strb.ToString
End Sub

End Class
'---------------------------------------------------------------------------------------------------
 
S

Samuel R. Neff

How big is your HTML that you need to multithread the code to generate
it quickly? Seems like you'll have more problems than just generating
the HTML--it'll be a lot of HTML to send to the user. I'd look for a
way to refactor the UI so HTML is smaller or use DHTML to send just a
template and the data to the client and use javascript to genearte the
whole thing (faster transmission, slower rendering).

However, if you're set on using multiple threads there are some issues
and suggestions..

1. You can't have multiple threads accessing Me.Form and appending to
it's InnerHtml. YOu have no real control over what gets inserted in
what order and can generally end up with a huge mess. Also since
InnerHtml is not threasafe the &= operation could end up wiping out
html written by the other thread. In general each thread should do
it's own work creating it's own stream of html and then they should be
combined at the end or you should use locking to ensure only one
thread is actually writing at a time.

2. Don't use strings. If you're really generating that much HTML
don't do it by using InnerHtml or strings or &= at all. Instead
override the Render method of your page and put the appropriate
rendering code in there directly to the response stream. This will
give you better memory usage and performance.

3. Wait for worker threads. When you kick off extra worker threads
to do things and you want to synchronize results, use a
ManualResetEvent to wait for the threads. Create one for each task
and pass it to the thread in the state parameter. The caller should
call WaitOne on each MaualResetEvent instance to wait for the tasks to
complete. Each task should call Set on their respective
ManualResetEvent to trigger to the caller that they are complete.

4. ThreadPool is limited. The thread pool has a limited number of
threads--by default 25 per processor. If you have a web page that
kicks off two threadpool threads it will only take 10-13 requests o
saturate the thread pool and cause later requests to be queued and
take even longer (actually a few threads are used up by .NET so you
can't even rely on having all 25 threads available). You can create
your own threads or keep track of the threadpool and do some requests
single-threaded if the threadpool is full but in general this type of
threading in a web application is extrememly unusual (web-apps are
inherintly multithreaded already).

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
 
P

Pradnya Patil

Thanks Samuel...for the detailed explanation.It really
gave me an insight into what I am doing exactly. Many thanks....After
reading this I feel that it will not be efficient to implement
multithreading in this case.

My BASIC problem is -- I want 2 draw huge graphs which
needs very large canvas.
Currenly I am using bitmap object ( .net GDI+ API ) to draw smaller
graphs but bitmap object has certain limitations

<1> U can't cross particular height and width..otherwise it throws an
exception.
<2> Image quality is good but renderig is slow.
One of my collegues tried with Java script graph
library..but the canvas has some limitation..U can't draw really huge
graphs..And it wont work if Javascript is disabled in target
browser..Thats why I'm experimenting with this 'HTML' kind of
thing..atleast it draws faster. I ALSO TRIED UR SUGGESTION - putting
the drawing -code in PAGE_PRERENDERCOMPLETE event but still it takes
3-4 minutes to draw big graph..I want to still reduce
it..And one more imp. thing...to draw lines, I have written
my own function coz theres a req. to draw connections between
rectangles..WHICH IS CONTRIBUTING TO MOST OF THE EXECUTION TIME.
How do i reduce it ?
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top