Threading in C#.Net Web application

Y

Yatharth

Hi,

I m new to threading and i have successfully runed threading but i
could display value on my web page ,but its working in code behind
when i see it through debugger,plzzzzzzz help me here is the code
below:

i just wana display the simple array value stored in my array variable
in my textbox thats it.

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 MultithreadingApplication.Class ;
using System.Threading;
using System.Data.SqlClient;



namespace MultithreadingApplication
{
public class WebForm2 : System.Web.UI.Page
{
string strglobal="";
Object objCust=new Object() ;
int j=0;


protected System.Web.UI.WebControls.Label Label1;


private void Page_Load(object sender, System.EventArgs e)
{

ThreadStart simplest = new ThreadStart(Simplest);
Thread thread1 = new Thread( simplest ) ;

thread1.Start() ;


Label1.Text=strglobal;


}

public string GetCustomers()//string city)
{
string []str=new string[2] ;
string strsql="select firstname,lastname from employees";
SqlConnection conn =new SqlConnection();
DataSet CustDS =new DataSet();
conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlCommand commnd=new SqlCommand(strsql,conn);
SqlDataReader dr ;
conn.Open();
dr=commnd.ExecuteReader();
if(dr.Read())
{


for(int i=0;i<1;i++)
{
str[0]=dr["firstname"].ToString() ;

}


}
conn.Close();
TextBox1.Text =j.ToString() ;
j++;



return str[0];

}

public void Simplest()
{
TextBox1.Text=strglobal;

for(;;)
{
//string i="yy";
strglobal=GetCustomers();


Thread.Sleep(10000) ;
}

//Console.WriteLine( "Simplest worker - done" ) ;


}





}
}


Please email me if u anybody have the solution i will be very
thankful.

Email:[email protected]
 
A

Ajay Kalra

The following statement is going to give problems:
TextBox1.Text=strglobal;

TextBox1 is created on a different thread than where it is being
assigned. You need to use a delegate and InvokeRequired to do this. See
MSDN for articles by Chris Sells (3 articles) which show how to use
thread in WinForms. Also there is a sample in codeguru. I have listed
all the related links in a post yesterday.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Basically you cannot do it, a web app is a completely different thing that
a win app, once the thread that is handling the request ( the "main"
thread ) is finish all the info was sent to the client and the instance of
the page ( with its controls ) is ready to be GC , in a web app you can
spawn a thread for JUST background processing, to keep doing something after
you sent back the client page.

cheers,
 
L

LP

Before delving into threading, read more about underlying technologies of
ASP.NET; specifically HTTP, web server vs. clients browser. It will help you
understand why you can't apply some thing you learned about Windows
programming in a web environment. And why the example you provided is
redicuolos in a web application.
I have seen some "successful" implementation of threading in web
applications, usually when there was a long running task such as long
running sql queries or mass emailing (not spamming). Usually these processes
would take much longer than acceptable in typical web application. A client
browser would make periodic calls to a server checking on the process
progress then updating some html object that resembled a progress bar. These
solutions weren't pretty, programmers have to jump through hoops with clever
mix of client and server side script, often these solutions had unexpected
side effects.
In the end it was decided it's better to optimize SQL queries or have some
kind of batch process that pre-calculates or pre-aggregates data. IMHO use
threading in a web application as the last resort when all other possible
options have been explored.



Yatharth said:
Hi,

I m new to threading and i have successfully runed threading but i
could display value on my web page ,but its working in code behind
when i see it through debugger,plzzzzzzz help me here is the code
below:

i just wana display the simple array value stored in my array variable
in my textbox thats it.

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 MultithreadingApplication.Class ;
using System.Threading;
using System.Data.SqlClient;



namespace MultithreadingApplication
{
public class WebForm2 : System.Web.UI.Page
{
string strglobal="";
Object objCust=new Object() ;
int j=0;


protected System.Web.UI.WebControls.Label Label1;


private void Page_Load(object sender, System.EventArgs e)
{

ThreadStart simplest = new ThreadStart(Simplest);
Thread thread1 = new Thread( simplest ) ;

thread1.Start() ;


Label1.Text=strglobal;


}

public string GetCustomers()//string city)
{
string []str=new string[2] ;
string strsql="select firstname,lastname from employees";
SqlConnection conn =new SqlConnection();
DataSet CustDS =new DataSet();
conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings
["ConnectionString"];
SqlCommand commnd=new SqlCommand(strsql,conn);
SqlDataReader dr ;
conn.Open();
dr=commnd.ExecuteReader();
if(dr.Read())
{


for(int i=0;i<1;i++)
{
str[0]=dr["firstname"].ToString() ;

}


}
conn.Close();
TextBox1.Text =j.ToString() ;
j++;



return str[0];

}

public void Simplest()
{
TextBox1.Text=strglobal;

for(;;)
{
//string i="yy";
strglobal=GetCustomers();


Thread.Sleep(10000) ;
}

//Console.WriteLine( "Simplest worker - done" ) ;


}





}
}


Please email me if u anybody have the solution i will be very
thankful.

Email:[email protected]
 
Y

yatharth

Thnx evrybody ,but is there anyway we can do that or if u could create
a demo project and tell me ,since i have to create a thread on a text
box ,suppose data displayed in a textbox from database and if i change
the data from database ,it changes in the text box without page refresh
,or if there is any other way plz suggest me .....
 
Y

yatharth

Thnx ,but is there anyway we can do that or if u could create a demo
project and tell me ,since i have to create a thread on a text box
,suppose data displayed in a textbox from database and if i change the
data from database ,it changes in the text box ,or if there is any
other way plz suggest me .....
 
Y

yatharth

Thnx ,can we display a message in a textbox coming from database and if
suppose i change the data in database ,the data in the textbox changes
without page refresh.
Plz email me at (e-mail address removed).
 

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