multi-threading

Discussion in 'ASP .Net' started by chxant, Jul 14, 2004.

  1. chxant

    chxant Guest

    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
    }
    }
    chxant, Jul 14, 2004
    #1
    1. Advertising

  2. 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.

    "chxant" <> a écrit dans le message de
    news:...
    > 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
    > }
    > }
    Bruno Jouhier [MVP], Jul 14, 2004
    #2
    1. Advertising

  3. chxant

    tommy b Guest

    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???

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    tommy b, Jul 14, 2004
    #3
  4. chxant

    Scott Allen Guest

    I have an article on this issue here:
    http://odetocode.com/Articles/112.aspx

    Note that I would only recommend using background threads for Page
    processing in very unique circumstances. In most cases I feel you may
    be hurting scalability.

    --
    Scott
    http://www.OdeToCode.com

    On Wed, 14 Jul 2004 03:22:35 -0700, tommy b <> wrote:

    >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???
    >
    >*** Sent via Developersdex http://www.developersdex.com ***
    >Don't just participate in USENET...get rewarded for it!
    Scott Allen, Jul 14, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    38
    Views:
    1,262
    Dennis Lee Bieber
    Feb 15, 2005
  2. Replies:
    9
    Views:
    1,016
    Mark Space
    Dec 29, 2007
  3. Steven Woody
    Replies:
    0
    Views:
    394
    Steven Woody
    Jan 9, 2009
  4. Steven Woody
    Replies:
    0
    Views:
    436
    Steven Woody
    Jan 9, 2009
  5. akineko
    Replies:
    3
    Views:
    2,603
    Jesse Noller
    Jan 29, 2009
Loading...

Share This Page