Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, the problem is the <code>while</code> loop in client method <code>Connect</code>. Remove it because it loops infinitely raising CPU usage to 100% and it's useless.</p> <p>BTW, you have another problems in your code:</p> <ul> <li><strong>CrossThread operation exception</strong></li> </ul> <p>For example in your <code>Client.Recieve</code> method you do:<br> <code>label1.Text = Encoding.ASCII.GetString(g_bmsg, 0, g_bmsg.Length);</code><br> Actually, you're trying to set the label text from another thread (the one listening for received msgs) and this is not allowed;<br> Do something like this:</p> <p>Create a Setter method for the label text:</p> <pre><code>private void SetLabelText(string txt) { if (label1.InvokeRequired) label1.Invoke(new MethodInvoker(delegate { SetLabelText1(txt); })); else label1.Text = txt; } </code></pre> <p>then use the setter instead of directly call <code>label1.Text = ...</code>:</p> <pre><code>SetLabelText(Encoding.ASCII.GetString(g_bmsg, 0, g_bmsg.Length)); </code></pre> <hr> <p><strong>EDIT to answer to OP comment:</strong></p> <p>For a good and extensive explanation of what is a thread, look at its <a href="http://en.wikipedia.org/wiki/Thread_%28computer_science%29" rel="nofollow">wikipedia page</a>.</p> <p>Anyway, in simple words, a running process contains one or more threads and these are part of code that can be executed concurrently.</p> <p>Starting from your TCP example, using <code>Socket.Receive</code> instead of <code>Socket.BeginReceive</code> you would have blocked the execution on <code>Socket.Receive()</code> call (I mean the lines of code after the one containing <code>Receive</code> method wouldn't be reached) until something is received.</p> <p>This because <code>Socket.Receive</code> method runs on the same thread of the following code, and on each thread, the code is executed sequentially (i.e. line by line).</p> <p>Conversely, using <code>Socket.BeginReceive</code>, behind the scene a new thread is created. This thread likely calls and stops on <code>Socket.Receive</code> method, and once received something it calls the method passed as parameter.</p> <p>This makes <code>Socket.BeginReceive</code> asynchronous, while <code>Socket.Receive</code> is synchronous, and this is why I knew ther was another thread (when you hear asynchronous word, is extremely probable that you are dealing with multi-threading)</p> <p>Thus, when you change <code>label.Text</code> you are actually setting it from another thread: the one created by <code>Socket.BeginReceive</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload