Timed Operation
i need to run a command and stop the command if its running too long, is this possible??
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Roei BarRank #1,271 · 1.4K reputationPosted Aug 19, 2009, 11:29 PM EDT
using cellcom; using System.Threading; using System; using cellcom.SRE; public class WatchdogRunner { public string Response; string inputXML; string clientProcessID; AbstractSREPlug cPII; // Notifies a waiting thread that an event has occurred private AutoResetEvent evnt = new AutoResetEvent(false); public bool RunTimedCommand(TimeSpan timeout, AbstractSREPlug clientPII, string inputData, string ProcessId) { Thread th = new Thread(new ThreadStart(runCommand)); this.cPII = clientPII; this.inputXML = inputData; this.clientProcessID = ProcessId; // Sets the state of the specified event to non signaled. evnt.Reset(); // start thread th.Start(); // wait with timeout on the event if (evnt.WaitOne(timeout, false)) { // Sucess - timeout did not occurred return true; } else { // Failure - Timeout - do cleanup th.Abort(); return false; } } private void runCommand() { // run sample method with out parameters this.Response = // Sets the state of the specified event to signaled evnt.Set(); } }this code will run a method as you want in a different Thread while keeping the main in block till expiration time elapsed
to run it you do the following :
WatchdogRunner wcr = new WatchdogRunner(); bool RenderResponse = wcr.RunTimedCommand(plugInTimeOut, cPII, inputXML, clientProcessID); if ((RenderResponse)) { functionReturnValue = wcr.Response; } else { throw new ApplicationException("Assembly Render Timeout : " + plugInName); }of course you will need to change the params as needed :-)
dont forget to mark this as "Accepted Answer"