share resource b/t child thread & parent thead

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mai Phuong
    New Member
    • Sep 2009
    • 25

    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

    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
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • Mai Phuong
      New Member
      • Sep 2009
      • 25

      #3
      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 ^^

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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

        Comment

        • Mai Phuong
          New Member
          • Sep 2009
          • 25

          #5
          I have used updatePanel & timer of ajax. Then I focus on this sentence of you:
          Originally posted by Frinavale
          ...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:

          Code:
          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:

          Code:
          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 ...
          Last edited by Frinavale; Sep 24 '09, 01:07 PM. Reason: Please post code in [code] ... [/code] tags. Changed quote tags into code tags.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            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 UserWhoPostedTh eMessage 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

            Comment

            • Mai Phuong
              New Member
              • Sep 2009
              • 25

              #7
              Thank you so much!

              I have solved my problem with Sqldependency. It seems so good!

              Comment

              Working...