while (true)
{
//do stuff
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine("server now exiting.");
Console.ReadLine();
my problem is .close & .stop nevers happens. is there a way to refracted this? like on exit do this? -Thanks
problem with 'while(true)'
Page 1 of 15 Replies - 542 Views - Last Post: 09 February 2012 - 08:32 PM
Topic Sponsor:
#1
problem with 'while(true)'
Posted 04 February 2012 - 12:40 AM
hello, i currently learning socket programming. it seem easy, but i see a problem... while creating a DOS server program i can start a while loop but it stays true intill i exit the program example..
Replies To: problem with 'while(true)'
#2
Re: problem with 'while(true)'
Posted 04 February 2012 - 01:42 AM
Hi
Because closing a console application from the close cross doesn't fire any regular events because it's killing the application with quite brutal methods, you'll have to do something different.
I created this small sample to mimic your problem. The way I handled exiting is to hook up to Kernel32.Dll's SetConsoleCtrlHandler which fires events when your console application closes.
In my test app I have a class called server which writes to a file. In Main() I hook to the event and in the event I call stop method of the Server which flushes the stream to the text file. If the stop isn't called, it won't flush to the file and the file remains empty.
I replaced the while(true) loop with while(isRunning) and in Stop() I set isRunning to false.
Because closing a console application from the close cross doesn't fire any regular events because it's killing the application with quite brutal methods, you'll have to do something different.
I created this small sample to mimic your problem. The way I handled exiting is to hook up to Kernel32.Dll's SetConsoleCtrlHandler which fires events when your console application closes.
In my test app I have a class called server which writes to a file. In Main() I hook to the event and in the event I call stop method of the Server which flushes the stream to the text file. If the stop isn't called, it won't flush to the file and the file remains empty.
I replaced the while(true) loop with while(isRunning) and in Stop() I set isRunning to false.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace ConsoleApplication1
{
class Program
{
[System.Runtime.InteropServices.DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
private delegate bool EventHandler(CtrlType sig);
static EventHandler _handler;
enum CtrlType
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
private static Server server;
static void Main(string[] args)
{
_handler = new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
server = new Server();
server.Start();
}
private static bool Handler(CtrlType sig)
{
bool b = false;
switch (sig) {
case CtrlType.CTRL_C_EVENT:
case CtrlType.CTRL_LOGOFF_EVENT:
case CtrlType.CTRL_SHUTDOWN_EVENT:
case CtrlType.CTRL_CLOSE_EVENT:
SetConsoleCtrlHandler(_handler, false);
server.Stop();
b = true;
break;
default:
break;
}
return b;
}
}
class Server
{
public Server()
{
}
~Server()
{
Stop();
}
System.IO.StreamWriter stream;
public void Start()
{
stream = new System.IO.StreamWriter(@"D:\temp\server.txt", false);
stream.WriteLine("Start");
isRunning = true;
Run();
}
private bool isRunning = false;
private void Run()
{
while (isRunning) {
System.Threading.Thread.Sleep(500);
stream.WriteLine("running...");
}
}
public void Stop()
{
stream.WriteLine("Stop");
stream.Flush();
stream.Close();
isRunning = false;
}
}
}
#3
Re: problem with 'while(true)'
Posted 04 February 2012 - 03:05 PM
You can also do it without importing any dlls:
First, put in your using section:
Then in your main code, make a function:
Then in your inital code, at the top, put this line of code:
Be aware that this method has around 10 seconds of execution before your process will be considered "frozen"
First, put in your using section:
using System.Windows.Forms;
Then in your main code, make a function:
static void OnProcessExit (object sender, EventArgs e)
{
Console.WriteLine("Shutting down Process");
//DO STUFF HERE
Application.Exit();
}
Then in your inital code, at the top, put this line of code:
AppDomain.CurrentDomain.ProcessExit += new EventHandler (OnProcessExit);
Be aware that this method has around 10 seconds of execution before your process will be considered "frozen"
This post has been edited by Atli: 04 February 2012 - 03:45 PM
Reason for edit:: Changed [quote] tags to [code] tags.
#4
Re: problem with 'while(true)'
Posted 06 February 2012 - 09:01 AM
I can't confirm this. I've tested it, and the ProcessExit handler is never called. Are you sure this will work with a Console Application?
#5
Re: problem with 'while(true)'
Posted 07 February 2012 - 03:33 AM
Yes, this is with a console application. Here is the code (besides the server code):
using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Classic6;
using System.Windows.Forms;
namespace Classic6Server
{
class Program
{
static ClassicServer server;
private static Server server2 = new Server();
static void Main()
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler (OnProcessExit);
server = server2.NewServer();
string url = server.ServerUrl.ToString();
bool hasSet = false;
while (true)
{
if (!String.IsNullOrEmpty(url) && !hasSet)
{
Clipboard.SetText(server.ServerUrl.ToString());
hasSet = true;
}
string input = "";
while (input != "quit" && input != "q")
{
input = Console.ReadLine();
ParseInput(input);
}
}
}
static bool ParseInput(string input)
{
//unimportant stuff
return false;
}
static void OnProcessExit (object sender, EventArgs e)
{
Server.server.Log("Shutting down Server");
Server.server.Exit(false);
Application.Exit();
}
}
}
#6
Re: problem with 'while(true)'
Posted 09 February 2012 - 08:32 PM
thank you very much, this helped me a lot. rep++
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|