multi-threading

C

chxant

Hi,

I tried a simple example of multi-threading, but it's not working.
test() is never excecuted.
What's wrong??

This is the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;

namespace Threading
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

Thread background = new Thread(new ThreadStart(test));
background.Start();
}
private void test(){
Label1.Text="ok";
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
 
B

Bruno Jouhier [MVP]

What are you trying to do? You do not wait for the thread to complete so the
test method may very well execute after the page has been generated (this is
actually very likely). Then, it is normal that you don't see anything.

Bruno.
 
T

tommy b

Thanks for your answer.
I'll try to explain what I'm trying to do.
I've got a web application with a request to a dbase. This request takes
some time. So, I want to inform the client using multi-threading.

I never used this, that's why I created a new (simple) project just to
try it out.
I know it's possible to simulate a long process using
Thread.Sleep(10000). But ... how can I "force" to excecute test() before
the page has been generated???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top