Main Page | Report this Page
.NET DotNet Forum Index  »  Visual C# Forum  »  Adding a keyDown event to a console app?...
Page 1 of 1    

Adding a keyDown event to a console app?...

Author Message
Rich P...
Posted: Fri Oct 30, 2009 4:26 pm
Guest
The following code sample is a fully functional console project that
displays current time in the console window - the purpose is to display
Delegate and Event usage. What I want to do is to add an extra key down
event where when I press any key - the program will stop executing -
similar to a key down on a Console.Readline();

How would I implement a stop executing event in this program?


---------------------------------------------
using System;
using System.Threading;

namespace DelegatesEvents
{
// Our subject -- it is this class that other classes
// will observe. This class publishes one event:
// SecondChange. The observers subscribe to that event.
public class Clock
{
// Private Fields holding the hour, minute and second
private int _hour;
private int _minute;
private int _second;

public delegate void SecondChangeHandler(
object clock,
TimeInfoEventArgs timeInformation
);

// The event we publish
public event SecondChangeHandler SecondChange;

// The method which fires the Event
protected void OnSecondChange(
object clock,
TimeInfoEventArgs timeInformation
)
{
// Check if there are any Subscribers
if (SecondChange != null)
{
// Call the Event
SecondChange(clock, timeInformation);
}
}

// Set the clock running, it will raise an
// event for each new second
public void Run()
{
for (; ; )
{
// Sleep 1 Second
Thread.Sleep(1000);

// Get the current time
System.DateTime dt = System.DateTime.Now;

// If the second has changed
// notify the subscribers
if (dt.Second != _second)
{
// Create the TimeInfoEventArgs object
// to pass to the subscribers
TimeInfoEventArgs timeInformation =
new TimeInfoEventArgs(
dt.Hour, dt.Minute, dt.Second);

// If anyone has subscribed, notify them
OnSecondChange(this, timeInformation);
}

// update the state
_second = dt.Second;
_minute = dt.Minute;
_hour = dt.Hour;

}
}
}

public class TimeInfoEventArgs : EventArgs
{
public TimeInfoEventArgs(int hour, int minute, int second)
{
this.hour = hour;
this.minute = minute;
this.second = second;
}
public readonly int hour;
public readonly int minute;
public readonly int second;
}

/*====Event Subscribers ============== */

// An observer. DisplayClock subscribes to the
// clock's events. The job of DisplayClock is
// to display the current time
public class DisplayClock
{
// Given a clock, subscribe to
// its SecondChangeHandler event
public void Subscribe(Clock theClock)
{
theClock.SecondChange += new
Clock.SecondChangeHandler(TimeHasChanged);
}

public void TimeHasChanged(object theClock, TimeInfoEventArgs ti)
{
Console.WriteLine("Current Time: {0}:{1}:{2}",
ti.hour.ToString(),
ti.minute.ToString(),
ti.second.ToString());
}
}

// A second subscriber whose job is to write to a file
public class LogClock
{
public void Subscribe(Clock theClock)
{
theClock.SecondChange +=
new Clock.SecondChangeHandler(WriteLogEntry);
}

public void WriteLogEntry(
object theClock, TimeInfoEventArgs ti)
{
Console.WriteLine("Logging to file: {0}:{1}:{2}",
ti.hour.ToString(),
ti.minute.ToString(),
ti.second.ToString());
}
}

class Program
{
static void Main(string[] args)
{
// Create a new clock
Clock theClock = new Clock();
// Create the display and tell it to
// subscribe to the clock just created
DisplayClock dc = new DisplayClock();
dc.Subscribe(theClock);
// Create a Log object and tell it
// to subscribe to the clock
LogClock lc = new LogClock();
lc.Subscribe(theClock);
// Get the clock started
theClock.Run();
}
}
}
---------------------------------------------

Rich

*** Sent via Developersdex http://www.developersdex.com ***
 
Mr. Arnold...
Posted: Fri Oct 30, 2009 5:59 pm
Guest
"Rich P" <rpng123 at (no spam) aol.com> wrote in message
news:ukc9$$aWKHA.3720 at (no spam) TK2MSFTNGP04.phx.gbl...
Quote:
The following code sample is a fully functional console project that
displays current time in the console window - the purpose is to display
Delegate and Event usage. What I want to do is to add an extra key down
event where when I press any key - the program will stop executing -
similar to a key down on a Console.Readline();

How would I implement a stop executing event in this program?

You use the Console.ReadKey. Somewhere in your code most likely the Main(),
you put the program in a While Loop on the ReadKey and exit the loop if the
key you're looking for is pressed.


This is just a general how to, you can use Bing Google and look-up
Console.Readkey() and how to use it.

while true
{

var keyhit = Console.ReadKey();

if (keyhit == the key)
break;

processsomething();
}


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4559 (20091030) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Mon Dec 07, 2009 1:09 am