473,383 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

a little console app i wrote

Ok, here's an attempt at something. I figure I can use this to let me
know when my laundry's done! :)

I'm hoping you guys can spot ways to make it better/cleaner/more
efficient, etc. especially where the math is involved. Thanks.

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
int iDelayMS = iDelay * 60 * 1000;
Thread.Sleep(iDelayMS);
Console.WriteLine("\a");
Console.WriteLine(iDelay + " minutes have elapsed.");
}
}
Nov 17 '05 #1
7 1703
It's fine; my suggested improvements are very minor and not really worth the
effort for such a trivial app:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
Thread.Sleep(iDelay * 60000);
Console.WriteLine("\a{0} minutes have elapsed",iDelay);
}
}

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:-t********************@rcn.net...
Ok, here's an attempt at something. I figure I can use this to let me know
when my laundry's done! :)

I'm hoping you guys can spot ways to make it better/cleaner/more
efficient, etc. especially where the math is involved. Thanks.

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
int iDelayMS = iDelay * 60 * 1000;
Thread.Sleep(iDelayMS);
Console.WriteLine("\a");
Console.WriteLine(iDelay + " minutes have elapsed.");
}
}

Nov 17 '05 #2
Bob Grommes wrote:
It's fine; my suggested improvements are very minor and not really worth the
effort for such a trivial app:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
Thread.Sleep(iDelay * 60000);
Console.WriteLine("\a{0} minutes have elapsed",iDelay);
}
}


I like how you've condensed the calculation though. And I especially
like the final WriteLine method. That's exactly the kind of thing I was
looking for.

Thanks.
Nov 17 '05 #3
John Salerno wrote:
Bob Grommes wrote:
It's fine; my suggested improvements are very minor and not really worth the
effort for such a trivial app:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
Thread.Sleep(iDelay * 60000);
Console.WriteLine("\a{0} minutes have elapsed",iDelay);
}
}

I like how you've condensed the calculation though. And I especially
like the final WriteLine method. That's exactly the kind of thing I was
looking for.

Thanks.


If I change it to this:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
Thread.Sleep(iDelay * 60000);
Console.WriteLine("\a{0} minutes have elapsed", iDelay);
Thread.Sleep(2000);
Console.WriteLine("\a");
Thread.Sleep(2000);
Console.WriteLine("\a");
}
}

is there a more efficient way of repeating the sound, other than copying
and pasting code? It seems like there's always a better way than doing that.
Nov 17 '05 #4
John Salerno <jo******@NOSPAMgmail.com> wrote:
is there a more efficient way of repeating the sound, other than copying
and pasting code? It seems like there's always a better way than doing that.


How about:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

Thread.Sleep(iDelay * 60000);
Beep();

Console.WriteLine("{0} minutes have elapsed", iDelay);

Thread.Sleep(2000);
Beep();

Thread.Sleep(2000);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

One step further:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

SleepThenBeep(iDelay * 60000);
Console.WriteLine("{0} minutes have elapsed", iDelay);

SleepThenBeep(2000);
SleepThenBeep(2000);
}

static void SleepThenBeep(int sleepDuration)
{
Thread.Sleep(sleepDuration);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

Note that there are a couple of bugs in this program (and there have been
from the start). :-)
Nov 17 '05 #5
"C# Learner" <cs****@learner.here> schrieb im Newsbeitrag
news:1c****************@csharp.learner...
John Salerno <jo******@NOSPAMgmail.com> wrote:
is there a more efficient way of repeating the sound, other than copying
and pasting code? It seems like there's always a better way than doing
that.


How about:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

Thread.Sleep(iDelay * 60000);
Beep();

Console.WriteLine("{0} minutes have elapsed", iDelay);

Thread.Sleep(2000);
Beep();

Thread.Sleep(2000);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

One step further:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

SleepThenBeep(iDelay * 60000);
Console.WriteLine("{0} minutes have elapsed", iDelay);

SleepThenBeep(2000);
SleepThenBeep(2000);
}

static void SleepThenBeep(int sleepDuration)
{
Thread.Sleep(sleepDuration);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

Note that there are a couple of bugs in this program (and there have been
from the start). :-)


i'd prefer Console.Write("\a"); instead of Console.WriteLine("\a");
Why go to new line, if you only want to beep?
Nov 17 '05 #6
C# Learner wrote:
John Salerno <jo******@NOSPAMgmail.com> wrote:

is there a more efficient way of repeating the sound, other than copying
and pasting code? It seems like there's always a better way than doing that.

How about:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

Thread.Sleep(iDelay * 60000);
Beep();

Console.WriteLine("{0} minutes have elapsed", iDelay);

Thread.Sleep(2000);
Beep();

Thread.Sleep(2000);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

One step further:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

SleepThenBeep(iDelay * 60000);
Console.WriteLine("{0} minutes have elapsed", iDelay);

SleepThenBeep(2000);
SleepThenBeep(2000);
}

static void SleepThenBeep(int sleepDuration)
{
Thread.Sleep(sleepDuration);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

Note that there are a couple of bugs in this program (and there have been
from the start). :-)


But there's still repetition of code. I suppose for a couple of times
it's no problem, and I could use a for loop if I wanted to do something
bigger.
Nov 17 '05 #7
John Salerno <jo******@NOSPAMgmail.com> wrote:
But there's still repetition of code. I suppose for a couple of times
it's no problem, and I could use a for loop if I wanted to do something
bigger.


I didn't want to change the code too much at first, but, now that you
mention it, I think it's good practise to parameterize here; e.g.:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

const int OneMinute = 60000;
SleepThenBeep(iDelay * OneMinute);
Console.WriteLine("{0} minutes have elapsed", iDelay);

const int PostAlarmBeepCount = 2;
for (int i = 0; i < PostAlarmBeepCount; ++i)
{
const int PostAlarmBeepDelay = 2000;
SleepThenBeep(PostAlarmBeepDelay);
}
}

static void SleepThenBeep(int sleepDuration)
{
Thread.Sleep(sleepDuration);
Beep();
}

static void Beep()
{
Console.Write('\a');
}
}
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
32
by: John Salerno | last post by:
My first project when I started learning C# was to make a little timer to tell me when my laundry was done :) and I thought it would be fun to convert this to Python. Here's what I came up with...
3
by: julianmoors | last post by:
Hey, Currently I'm writing a VB.NET/1.1 app and I need to mask the input for the password field. Does anyone know how to do this in VB? I've seen a C# example, but wouldn't know how to convert...
6
by: tony | last post by:
Hello! When you have windows forms you have the same possibility as when you have a Console application to use Console.Writeln to write whatever on the screen. Now to my question: Is it...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
3
by: ThaDoctor | last post by:
Hi. I am quite new to C++ so I think I would ask here what I am doing wrong with this code. I am writing a little game in a text console, but here is something that is in no way related to the...
2
by: davidson1 | last post by:
Hai friends....the below code is working well for sending sms from VS2008 C# windows Application...........but i want the same to work in ASP.NET(vb)............can anyone help me....I converted the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.