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 / December 2006

Tip: Looking for answers? Try searching our database.

Multithreading in asp.net - pls reply

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pradnya Patil - 29 Dec 2006 16:17 GMT
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
'---------------------------------------------------------------------------------------------------
Samuel R. Neff - 29 Dec 2006 17:00 GMT
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.

>   hi ,
>         I am trying to draw ' html div-tag ' on the screen which will
[quoted text clipped - 22 lines]
>This is the code:
>'---------------------------------------------------------------------------------------------------
Pradnya Patil - 30 Dec 2006 10:14 GMT
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 ?

> 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
[quoted text clipped - 80 lines]
> >This is the code:
> >'-------------------------------------------------------------------------­--------------------------- Hide quoted text -- Show quoted text -

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.