Hey all,
I've only been learning to program now for about a week and i just wonderd if someone wouldnt mind looking over my first program (that works!).
I just wanted to make sure i'm doing everything the way i should be (good practises etc etc).
Basicly i tend to use my PC as a DVD player. Now alot of DVD's when they finish go back to the menu system and loop the music. I currently was just using a scheduled batch file to end the task at a specific time. But i thought i would try and use what little C# i had learnt (and googled what i haddent). I created a program that lets me select a process and then specify a length of time that it will countdown to till the process ends.
As i said above it does work. But it just dosnt seem very neat. I just wonderd if i was learning the "right way" or if im just making a mess!
I know i still need to add error checking.
Thanks,
James
CODE:
using System;
using
System.Collections.Generic;using
System.Linq;using
System.Text;using
System.Diagnostics;using
System.Collections;using
System.Threading;namespace
ConsoleApplication1{
class Program{
static void Main(string[] args){
int min = 0; // How Many Mins Till Kill Process int hour = 0; // How Many Hours Till Kill Process WorkingWithProcesses killTimer = new WorkingWithProcesses(); Console.WriteLine("How Many Hours?:");hour =
Convert.ToInt32(Console.ReadLine()); Console.WriteLine("How Many Minutes?:");min =
Convert.ToInt32(Console.ReadLine());killTimer.countdown(hour, min);
Console.ReadLine();}
}
class WorkingWithProcesses{
public Process[] arrProcesses = Process.GetProcesses(); public int selection = 0; public WorkingWithProcesses(){
int i = 1; foreach (Process proc in this.arrProcesses){
Console.WriteLine("{0}) {1}", i, proc.ProcessName);i++;
}
Console.Write("Please Make A Selection: "); this.selection = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("You Selected: " + rtnName);}
private string rtnName
{
get { return arrProcesses[selection - 1].ProcessName; }}
public void countdown(int hours, int mins)
{
DateTime killTime = DateTime.Now.AddMinutes(mins).AddHours(hours).AddSeconds(0); // Set the time you want to kill the process. do{
Console.WriteLine(killTime - DateTime.Now); Thread.Sleep(1000); // Wait 1 second before showing next countdown time.}
while (killTime > DateTime.Now); Console.WriteLine(rtnName + " Shutdown @ " + DateTime.Now);killprocess(rtnName);
}
private void killprocess(string name) // This Ends The Process{
Process[] processToKill = Process.GetProcessesByName(name);processToKill[0].Kill();
}
}
}
Replies
Know the answer? Post it — somebody with the same question will find it here.