Page 1 of 1

How create a timeout Thread on Program.cs

Posted: Wed May 19, 2010 11:03 am
by Enjoy
Hello!

I need some help, i need to receive by argument a timeout on Program.cs create a thread call a method and start the time. When the timeout ends i need to do this Report.Error("timeout"); and Report.End();

my code is :
public static int Main(string[] args)
{
string logFileName = args[1];
//receive the timeout here by argument and convert to int(milliseconds)
Report.Setup(ReportLevel.Info, logFileName, true); try{ Recording1.Setup(args);
//create here the thread and start the timeout, when timeout finish close the method and make Report.Error("timeout"); and Report.End();
Recording1.Start();

Re: How create a timeout Thread on Program.cs

Posted: Wed May 19, 2010 12:49 pm
by Enjoy
Ok i have already solve the problem doing that:
public static int Main(string[] args)
{
Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;
int error = 0;

string searchName = "xx";

string logFileName = args[1];

Report.Setup(ReportLevel.Info, logFileName, true);

string timeout = args[2];

//int timetOut = Convert.ToInt32(timeout);

Thread workerThread = new Thread(Recording1.Start);

try
{
Recording1.Setup(args);

int timetOut = Convert.ToInt32(timeout);
Report.Error(timeout);

TimeSpan waitTime = new TimeSpan(0, 0, 6);

workerThread.Start();

if(workerThread.Join(waitTime + waitTime))
{
Console.WriteLine("New thread terminated.");
}
else
{
Report.Error("Join timed out.");
Report.End();
workerThread.Abort();
Process[] processes = Process.GetProcessesByName(searchName);

foreach (Process proc in processes)
{
proc.Kill();
proc.WaitForExit();
}

error = -1;
return error;
}