ASp.Net and Threading
-
Hi, I am trying to use thread in my ASP.Net application and facing some problem. I want to monitor a Application[“variable”] variable, for this I am using following code in thread start method. private void monitor() { /*compare the application variable with monitorStartGame*/ while(monitorStartGame.Equals(Convert.ToInt32(Application["startGame"]))) { } Label1.Text="thread ended"; } but it does not stop even the values is changed, and the text of Label1 is not changed. Plz help me I will be grateful to u.
-
Hi, I am trying to use thread in my ASP.Net application and facing some problem. I want to monitor a Application[“variable”] variable, for this I am using following code in thread start method. private void monitor() { /*compare the application variable with monitorStartGame*/ while(monitorStartGame.Equals(Convert.ToInt32(Application["startGame"]))) { } Label1.Text="thread ended"; } but it does not stop even the values is changed, and the text of Label1 is not changed. Plz help me I will be grateful to u.
Well there seem to a few problems with your code 1. if you don't wanna kill the server completely u'll need a Thread.Sleep() statement in the while loop. 2. Remember when u load the page u are initialiseing all controls everytime so Label1 will reset to u'r default value so u will have to have an Application or Session variable to keep the new value and set Label1.Text to that value in pageload. Wyx :-D
-
Well there seem to a few problems with your code 1. if you don't wanna kill the server completely u'll need a Thread.Sleep() statement in the while loop. 2. Remember when u load the page u are initialiseing all controls everytime so Label1 will reset to u'r default value so u will have to have an Application or Session variable to keep the new value and set Label1.Text to that value in pageload. Wyx :-D
thankx for u r reply;i agree with the 1st point, but in 2nd there is some confusion. i run the thread after a long time of page load and one more thing the following code also not show response in the threead while loop >> Response.Write("asdasd"); please explane the Lable1.Text problem, i will be very thankfull to u.
-
thankx for u r reply;i agree with the 1st point, but in 2nd there is some confusion. i run the thread after a long time of page load and one more thing the following code also not show response in the threead while loop >> Response.Write("asdasd"); please explane the Lable1.Text problem, i will be very thankfull to u.
-
thankx for u r reply;i agree with the 1st point, but in 2nd there is some confusion. i run the thread after a long time of page load and one more thing the following code also not show response in the threead while loop >> Response.Write("asdasd"); please explane the Lable1.Text problem, i will be very thankfull to u.
Well here's point even if ur' running a thread the server can not call the client with the response.write u see Web is a Request Reply protocol and the only way for u to get the value that is set by the thread is to Request the page and get the value set by the thread. Did u try to set a fx Application["myValue"] in the thread to fx "ThreadSet" and in pageload set the value af Label1.Text to Session["myValue"] I understand what u'r trying to do but that is not possible in Web u'll have to resolve to an TCP/IP solution so to explain it kiss if you are running thread in a web application any value set in the thread has to be keept in a typ of variable on the server that maintain its value between request every time u make a request you will have to set the control value to that value. BR Wyx
-
Well here's point even if ur' running a thread the server can not call the client with the response.write u see Web is a Request Reply protocol and the only way for u to get the value that is set by the thread is to Request the page and get the value set by the thread. Did u try to set a fx Application["myValue"] in the thread to fx "ThreadSet" and in pageload set the value af Label1.Text to Session["myValue"] I understand what u'r trying to do but that is not possible in Web u'll have to resolve to an TCP/IP solution so to explain it kiss if you are running thread in a web application any value set in the thread has to be keept in a typ of variable on the server that maintain its value between request every time u make a request you will have to set the control value to that value. BR Wyx
Hi, please check it out this code; I am trying to monitor an Application[“startGame”] vaiable for this I wrote a class that lie in WebForm1 : System.Web.UI.Page class private void Page_Load(object sender, System.EventArgs e) { Label1.Text=Session["threadCheck"].ToString(); monitorStartGame=Convert.ToInt32(Application["startGame"]); } class myThread { public WebForm1 web; public object obj; public System.EventArgs ex; public void monitor() { while(monitorStartGame != Convert.ToInt32(web.Application["startGame"])) { Thread.Sleep(TimeSpan.FromSeconds(2)); } web.Session["threadCheck"]=100000; web.Page_Load(obj,ex); } } now when I click the button to start monitoring the following code execute static Thread thread; private void Button12_Click(object sender, System.EventArgs e) { myThread th = new myThread(); th.obj = sender; th.ex=e; th.web=this; thread = new Thread(new ThreadStart(th.monitor)); thread.Start(); } >> Now I open another explorer (using same address) and click another button to change the Application variable private void Button13_Click(object sender, System.EventArgs e) { Application["startGame"]=1000; monitorStartGame=12; } but nothing happen on my previous explorer page (even the vale of Application variable is changed now) and if I change the vale through one explorer it works. I tried to explain my problem I think u can trace out the solution, please give me a solution to this problem. thanx
-
Hi, please check it out this code; I am trying to monitor an Application[“startGame”] vaiable for this I wrote a class that lie in WebForm1 : System.Web.UI.Page class private void Page_Load(object sender, System.EventArgs e) { Label1.Text=Session["threadCheck"].ToString(); monitorStartGame=Convert.ToInt32(Application["startGame"]); } class myThread { public WebForm1 web; public object obj; public System.EventArgs ex; public void monitor() { while(monitorStartGame != Convert.ToInt32(web.Application["startGame"])) { Thread.Sleep(TimeSpan.FromSeconds(2)); } web.Session["threadCheck"]=100000; web.Page_Load(obj,ex); } } now when I click the button to start monitoring the following code execute static Thread thread; private void Button12_Click(object sender, System.EventArgs e) { myThread th = new myThread(); th.obj = sender; th.ex=e; th.web=this; thread = new Thread(new ThreadStart(th.monitor)); thread.Start(); } >> Now I open another explorer (using same address) and click another button to change the Application variable private void Button13_Click(object sender, System.EventArgs e) { Application["startGame"]=1000; monitorStartGame=12; } but nothing happen on my previous explorer page (even the vale of Application variable is changed now) and if I change the vale through one explorer it works. I tried to explain my problem I think u can trace out the solution, please give me a solution to this problem. thanx