Hi all i have created a small application to execute “Command Prompt” with a command so far i created a simple method with thread sleep
public static string Executecmd(string command, int sleepSec) {
try {
string result = null;
System.Threading.Thread objThread = new System.Threading.Thread(delegate() {
result = ExecuteCommandSync(command);
});
objThread.IsBackground = true;
objThread.Start();
while (objThread.IsAlive == true) {
System.Threading.Thread.Sleep(sleepSec * 1000);
objThread.Abort();
}
return result;
}
catch (Exception x) {
Console.WriteLine(x.Message + "\n" + x);
return null;
}
}
it works fine but even if the command executed finish it stays sleep until the thread sleep is completed so my question is how can i create a method which will excecute it and sleep for 5 secs and if it completed it stops else wait 5 sec then abort
Use Thread.Join with a timespan.