Multi Threading


V

VSK

Hi all,

Am using multithreading for the first time in .NETweb application.

I have code like below.Iam creating 3 methods to invoke 3 methods
simultaneously.
In each method i will the assign the loop index to var which i display in
the web page.
But it behaves very strangely.Sometimes the methods invoked by the
thread(m1/m2/m3) is not called at all.
Some times it calls just m1 and m2. like this the results are very
inconsistent.

Please advice me on this. Am i doing anything wrong.

Thanks
VSK

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.Label Label2;

private void Page_Load(object sender, System.EventArgs e)
{
DateTime dt1 = GetCurrentDateTime();


ThreadStart ts1 = new ThreadStart(this.m1);
Thread t1 = new Thread(ts1);
t1.Start();

ThreadStart ts2 = new ThreadStart(this.m2);
Thread t2 = new Thread(ts2);
t2.Start();

ThreadStart ts3 = new ThreadStart(this.m3);
Thread t3 = new Thread(ts3);
t3.Start();

// m1();
// m2();
// m3();

TimeSpan tst1 = GetTimeSpan(dt1);
DisplayOutPut(tst1,Label1,0);
}

private void m1()
{
int m = 0;
DateTime m1dt = GetCurrentDateTime();
for(int i=0;i<100000;i++)
{
m = i;

}
TimeSpan m1ts = GetTimeSpan(m1dt);
DisplayOutPut(m1ts,Label2,m);
}

private void m2()
{
int n=0;
DateTime m2dt = GetCurrentDateTime();
for(int j=0;j<100000;j++)
{
n=j;
}
TimeSpan m2ts = GetTimeSpan(m2dt);
DisplayOutPut(m2ts,Label3,n);

}

private void m3()
{
int o=0;
DateTime m3dt = GetCurrentDateTime();
for(int k=0;k<100000;k++)
{
o=k;
Debug.Write("loop3");
}
Label5.Text = "Completed the loop";
TimeSpan m3ts = GetTimeSpan(m3dt);
DisplayOutPut(m3ts,Label4,o);
}

private DateTime GetCurrentDateTime()
{
return DateTime.Now;
}

private TimeSpan GetTimeSpan(DateTime prev)
{
return DateTime.Now-prev;
}

private string GetTimeInSecs(TimeSpan ts)
{
return ts.Seconds.ToString();
}
private string GetTimeInMsec(TimeSpan ts)
{
return (ts.Seconds*1000+ts.Milliseconds).ToString();
}

private void DisplayOutPut(TimeSpan ts,Label lbl,int j)
{
lbl.Text = GetTimeInSecs(ts) + "---- " + GetTimeInMsec(ts) + " Looped "
+ j + "times";
}

#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
}
}
 
Ad

Advertisements

V

VSK

sorry i mean 3 threads to invoke 3 methods
VSK said:
Hi all,

Am using multithreading for the first time in .NETweb application.

I have code like below.Iam creating 3 methods to invoke 3 methods
simultaneously.
In each method i will the assign the loop index to var which i display in
the web page.
But it behaves very strangely.Sometimes the methods invoked by the
thread(m1/m2/m3) is not called at all.
Some times it calls just m1 and m2. like this the results are very
inconsistent.

Please advice me on this. Am i doing anything wrong.

Thanks
VSK

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.Label Label2;

private void Page_Load(object sender, System.EventArgs e)
{
DateTime dt1 = GetCurrentDateTime();


ThreadStart ts1 = new ThreadStart(this.m1);
Thread t1 = new Thread(ts1);
t1.Start();

ThreadStart ts2 = new ThreadStart(this.m2);
Thread t2 = new Thread(ts2);
t2.Start();

ThreadStart ts3 = new ThreadStart(this.m3);
Thread t3 = new Thread(ts3);
t3.Start();

// m1();
// m2();
// m3();

TimeSpan tst1 = GetTimeSpan(dt1);
DisplayOutPut(tst1,Label1,0);
}

private void m1()
{
int m = 0;
DateTime m1dt = GetCurrentDateTime();
for(int i=0;i<100000;i++)
{
m = i;

}
TimeSpan m1ts = GetTimeSpan(m1dt);
DisplayOutPut(m1ts,Label2,m);
}

private void m2()
{
int n=0;
DateTime m2dt = GetCurrentDateTime();
for(int j=0;j<100000;j++)
{
n=j;
}
TimeSpan m2ts = GetTimeSpan(m2dt);
DisplayOutPut(m2ts,Label3,n);

}

private void m3()
{
int o=0;
DateTime m3dt = GetCurrentDateTime();
for(int k=0;k<100000;k++)
{
o=k;
Debug.Write("loop3");
}
Label5.Text = "Completed the loop";
TimeSpan m3ts = GetTimeSpan(m3dt);
DisplayOutPut(m3ts,Label4,o);
}

private DateTime GetCurrentDateTime()
{
return DateTime.Now;
}

private TimeSpan GetTimeSpan(DateTime prev)
{
return DateTime.Now-prev;
}

private string GetTimeInSecs(TimeSpan ts)
{
return ts.Seconds.ToString();
}
private string GetTimeInMsec(TimeSpan ts)
{
return (ts.Seconds*1000+ts.Milliseconds).ToString();
}

private void DisplayOutPut(TimeSpan ts,Label lbl,int j)
{
lbl.Text = GetTimeInSecs(ts) + "---- " + GetTimeInMsec(ts) + " Looped "
+ j + "times";
}

#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
}
}
 
Ad

Advertisements

A

Alvin Bruney [MVP]

They are being called. The problem is by the time they are done, the page
has already completed processing and sent out to the client. The easiest way
to do this is to do a t1./t2./t3.join which will cause the page to wait for
all processing to be done so you can see the results of the thread
execution.
 

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