Click here to Skip to main content
15,885,655 members
Articles / Programming Languages / C#
Article

AlarmTimer

Rate me:
Please Sign up or sign in to vote.
2.93/5 (9 votes)
25 May 2004CPOL2 min read 69.7K   5.1K   65   2
This is a 3 in 1 program featuring a Scheduler, an Email Sender and a Command Line executer

Introduction

The application acts as an Alarm, Reminder, Mailer and Command Line executor all packed into one. You can set the timer to start an alarm after some specified time. You can also add a Message to pop up at that time to remind of something to be done. Or you could execute any commands from the command prompt or even better you can compose a mail that will be sent at the predetermined time. Now in case you want to send mail alone or execute a command without using the Timer then that too is possible.

Background

It all started as a simple Scheduler and then I started adding on these extra features when I came across difficulties faced by others on the Net with regard to:

  • Making Sounds in C#
  • Sending attachment along with mail
  • Hiding an application while it is in execution mode
  • Getting a Command Prompt

Using the code

First we'll start off with the code related to the Timer: The implementation for this application has made some use of the Timer alarm code taken from the article Use a timer to create a simple alarm application by Andrew Boisen. My complements to you, Andrew Boisen. So you can refer to that article for detailed comments and the whole code that he has used. The additional input made was for the sound by using Kernel32.dll.You will have to use the System.Runtime.InteropServices namespace.

C#
//For Sound
using System.Runtime.InteropServices;
Add this to your variable declarations.
C#
//For Sound
[DllImport("Kernel32.dll")]
public static extern bool Beep(int freq, int duration);
Now that all that we need to make the sound is
C#
Beep(1000,500);
For Sending Mail you need to add a reference to your project to System.Web. Then we need to add the System.Web and System.Web.Mail namespaces.
C#
//For Mail
using System.Web;
using System.Web.Mail;
The code snippet shown below is self documentary. You can place this code in your button click event also.
C#
// Construct a new mail message and fill it 
// with information from the form
MailMessage msgMail = new MailMessage();
msgMail.From = txtFrom.Text;       //From Textbox
msgMail.To = txtTo.Text;         //To Textbox
msgMail.Cc = txtCC.Text;           //CC Textbox 
msgMail.Bcc = txtBCC.Text;         //BCC Textbox
msgMail.Subject = txtSubject.Text; //Subject Textbox
msgMail.Body = txtMessage.Text;     //Message Textbox  
// if an attachment file is indicated, create it and add it to the message
if (txtAttachment.Text.Length > 0)
  msgMail.Attachments.Add(new MailAttachment(
    txtAttachment.Text, MailEncoding.Base64));
// Now send the message
SmtpMail.Send(msgMail);
// Indicate that the message has been sent
string strSentMail="";
strSentMail = "Message Sent to " + txtTo.Text;
if(txtCC.Text!="")
  strSentMail+= " Carbon Copy Sent to " + txtCC.Text;
if(txtBCC.Text!="")
  strSentMail+= " Blind Carbon Copy Sent to " + txtBCC.Text;
MessageBox.Show(strSentMail);
To get the command prompt to execute your commands and exit is done by the code snippet below. But first we need to use the System.Diagnostics namespace.
C#
//For Command Line
using System.Diagnostics;
Then you can add this code snippet to get your command prompt.
C#
//Declare and instantiate a new process component.
System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
//Do not receive an event when the process exits.
process1.EnableRaisingEvents = false;
//The "/C" Tells Windows to Run The Command then Terminate 
string strCmdLine;
strCmdLine = "/C " + txtCmd.Text.Trim(); 
  //txtCmd is the text box where we enter the commands
System.Diagnostics.Process.Start("CMD.exe",strCmdLine);
process1.Close();

Points of Interest

The only nutty thing that I did was to have the mailing program and the command line executor added into the timer. So when the predetermined time was over you could send mail and execute commands. And you could execute the commands for any number of times depending on the number you inputted in the textbox with instances. (These options do work independent of the timer also.)

Conclusion

Its so easy to make a simple application really "un-simple??". Let me end with this quote - "If confusion is the first step to knowledge, I must be a genius."

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Aby Thomas Varghese lives in India, at Mangad in Kerala. He is a Software Engineer who has been working in the Software Field for some time now, in the city of Chennai. He works exclusively in Microsoft .NET platform with C# and ASP.NET. Previously he worked at Infortech Alliance, Cochin.

Prime interests: Graphics, AI, and Computer Games
Activities he loves: Working on his own projects, Listening to music, Reading anything interesting he can get his hands on.

Comments and Discussions

 
Generalusing system.web.mail; Pin
VGstudios6-Sep-07 16:34
VGstudios6-Sep-07 16:34 
GeneralCode snippet comment Pin
Julio Cesar Dominguez23-Oct-06 21:03
Julio Cesar Dominguez23-Oct-06 21:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.