| View previous topic :: View next topic |
| Author |
Message |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Sun Jun 20, 2010 6:03 pm Post subject: Help with C# Windows Form App |
|
|
Hi,
I have a Windows Form Application created in Visual Studio 2008.
I need to figure out how to create a main loop while this GUI is showing.
For example, I have a chat client server, and when I press my "Start Server" button, I want a loop to run for as long as the server is running. This loop would then handle all packets and such.
I have heard of timers, but I wanted to know if there is another way, because I don't know how long these actions will take to complete, and I want the program to run as fast as possible, without commands backing up or anything.
Thanks! |
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Sun Jun 20, 2010 6:25 pm Post subject: |
|
|
| create a new thread for the packets handling. |
|
| Back to top |
|
 |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Sun Jun 20, 2010 6:28 pm Post subject: |
|
|
Could you provide an example? I'm looking at some tutorials and I'm not sure I understand. I do however, know what threads are, as well as what multi-threading is, so no need to explain that.
Where should I create the thread? In the .cs file that creates my forms?
How do I create it?
P.S. I'm pretty new to c#. I'm trying the best I can to learn, but reading documentation isn't enough at the moment.  |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Jun 21, 2010 3:47 pm Post subject: |
|
|
Since you even stated you are new to the language, you might want to slow down and actually learn the basics before jumping into threading and networking. There are tons of examples on the net for threading in C# as well, Google is your friend. _________________
- Retired. |
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Mon Jun 21, 2010 9:09 pm Post subject: |
|
|
| Wiccaan wrote: | | Since you even stated you are new to the language, you might want to slow down and actually learn the basics before jumping into threading and networking. There are tons of examples on the net for threading in C# as well, Google is your friend. |
I agree. I assume the loops are needed to receive packets and such, and if this is the case I would recommend Asynchronous Sockets when needed or helpful. |
|
| Back to top |
|
 |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Wed Jun 23, 2010 12:30 pm Post subject: |
|
|
Solved:
| Code: |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Server
{
public partial class Server : Form
{
bool running = false;
public Server()
{
InitializeComponent();
}
private void hStart_Click(object sender, EventArgs e)
{
running = true;
ThreadStart MainLoop = new ThreadStart(MainThread);
Thread Thread = new Thread(MainLoop);
Thread.IsBackground = true;
Thread.Start();
}
private void hStop_Click(object sender, EventArgs e)
{
running = false;
}
public void MainThread()
{
while (running == true)
{
Thread.Sleep(100);
}
}
}
}
|
And I figured I could handle it because I'm not new to programming, just threads. I have every single bit of TCP done. Just needed to know the structure to put it in a thread.  |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Wed Jun 23, 2010 1:20 pm Post subject: |
|
|
| darkjohn20 wrote: | And I figured I could handle it because I'm not new to programming, just threads. I have every single bit of TCP done. Just needed to know the structure to put it in a thread.  |
No. Go write the following on the board 20 times:
I will not use blocking sockets in a server application. |
|
| Back to top |
|
 |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Wed Jun 23, 2010 1:24 pm Post subject: |
|
|
| Does an internally-threaded tcp/udp dll count? |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jun 24, 2010 5:28 pm Post subject: |
|
|
| darkjohn20 wrote: | | Does an internally-threaded tcp/udp dll count? |
You do not want a new thread for every connection. Threads are not cheap. What you want to use for a server is asynchronous sockets (or I/O Completion Ports for very heavy loads). |
|
| Back to top |
|
 |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Thu Jun 24, 2010 7:02 pm Post subject: |
|
|
| I do not believe it is a new thread for every connection, and I agree. I know enough about threads to know you only want a few. |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jun 24, 2010 8:02 pm Post subject: |
|
|
| darkjohn20 wrote: | | I do not believe it is a new thread for every connection, and I agree. I know enough about threads to know you only want a few. |
Post your code. Your sample above strongly suggests you are creating a thread for every connection. |
|
| Back to top |
|
 |
KryziK Expert Cheater
Reputation: 3
Joined: 16 Aug 2009 Posts: 199
|
Posted: Thu Jun 24, 2010 8:10 pm Post subject: |
|
|
| Code: | using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Server
{
public partial class test : Form
{
bool running = false;
public test()
{
InitializeComponent();
}
private void hStartServer_Click(object sender, EventArgs e)
{
int Instances = 1;
int Threads = 0;
ushort Local_Port = Convert.ToUInt16(hPort.Text);
string Local_IP = hIPAddress.Text.ToString();
Global.Max_Clients = Convert.ToInt32(hMaxConnections.Text);
Global.SendPacket = mn.CreatePacket();
Global.RecvPacket = mn.CreatePacket();
mn.SetMemorySize(Global.SendPacket, 1024);
mn.Start(Instances, Threads);
mn.SetLocal(0, Local_IP, Local_Port, Local_IP, Local_Port);
mn.DisableUDP(0);
int Result = mn.StartServer(0, Global.Max_Clients, 0, 0);
if (Result == 0)
{
hStatus.Text = "Online";
hStartServer.Enabled = false;
hStopServer.Enabled = true;
hIPAddress.ReadOnly = true;
hPort.ReadOnly = true;
hMaxConnections.ReadOnly = true;
hStopServer.Focus();
ThreadStart MainLoop = new ThreadStart(MainThread);
Thread Thread = new Thread(MainLoop);
Thread.IsBackground = true;
Thread.Start();
running = true;
}
else
{
mn.Finish(-1);
}
}
private void hStopServer_Click(object sender, EventArgs e)
{
running = false;
hStatus.Text = "Offline";
hStartServer.Enabled = true;
hStopServer.Enabled = false;
hIPAddress.ReadOnly = false;
hPort.ReadOnly = false;
hMaxConnections.ReadOnly = false;
hStartServer.Focus();
}
public void MainThread()
{
while (running == true)
{
//Check for joining or leaving clients, and new packets
Thread.Sleep(100);
}
mn.Finish(-1);
}
public void _TCPSendString(int Instance, int ClientID, string Data)
{
mn.AddString(Global.SendPacket, Data, 0, true);
mn.SendTCP(Instance, Global.SendPacket, ClientID, false, true);
}
public void _TCPSendInt(int Instance, int ClientID, int Data)
{
mn.AddInt(Global.SendPacket, Data);
mn.SendTCP(Instance, Global.SendPacket, ClientID, false, true);
}
}
} |
Does
| Code: | ThreadStart MainLoop = new ThreadStart(MainThread);
Thread Thread = new Thread(MainLoop);
Thread.IsBackground = true;
Thread.Start(); |
need to be moved so it isn't created every time I press "Start Server"?
Edit: Using library found at: DarkNet |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Jun 25, 2010 12:07 am Post subject: |
|
|
| Don't use that library, at first glance it appears to be complete shit. Do it yourself with .NET asynchronous sockets. |
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Fri Jun 25, 2010 4:06 am Post subject: |
|
|
| Flyte wrote: | | Don't use that library, at first glance it appears to be complete shit. Do it yourself with .NET asynchronous sockets. |
Could you explain why DarkNet looks like shit? I've been looking for network libraries recently and chose ACE, but since I'm not an expert on this, I can't tell why this 'DarkNet' would be bad. |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Jun 25, 2010 12:06 pm Post subject: |
|
|
| tombana wrote: | | Could you explain why DarkNet looks like shit? I've been looking for network libraries recently and chose ACE, but since I'm not an expert on this, I can't tell why this 'DarkNet' would be bad. |
It seems to lack any sort of design. This is evident by the naming conventions, the use of arbitrary integer values, and the fact there is a sound library bundled with a networking library.
The vast majority of networking in .NET can be done with TcpClient and TcpListener. It isn't often you'll need any more than that. |
|
| Back to top |
|
 |
|