/*
* $Id: Engine.cs,v 1.7 2004/08/06 10:31:46 michaelferrante Exp $
* Copyright (c) 2002-2003, Chive Software Limited
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 2. Neither the name of Chive Software Limited nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Specialized;
using System.Threading;
namespace Sleeper{
/// <summary>
/// Engine
/// </summary>
/// <remarks>
/// Executes for a given time period, passed in on the command line in milliseconds.
/// </remarks>
class Engine {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args) {
int returnCode = 0;
try {
StringCollection argList = new StringCollection();
argList.AddRange(args);
returnCode = ParseAndRun(argList);
} catch (Exception ex) {
WriteLineBoth(ex.ToString());
returnCode = -1;
}
return returnCode;
}
private static int ParseAndRun(StringCollection args)
{
if (args.Count < 1 || args.Count > 3) {
WriteLineBoth("Invalid number of params");
Usage();
return 2;
}
if (args.Count > 1 && args[1] != "/input" && args[1] != "/error")
{
WriteLineBoth("Invalid argument 1 " + args[1]);
Usage();
return 4;
}
if (args.Count > 2 && args[2] != "/input" && args[2] != "/error")
{
WriteLineBoth("Invalid argument 2 " + args[2]);
Usage();
return 8;
}
int sleepMs = Int32.Parse(args[0]);
TimeSpan sleepTime = TimeSpan.FromMilliseconds(sleepMs);
bool waitForInput = args.Contains("/input");
bool error = args.Contains("/error");
DoSleepReally(sleepTime, waitForInput, error);
return 0;
}
private static void DoSleepReally(TimeSpan sleepTime, bool waitForInput, bool error)
{
if (waitForInput)
{
Console.WriteLine(Console.ReadLine());
}
Thread.Sleep(sleepTime);
Console.WriteLine("Executed for {0} milliseconds", sleepTime.TotalMilliseconds);
if (error)
{
throw new ApplicationException("Sleeper Forced Error");
}
}
private static void WriteLineBoth(string format, params object[] args) {
Console.WriteLine(format, args);
Console.Error.WriteLine(format, args);
}
private static void Usage()
{
Console.WriteLine("");
Console.WriteLine("Usage:");
Console.WriteLine("\tsleeper.exe <sleepTimeInMS> [/input] [/error]");
Console.WriteLine("\t/input do a read from StdIn");
Console.WriteLine("\t/error return exit code < 0");
}
}
}
|