Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need help on chat between two computers

Need help on chat between two computers

Scheduled Pinned Locked Moved C#
helpcomtutorialquestionlounge
17 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard DeemingR Richard Deeming

    larsp777 wrote:

    listMessage.Items.Add("Friend: " + receivedMessage);

    I'm guessing that's the line that's causing the problem. The callback method will be called on a background thread; you will need to use InvokeRequired and Invoke to get back to the UI thread when you want to access the form's controls.

    Invoke((Func<object, int>)listMessage.Items.Add, "Friend: " + receivedMessage);


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    L Offline
    L Offline
    larsp777
    wrote on last edited by
    #6

    Thank you both! I think that is the problem. Will check it out and get back.

    1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      Where do you get the error? Which line?

      Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

      L Offline
      L Offline
      larsp777
      wrote on last edited by
      #7

      Ok... The other guy whose thread I can´t se now wrote: I'm guessing that's the line that's causing the problem. The callback method will be called on a background thread; you will need to use InvokeRequired and Invoke to get back to the UI thread when you want to access the form's controls. Invoke((Action<object>)listMessage.Items.Add, "Friend: " + receivedMessage); I Think he was right about the line. Tried to do change the line accordingly but got an error:

      int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type

      Richard DeemingR 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Where do you get the error? Which line?

        Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

        L Offline
        L Offline
        larsp777
        wrote on last edited by
        #8

        The error occurs when I try to send a message. It occurs on both computers. When I send from one computer the error shows on the other.

        1 Reply Last reply
        0
        • L larsp777

          Ok... The other guy whose thread I can´t se now wrote: I'm guessing that's the line that's causing the problem. The callback method will be called on a background thread; you will need to use InvokeRequired and Invoke to get back to the UI thread when you want to access the form's controls. Invoke((Action<object>)listMessage.Items.Add, "Friend: " + receivedMessage); I Think he was right about the line. Tried to do change the line accordingly but got an error:

          int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #9

          larsp777 wrote:

          int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type

          Whoops - didn't notice that the method returned an int. The delegate type will need to be a Func<object, int> instead of an Action<object>.

          Invoke((Func<object, int>)listMessage.Items.Add, "Friend: " + receivedMessage);


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          OriginalGriffO L 3 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            larsp777 wrote:

            int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type

            Whoops - didn't notice that the method returned an int. The delegate type will need to be a Func<object, int> instead of an Action<object>.

            Invoke((Func<object, int>)listMessage.Items.Add, "Friend: " + receivedMessage);


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #10

            :thumbsup:

            Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              larsp777 wrote:

              int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type

              Whoops - didn't notice that the method returned an int. The delegate type will need to be a Func<object, int> instead of an Action<object>.

              Invoke((Func<object, int>)listMessage.Items.Add, "Friend: " + receivedMessage);


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              L Offline
              L Offline
              larsp777
              wrote on last edited by
              #11

              Thanks, Richard, it worked! Well, thank you both for helping me out!

              1 Reply Last reply
              0
              • L larsp777

                I have used this tutorial to make a chat: http://www.youtube.com/watch?v=BDVfpPq3weo[^] It works on the same computer but not between two. The problem seem so be that I need more than one thread. I get Cross-thread operation not valid.

                L Offline
                L Offline
                larsp777
                wrote on last edited by
                #12

                Can you mark a question as solved. You really solved it Richard but Griffin also helped. I am working on a game for XNA and need both computers to have access to the same "board". Do you know anything about that?

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  No, the problem is the reverse of that: "Cross-thread operation not valid" means that you are executing code on one thread that can only be executed on a different thread: normally, this occurs when you try to update a control from a different thread to that from which it was created (which must be the UI thread), either in a BackgoundWorker, a Thread instance or in the handler of a communications class that uses threading to handle it's events (the SerialPort does this for example). Check your code: you may just have to start invoking the control instead of accessing it directly. For example:

                      private void AddNewTab(string tabName)
                          {
                          if (InvokeRequired)
                              {
                              Invoke(new MethodInvoker(delegate { AddNewTab(tabName); }));
                              }
                          else
                              {
                              TabPage tp = new TabPage(tabName);
                              myTabControl.TabPages.Add(tp);
                              }
                          }
                  

                  Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                  R Offline
                  R Offline
                  Rahul VB
                  wrote on last edited by
                  #13

                  hey OG, please enlighten me more on this.

                  OriginalGriffO 1 Reply Last reply
                  0
                  • R Rahul VB

                    hey OG, please enlighten me more on this.

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #14

                    Sorry Rahul, but it's a huge subject - I couldn't begin to do it justice in a small text box! :laugh: There are some good tutorials out there which explain threading and the UI pretty well: http://stuff.seans.com/2009/05/21/net-basics-do-work-in-background-thread-to-keep-gui-responsive/[^] (Backgound and why to thread) http://www.dreamincode.net/forums/topic/246911-c%23-multi-threading-in-a-gui-environment/[^] (Fairly advanced) But basically when you start to use multiple threads you can't touch any controls, except from the thread that created them - which is called the UI thread (for User Interface) and is the original thread the form started on. If you try, you will get a "cross-threading" error telling you not to do that. The only way to get round it is to Invoke the control - which basically requests the UI thread to do the work for you. Have a look at the BackgroundWorker thread - it provides a way to update the display without invoking via the ProgressChanged event, which is executed on the original thread.

                    Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      larsp777 wrote:

                      int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type

                      Whoops - didn't notice that the method returned an int. The delegate type will need to be a Func<object, int> instead of an Action<object>.

                      Invoke((Func<object, int>)listMessage.Items.Add, "Friend: " + receivedMessage);


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      L Offline
                      L Offline
                      larsp777
                      wrote on last edited by
                      #15

                      Do you know if there is a simple way for a third person can listen in what the others are Writing. (only has to listen to the sender) I am making an application where you should be able to simulate someone eavesdropping the conversation.

                      Richard DeemingR 1 Reply Last reply
                      0
                      • L larsp777

                        Do you know if there is a simple way for a third person can listen in what the others are Writing. (only has to listen to the sender) I am making an application where you should be able to simulate someone eavesdropping the conversation.

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #16

                        If all the computers are on the same LAN, you could use the broadcast address so that any computer can pick up the messages: Broadcasting Using Socket-Oriented Approach[^] If they're on different networks, or you don't want the overhead associated with broadcasting, then you'll need to use multicasting: IP Multicasting in C#[^]


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        L 1 Reply Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          If all the computers are on the same LAN, you could use the broadcast address so that any computer can pick up the messages: Broadcasting Using Socket-Oriented Approach[^] If they're on different networks, or you don't want the overhead associated with broadcasting, then you'll need to use multicasting: IP Multicasting in C#[^]


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          L Offline
                          L Offline
                          larsp777
                          wrote on last edited by
                          #17

                          Thanks! It will probebly be on the same LAN.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups