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:
'---------------------------------------------------------------------------------------------------