public void TestAbortThread() { Lock sync = new Lock(); TimedCondition aborted = new TimedCondition(sync); Thread testThread = new Thread(delegate() { try { Log("Test thread sleeping for 8 seconds.."); Thread.Sleep(8000); } catch (ThreadAbortException) { Log("Test thread aborted.."); aborted.Signal(); } Fail("This line should never be executed"); }); testThread.Start(); sync.Acquire(); Log("Aborting test thread in 2 seconds. Sleeping..."); Thread.Sleep(2000); Log("...awake! Going to abort the test thread."); testThread.Abort(); Log("Called testThread.Abort()"); try { aborted.Await(12000); } catch (ConditionTimedOutException) { Fail("Thread was not aborted as expected!"); } }
public void TestAbortThreadWithState() { Lock sync = new Lock(); TimedCondition aborted = new TimedCondition(sync); Thread testThread = new Thread(delegate() { try { Log("Test thread sleeping for 8 seconds.."); Thread.Sleep(8000); } catch (ThreadAbortException e) { if (e.ExceptionState != null && e.ExceptionState.ToString() == "Exception state") { Log("Test thread aborted with state: " + e.ExceptionState); aborted.Signal(); } else { Log("Test thread aborted with unknown state: " + e.ExceptionState); } } Fail("This line should never be executed"); }); testThread.Start(); sync.Acquire(); Log("Aborting test thread in 2 seconds"); Thread.Sleep(2000); testThread.Abort("Exception state"); try { aborted.Await(12000); } catch (ConditionTimedOutException) { Fail("Thread was not aborted as expected!"); } }
public object Call(string method, object[] args, int timeout) { XmlHttpRequest xhr = XmlHttpRequestManager.GetXmlHttpRequest(); Lock l = new Lock(); l.Acquire(); TimedCondition condition = new TimedCondition(l); OnReadyStateChangeHandler handler = new OnReadyStateChangeHandler(xhr, condition); xhr.OnReadyStateChange = (NativeFunction)new VoidDelegate(handler.OnReadyStateChange); xhr.Open("POST", ServerUrl, true, Username, Password); xhr.SetRequestHeader(RpcMethodRequestHeader, method); xhr.Send(Serialize(args)); if (timeout > 0) { try { // wait for timeout milliseconds condition.Await(timeout); } catch (ConditionTimedOutException) { Logging.Debug("Request timed out!!!"); throw new JsonRpcCallTimedOutException(this, method, args, timeout); } } else { // wait indefinitely condition.Await(); } // we have a response from the server, or we couldn't connect Logging.Debug("Status code is: " + xhr.Status); if (xhr.Status == 200) { Logging.Debug("Got 200! Response text is " + xhr.ResponseText); return Deserialize(xhr.ResponseText); } else if (xhr.Status == 0) { Logging.Debug("Request timed out!!!"); throw new JsonRpcCallTimedOutException(this, method, args, 1000); // TODO: Extract into constant of 1000 ms. Also this should be some sort of connection refused exception instead } else { throw GenerateException(xhr.Status, method, args, xhr.ResponseText); } }