473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Threading in C#.Net Web application

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.Configu rationSettings.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:Ya************@netlinkis.com
Nov 17 '05 #1
8 13464
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.

-------
Ajay Kalra
aj*******@yahoo.com

Nov 17 '05 #2
Hi,

This is true in WIN app, not in web app.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ajay Kalra" <aj*******@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.

-------
Ajay Kalra
aj*******@yahoo.com

Nov 17 '05 #3
My apologies. I have no idea what a web app is.

----------
Ajay Kalra
aj*******@yahoo.com

Nov 17 '05 #4
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Yatharth" <ya******@gmail.com> wrote in message
news:ae**************************@posting.google.c om...
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.Configu rationSettings.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:Ya************@netlinkis.com

Nov 17 '05 #5
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" <ya******@gmail.com> wrote in message
news:ae**************************@posting.google.c om...
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.Configu rationSettings.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:Ya************@netlinkis.com

Nov 17 '05 #6
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 .....

Nov 17 '05 #7
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 .....

Nov 17 '05 #8
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 ya************@netlinkis.com.

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
3
by: Avi Kadosh | last post by:
hi all I am new to Csharp I tried the following code using System; using System.Threading;
0
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks...
2
by: hecklar | last post by:
This is my first time posting here, so i apologize if i'm posting in the wrong subgroup or whatever, but here goes... I’m having a problem with threading and events (permissions?) in a VB.net...
9
by: AdrianJMartin | last post by:
Hi all, I have a need for a STA thread from asp.net. I can create the thread and it runs fine. But when it is finished, the thread still 'hangs' arround. Visible only to the debugger..... I get...
3
by: Aleksandar Cikota | last post by:
Hi all, I have a problem with threading. The following part should be running in a main programm all the time, but so that the main programm also works (like 2 seperate programms, but in one)...
6
by: hzgt9b | last post by:
Using VS 2003, .NET: I developed a windows application that performs several actions based on an input file. The application displays a progress bar as each action executes. Based on new...
5
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.