|
-
Feb 22nd, 2006, 03:18 PM
#1
Thread Starter
Lively Member
Connect over net
I wrote a program to act as a chess clock. I would like to give a copy to somebody else. If your familiar with a chess clock, you know that when you make a move, you stop your time... and stopping your time starts your opponents time.
I would like for the program to connect to his computer so that when he stops his time, the program on my machine will act accordingly. And when I stop my time, his will start. We can see each others time.
I'm ok with programming, but no idea when it comes to connecting over the net.
If I could learn to send maybe 1 character at at time over the internet, I could go from there.
Last edited by DanielS1324; Feb 22nd, 2006 at 04:10 PM.
-
Feb 22nd, 2006, 07:09 PM
#2
Re: Connect over net
You should take a look at the Sockets example in the 2003 101 samples from Microsoft. It uses a client/server chat application as an example of network programming but the principles would be exactly the same for what you want to do. If you're using VS 2005, the 2005 101 Samples may have an equivalent example but I haven't checked.
-
Feb 22nd, 2006, 09:32 PM
#3
Thread Starter
Lively Member
Re: Connect over net
Thanks for the reply. Apparently the 101 samples for VS 2005 dont have the chat example.
-
Feb 22nd, 2006, 09:40 PM
#4
Re: Connect over net
Open the 2003 version in 2005 and it will be upgraded. There may be a few issues that need addressing but chances are it will work without need for any changes.
-
Feb 23rd, 2006, 12:11 AM
#5
Thread Starter
Lively Member
Re: Connect over net
Thanks! I got the connection part done. But now there's another problem... I have the connection stuff in a seperate thread and when I try to set a label's text from that thread, it throws an exception.
Something about accessing the control from a thread other than the one it was created on.
I tried the examples for accessing the label from seperate threads, but none of them work.
So... How can I update the text of a label from a seperate thread?
Thanks!
-
Feb 23rd, 2006, 12:18 AM
#6
Re: Connect over net
To access members of a control from a different thread you need to use a delegate to marshal the call to the UI thread. Basically you create a delegate, tell it what method you want it to execute, then send it off to the UI thread to execute that method where it can safely access control members. Search the forum for "delegate" with my user name and you'll find a couple of examples, by me and others.
-
Feb 23rd, 2006, 12:30 AM
#7
Re: Connect over net
Actually, I realised that all the examples I've given are in VB so here's a C# example. You would need to define a method to access the members of the Label, then declare a matching delegate:
Code:
// This delegate will be used to invoke the SetLabelText method so it must have the same signature.
private delegate void SetLabelTextDelegate(string text);
// This method sets the text of the label.
private void SetLabelText(string text)
{
this.label1.Text = text;
}
When you wanted to set the Label's Text you would use code like this to test whether an invocation was required and, if so, perform one:
Code:
if (this.label1.InvokeRequired)
{
// We are not in the UI thread so invoke a method on the UI thread to set the label text.
this.label1.Invoke(new SetLabelTextDelegate(SetLabelText), new string[] { "Some text" });
}
else
{
// We are in the UI thread so it is safe to access the label directly.
this.label1.Text = "Some text";
}
-
Feb 23rd, 2006, 10:04 AM
#8
Thread Starter
Lively Member
Re: Connect over net
Thanks a million! That should be excatly what I need.
The "if (invokeRequired...)" where does that code go?
Last edited by DanielS1324; Feb 23rd, 2006 at 10:11 AM.
-
Feb 23rd, 2006, 04:55 PM
#9
Re: Connect over net
 Originally Posted by DanielS1324
The "if (invokeRequired...)" where does that code go?
Wherever you were previously accessing the Label directly. The code you are currently using would now go in the 'else' block and the cross-thread equivalent in the 'if' block.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|