multi-threading

tommyb

Newcomer
Joined
Jul 14, 2004
Messages
2
Hi,

I'm new to this subject.
Who can post a very simple example of multi-threading in asp.net (if possible C#)?
Thanks you very much!

Tommy.
 
Is there a particular problem you have or an idea for what you need multithreading for? Quite often multithreading in a web app will not behave as you would expect due to the way HTTP requests are handled.
 
I have a web application with a dbase query.
This takes some time.
I just want to inform the user that the query is started.
That's why I want to use multi-threading.
First thread on the background processing the query.
Second thread sending a message to the client (=changing the text of a label on the same page into "please wait..").
Is this possible?

Thankx!
 
tommyb said:
I have a web application with a dbase query.
This takes some time.
I just want to inform the user that the query is started.
That's why I want to use multi-threading.
First thread on the background processing the query.
Second thread sending a message to the client (=changing the text of a label on the same page into "please wait..").
Is this possible?

Thankx!

You do realize that if 100 users go to that page at the same time you get a 100 threads all running the same query on the server?

Anyway, some code to start a thread to do some heavy calculation. I used this in a windows form based applications so you probably have to rearrange the thread to an object that remains in memory after the user request the page, and can return the results to the user when he refreshes the page after the thread is finished.

First the declaration of the thread and thread starter.
PHP:
private ThreadStart m_objStarter = null;
private Thread m_objWorker = null;

Somewhere along the line I decide to start the background thread.
PHP:
//ensure the thread is not started a second time.
if (null == m_objWorker)  
{
//Create the thread starter object. It points to the DoTheLongStuff method that does the long stuff.
m_objStarter = new ThreadStart(DoTheLongStuff);
//Create a new thread.
m_objWorker = new Thread(m_objStarter);  
//Start the new thread
m_objWorker.Start();
}

Looking back at the code after a month, you dont need to make the thread starter a member object. Oh well, we all have to learn ;).
 
Think about Global class.
A thread in this section might give good result. However... make sure data isn't used by your pages or you might find problem with accessing the same data.
 
Back
Top