473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Multithreading in asp.net - pls reply


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 -
<1LongTimeTask
<2LongTimeTask2
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
'---------------------------------------------------------------------------------------------------

Dec 29 '06 #1
2 2235

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.

On 29 Dec 2006 08:17:09 -0800, "Pradnya Patil"
<pr************@gmail.comwrote:
>
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 -
<1LongTimeTask
<2LongTimeTask2
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:
'---------------------------------------------------------------------------------------------------
Dec 29 '06 #2

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

<1U can't cross particular height and width..otherwise it throws an
exception.
<2Image 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 ?


On Dec 29, 9:00 am, Samuel R. Neff <samueln...@nomail.comwrote:
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.

On 29 Dec 2006 08:17:09 -0800, "Pradnya Patil"

<pradnyapati...@gmail.comwrote:
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 -
<1LongTimeTask
<2LongTimeTask2
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:
'-------------------------------------------------------------------------*--------------------------- Hide quoted text -- Show quoted text -
Dec 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mark | last post by:
Is there a way to achieve multithreading in JavaScript? I'm looking to fetch a page into a div while allowing the user to interact with another div. At some point the newly fetched page contents...
2
by: ArmedCoder | last post by:
Hi, im just learning about multithreading for a program i am writin that needs to read from multiple camreas attached to the computer a the same time. I know how to create threads and pass around...
12
by: Winbatch | last post by:
Hi, I'm trying to learn multithreading and it doesn't seem to be working for me. I have a feeling it has to do with the fact that I'm writing to files rather than to printf, but maybe not. ...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
2
by: Rekkie | last post by:
Hi, I am trying to implement a ping client that is multithreading. The approach I have used is to create a ping class which I instantiate from the main thread and which contains a method...
5
by: Per Rollvang | last post by:
Hi All! I have been struggling with where what & when to set the mousepointer to an hourglass when using async. multithreading. Nothing seems to work... : ( Anybody that can help me out with...
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
1
by: madankarmukta | last post by:
Hi All, I have the application whose runtime argument will decides how many threads I have to create.All these threads functionality may differ by a step or two.Threads are going to use the same...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.