Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Help with C# Windows Form App
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
KryziK
Expert Cheater
Reputation: 3

Joined: 16 Aug 2009
Posts: 199

PostPosted: Sun Jun 20, 2010 6:03 pm    Post subject: Help with C# Windows Form App Reply with quote

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
View user's profile Send private message
HolyBlah
Master Cheater
Reputation: 2

Joined: 24 Aug 2007
Posts: 446

PostPosted: Sun Jun 20, 2010 6:25 pm    Post subject: Reply with quote

create a new thread for the packets handling.
Back to top
View user's profile Send private message
KryziK
Expert Cheater
Reputation: 3

Joined: 16 Aug 2009
Posts: 199

PostPosted: Sun Jun 20, 2010 6:28 pm    Post subject: Reply with quote

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. Confused
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Mon Jun 21, 2010 3:47 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Mon Jun 21, 2010 9:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
KryziK
Expert Cheater
Reputation: 3

Joined: 16 Aug 2009
Posts: 199

PostPosted: Wed Jun 23, 2010 12:30 pm    Post subject: Reply with quote

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. Wink
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Wed Jun 23, 2010 1:20 pm    Post subject: Reply with quote

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. Wink


No. Go write the following on the board 20 times:

I will not use blocking sockets in a server application.
Back to top
View user's profile Send private message
KryziK
Expert Cheater
Reputation: 3

Joined: 16 Aug 2009
Posts: 199

PostPosted: Wed Jun 23, 2010 1:24 pm    Post subject: Reply with quote

Does an internally-threaded tcp/udp dll count?
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Jun 24, 2010 5:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
KryziK
Expert Cheater
Reputation: 3

Joined: 16 Aug 2009
Posts: 199

PostPosted: Thu Jun 24, 2010 7:02 pm    Post subject: Reply with quote

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
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Jun 24, 2010 8:02 pm    Post subject: Reply with quote

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
View user's profile Send private message
KryziK
Expert Cheater
Reputation: 3

Joined: 16 Aug 2009
Posts: 199

PostPosted: Thu Jun 24, 2010 8:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Jun 25, 2010 12:07 am    Post subject: Reply with quote

Don't use that library, at first glance it appears to be complete shit. Do it yourself with .NET asynchronous sockets.
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Fri Jun 25, 2010 4:06 am    Post subject: Reply with quote

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
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Jun 25, 2010 12:06 pm    Post subject: Reply with quote

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. Confused

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites