hi all,
I have met a problem with asp.net. Textbox can't be updated. "Main Thread" runs on Page_load. It calls child thread, which changes the text of textbox, recursively.
Here is my code -
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
serverSocket = new Socket(AddressFamily.InterNetwork,
-
SocketType.Dgram, ProtocolType.Udp);
-
-
//Assign the any IP of the machine and listen on port number 1000
-
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1002);
-
-
//Bind this address to the server
-
serverSocket.Bind(ipEndPoint);
-
-
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
-
//The epSender identifies the incoming clients
-
EndPoint epSender = (EndPoint)ipeSender;
-
-
//Start receiving data
-
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
-
SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
-
-
ONRECEIVE IS CALLED. THIS STARTS WITH CHILD THREAD.
-
}
-
-
private void OnReceive(IAsyncResult ar)
-
{
-
-
//SOME CODE HERE....
-
-
txtLog.Text += msgToSend.strMessage + "\r\n";
-
-
//UPDATE TEXT OF TEXT BOX
-
-
-
//Start listening to the message send by the user
-
serverSocket.BeginReceiveFrom (byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
-
new AsyncCallback(OnReceive), epSender);
-
// ONCE AGAIN CALL ONRECEIVE & GO ON
-
-
}
-
Thanks much much for all of yours help
You could use Silverlight to do this. Then you could use your sockets to connect to the server and wait for the server "push" to them.
You can't use sockets the way you want to in an asp.net application.
If you don't want to use Silverlight, and you want to do this using ASP.NET, then you are going to have to "pull" (poll) from the server.
Like I said, ASP.NET applications are stateless. Since there's no connection maintained between the client (web browser) and the server, the client can't (easily and safely) be waiting for data to come from the server. This means that the client has to ask for the information.
The smoothest way to do this is to use Ajax. The web browser will wait for "X" amount of time before using Ajax to make an asynchronous request to the server for any new data.
The server will have to figure out if there is any new data by checking something (maybe a database??) to see if there are any new messages for the current chat. Since this is done using Ajax, only a portion of the page will be updated with the new information (instead of the whole page being sent to the server, which is what happens during normal requests).
-Frinny
6 2430
Wait a second.
You've created an aspx page that connects to a socket server to retrieve "something" (text I'm assuming because you're displaying it in a TextBox).
Why?
There's 2 things that you have to know here. The first (more obvious point) is that you're working in a stateless environment. In other words, there is no connection maintained between the web browser and the server. So, if your TextBox's text changes on server it wont be seen in the web browser until the request finishes.
You're using asynchronous sockets which connect to the server and you have a continuous, recursive loop used to receive data from the socket server in your PageLoad event.
So, how do you expect the request to return to the browser with the updated text if the page never gets out of the PageLoad event?
The second thing I'd like to point out is that you cannot access resources that are outside of the thread that your asynchronous socket is using. You can get around this using Dispatch...but I don't see a point to doing this because your request is never going to return to the browser anyways so ...??
What are you trying to do?
-Frinny
hey,
All I want to do is sth like webchat. In fact, I can describe it as following:
I have 2 pages: Monitor page & User page.
_ Users may have some actions such as: login / logout / action1 / action2 ... _ From Monitor page, we can see all statuses of all users who have logined.
Then I think about UDP protocol.
That 's all I want to do. Have you got any suggestion?
Your idea above is very valuable to me ^^
You could use Silverlight to do this. Then you could use your sockets to connect to the server and wait for the server "push" to them.
You can't use sockets the way you want to in an asp.net application.
If you don't want to use Silverlight, and you want to do this using ASP.NET, then you are going to have to "pull" (poll) from the server.
Like I said, ASP.NET applications are stateless. Since there's no connection maintained between the client (web browser) and the server, the client can't (easily and safely) be waiting for data to come from the server. This means that the client has to ask for the information.
The smoothest way to do this is to use Ajax. The web browser will wait for "X" amount of time before using Ajax to make an asynchronous request to the server for any new data.
The server will have to figure out if there is any new data by checking something (maybe a database??) to see if there are any new messages for the current chat. Since this is done using Ajax, only a portion of the page will be updated with the new information (instead of the whole page being sent to the server, which is what happens during normal requests).
-Frinny
I have used updatePanel & timer of ajax. Then I focus on this sentence of you:
...The server will have to figure out if there is any new data by checking something (maybe a database??) .....
Something I intend to check here is a global static variable: - public static string abc =
Which holds the data coming from Client Page. This is effective with the first data pagkage. That means, txtbox of Monitor Page has been updated to inform the content of the first pagkage. But Error has happened while waiting for the second pakage. This leads to the following lines on the debug screen automatically: - finally {
-
if (_this._xmlHttpRequest != null) {
-
_this._xmlHttpRequest.onreadystatechange = Function.emptyMethod;
-
_this._xmlHttpRequest = null;
-
}
-
What's wrong?
May be I have a question of a baby in coding. I haven't stored coming data in database b/c my system following SOA. Using service to connect to database frequently after interval of X s may make the system slow. So I try to do this with static global variable. But I feel, problem has risen here ...
I don't know what is wrong because you haven't told me what the error message is........
Just so you know, an application implemented using SOA (Service-Oriented Architecture) can use a database...I'm pretty sure that most of them do.
It's fine not to use a database but it would probably be a good idea not to just store the message in a global string.
If I weren't using a DataBase I'd create a "Message" class and store a List Of Messages instead using a String...I'd clean this list from time to time to keep it from getting too big.
The Message class would have a DateTime property along with the UserWhoPostedTheMessage property. That way when making the Ajax request to refresh the chat window, you could grab any messages that haven't yet been sent to the ChatUser.
If you really want to do SOA, why don't you consider using Web Services to implement the chat? Use Ajax to request/send information from the Web Service...
-Frinny
Thank you so much!
I have solved my problem with Sqldependency. It seems so good!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: jinu |
last post by:
Hello,
My application loads with a form which is the mdi parent and a menu item click (main menu of the parent form) brings up a child form.
I have to wait for a considerable amout of time to...
|
by: Dayne |
last post by:
Can a Parent thread catch a event send by a child thread?
Dayne
|
by: maricel |
last post by:
Is there a way to list (using db2 command or catalogs) to list hierarchy of
table parent & child relationship:
1) A list that shows which table should be deleted first,second,third...
2) A list...
|
by: John |
last post by:
Hi,
I have a base class that's compiled in an assembly.
public class mybase
{
public void mymethod()
{
//can i get the embeded resources?
//Stream st =...
|
by: Dinesh Jain |
last post by:
Hi all,
How to inform Parent thread from a Child Thread?
I mean want to notify something from Child thread to
Parent Thread.
Please help,
Thanks in advance,
-Regards,
Dinesh
|
by: Chukkalove |
last post by:
I originally created several "copy" resource reader classes which worked
great individually, but after I converted them into an parent class with
children they no longer find the resources. Each...
|
by: sunil |
last post by:
Hi All,
I am tring to debug the following program.I written this
program in HP-Unix and I am using the GDB as a debugger.
#include <stdio.h>
#include<unistd.h>
int main()
{
int pid;
pid =...
|
by: Sanders Kaufman |
last post by:
I'm wondering if I'm doing this right, as far as using another class
object as a PHP class property.
class my_baseclass {
var $Database;
var $ErrorMessage;
var $TableName;
var $RecordSet;...
|
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= |
last post by:
vs2005, c#
Trying to understand why one way works but the other doesnt.
I have not tried to create a simpler mdi/child/showdialog app for posting
purposes (I think even this would not be so small...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
| |