473,836 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Threadin g;

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

using System;
using System.Threadin g;

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

"John Salerno" <jo******@NOSPA Mgmail.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.Threadin g;

class Alarm
{
static void Main()
{
Console.Write(" Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Con sole.ReadLine() );
int iDelayMS = iDelay * 60 * 1000;
Thread.Sleep(iD elayMS);
Console.WriteLi ne("\a");
Console.WriteLi ne(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.Threadin g;

class Alarm
{
static void Main()
{
Console.Write(" Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Con sole.ReadLine() );
Thread.Sleep(iD elay * 60000);
Console.WriteLi ne("\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.Threadin g;

class Alarm
{
static void Main()
{
Console.Write(" Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Con sole.ReadLine() );
Thread.Sleep(iD elay * 60000);
Console.WriteLi ne("\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.Threadin g;

class Alarm
{
static void Main()
{
Console.Write(" Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Con sole.ReadLine() );
Thread.Sleep(iD elay * 60000);
Console.WriteLi ne("\a{0} minutes have elapsed", iDelay);
Thread.Sleep(20 00);
Console.WriteLi ne("\a");
Thread.Sleep(20 00);
Console.WriteLi ne("\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******@NOSPA Mgmail.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.Threadin g;

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

Thread.Sleep(iD elay * 60000);
Beep();

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

Thread.Sleep(20 00);
Beep();

Thread.Sleep(20 00);
Beep();
}

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

One step further:

using System;
using System.Threadin g;

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

SleepThenBeep(i Delay * 60000);
Console.WriteLi ne("{0} minutes have elapsed", iDelay);

SleepThenBeep(2 000);
SleepThenBeep(2 000);
}

static void SleepThenBeep(i nt sleepDuration)
{
Thread.Sleep(sl eepDuration);
Beep();
}

static void Beep()
{
Console.WriteLi ne("\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******@NOSPA Mgmail.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.Threadin g;

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

Thread.Sleep(iD elay * 60000);
Beep();

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

Thread.Sleep(20 00);
Beep();

Thread.Sleep(20 00);
Beep();
}

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

One step further:

using System;
using System.Threadin g;

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

SleepThenBeep(i Delay * 60000);
Console.WriteLi ne("{0} minutes have elapsed", iDelay);

SleepThenBeep(2 000);
SleepThenBeep(2 000);
}

static void SleepThenBeep(i nt sleepDuration)
{
Thread.Sleep(sl eepDuration);
Beep();
}

static void Beep()
{
Console.WriteLi ne("\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.WriteLi ne("\a");
Why go to new line, if you only want to beep?
Nov 17 '05 #6
C# Learner wrote:
John Salerno <jo******@NOSPA Mgmail.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.Threadin g;

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

Thread.Sleep(iD elay * 60000);
Beep();

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

Thread.Sleep(20 00);
Beep();

Thread.Sleep(20 00);
Beep();
}

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

One step further:

using System;
using System.Threadin g;

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

SleepThenBeep(i Delay * 60000);
Console.WriteLi ne("{0} minutes have elapsed", iDelay);

SleepThenBeep(2 000);
SleepThenBeep(2 000);
}

static void SleepThenBeep(i nt sleepDuration)
{
Thread.Sleep(sl eepDuration);
Beep();
}

static void Beep()
{
Console.WriteLi ne("\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******@NOSPA Mgmail.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.Threadin g;

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

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

const int PostAlarmBeepCo unt = 2;
for (int i = 0; i < PostAlarmBeepCo unt; ++i)
{
const int PostAlarmBeepDe lay = 2000;
SleepThenBeep(P ostAlarmBeepDel ay);
}
}

static void SleepThenBeep(i nt sleepDuration)
{
Thread.Sleep(sl eepDuration);
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
5389
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 application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
5
11271
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 to avoid the overhead of writing to the console if it is not visible, eg I am running inside a WinForm application. thanks
17
4243
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 console using System.Console.WriteLine if thge project has its "Output Type" to "Windows Application". However I can write to stdio if i set output type to "Console Application". When I do this I unfortunately get a "console box" as well
32
1877
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 after much struggling with the Timer class from the threading module -- as you can see, I abandoned it for the sleep() function from timer. Please let me know if this is a good (i.e. Pythonic) way of doing this, and if it can be improved (although...
3
11378
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 it myself. Here's the URL: http://www.codeproject.com/dotnet/ConsolePasswordInput.asp
6
5719
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 possible to use Console.Writeln when you have a Webservice. I don't think it's possible but just to be sure I ask you?
1
2047
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 error: The CLR has been unable to transition from COM context 0x1a2008 to COM context 0x1a2178 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long...
3
1661
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 game but when I am compiling this it yields an error.... Why is that #include<iostream>
2
1128
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 code and tried but it didnt work.............................................. In the String uri....under send_to put ur mobile Number............... CODING:-(c# windows Application) using System; using System.Collections.Generic; using...
0
9825
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9672
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10854
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10260
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9388
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5652
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.