473,386 Members | 1,819 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,386 software developers and data experts.

share resource b/t child thread & parent thead

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

Expand|Select|Wrap|Line Numbers
  1.  
  2.     protected void Page_Load(object sender, EventArgs e)
  3.     {
  4.      serverSocket = new Socket(AddressFamily.InterNetwork,
  5.             SocketType.Dgram, ProtocolType.Udp);
  6.  
  7.         //Assign the any IP of the machine and listen on port number 1000
  8.         IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1002);
  9.  
  10.         //Bind this address to the server
  11.         serverSocket.Bind(ipEndPoint);
  12.  
  13.         IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
  14.         //The epSender identifies the incoming clients
  15.         EndPoint epSender = (EndPoint)ipeSender;
  16.  
  17.         //Start receiving data
  18.         serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
  19.             SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);  
  20.  
  21.     ONRECEIVE IS CALLED. THIS STARTS WITH CHILD THREAD.
  22.     }
  23.  
  24.     private void OnReceive(IAsyncResult ar)
  25.         {
  26.  
  27.             //SOME CODE HERE....
  28.  
  29.                       txtLog.Text += msgToSend.strMessage + "\r\n";
  30.  
  31.              //UPDATE TEXT OF TEXT BOX       
  32.  
  33.  
  34.                     //Start listening to the message send by the user
  35.                     serverSocket.BeginReceiveFrom (byteData, 0, byteData.Length, SocketFlags.None, ref epSender, 
  36.                         new AsyncCallback(OnReceive), epSender);
  37.               // ONCE AGAIN CALL ONRECEIVE & GO ON
  38.  
  39.         }
  40.  
Thanks much much for all of yours help
Sep 22 '09 #1

✓ answered by Frinavale

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
Frinavale
9,735 Expert Mod 8TB
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
Sep 22 '09 #2
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 ^^
Sep 23 '09 #3
Frinavale
9,735 Expert Mod 8TB
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
Sep 23 '09 #4
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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. finally {
  2.                 if (_this._xmlHttpRequest != null) {
  3.                     _this._xmlHttpRequest.onreadystatechange = Function.emptyMethod;
  4.                     _this._xmlHttpRequest = null;
  5.                 }
  6.  
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 ...
Sep 23 '09 #5
Frinavale
9,735 Expert Mod 8TB
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
Sep 24 '09 #6
Thank you so much!

I have solved my problem with Sqldependency. It seems so good!
Nov 9 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
5
by: Dayne | last post by:
Can a Parent thread catch a event send by a child thread? Dayne
3
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...
3
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 =...
6
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
1
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...
2
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 =...
61
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;...
2
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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:
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...
0
marktang
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,...
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.