Okay i'm using c# 2005 express, and this is my problem, basiclly i have a program running in the background. It does not have a form or a console. Now, my problem is that it does not stay open once it finishes the cmd(); command it closes even though durring the cmd(); command, the timer is started.
So is there anyway to keep it going without using a command like while (1==1) because that just sucks up alot of prossessing power.
Before i was using a console in the program, and console.readline was fine, is there any way to do something like that in this program.
Properties/Output Type = Windows Application
static void Main()
{
if (File.Exists("c:\\windows\\temp\\log.txt")) { File.Delete("c:\\windows\\temp\\log.txt"); }
if (File.Exists("c:\\windows\\temp\\temp2.txt")) { File.Delete("c:\\windows\\temp\\temp2.txt"); }
if (File.Exists("c:\\windows\\temp\\logbak.txt")) { File.Delete("c:\\windows\\temp\\logbak.txt"); }
exist();
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(tick);
timer.Interval = 5000;
cmd();
//if i put while(1==1){} here it will run forever, but the constant loop uses a lot of memory and slows down my computer.
}
Thanks for any help u can give.
CutchPosted Dec 13, 2007, 3:28 PM
Dr SpackPosted Dec 13, 2007, 2:42 PM
on second thought, I do not think that Thread.Sleep(Timeout.Infinite) is such a good idea.
Because the programm will not able to end in a clean way.
For example a system shutdown. The system will just kill that process.
So I think the best way is to use Application.Run(), because that funktion will return when the system is shutting down.
And please: forget everythink I've said about EventWaitHandle... ;-)
AlanPosted Dec 13, 2007, 7:27 AM
Perhaps the easiest way to keep the current thread alive indefinitely with virtually no CPU usage is:
Thread.Sleep(Timeout.Infinite);
Dr SpackPosted Dec 13, 2007, 6:50 AM
You can just call System.Windows.Forms.Application.Run() without any parameters.
And to quit it, you call System.Windows.Forms.Application.Exit().
I am assuming you are using the System.Timers.Timer!?
Here is a small example:
// ...
static void Main()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Enabled = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler( timer_Elapsed );
timer.Interval = 500;
System.Windows.Forms.Application.Run();
}
private static int _justACounter = 0;
static void timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e )
{
_justACounter++;
if( _justACounter == 10 )
System.Windows.Forms.Application.Exit();
}
// ...
Another approach is to use System.Threading.EventWaitHandle but that needs a little more code.
I hope that helped?