473,225 Members | 449 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,225 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 2227

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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.