I have a class that talks to an external .exe. The class has a bunch of similar methods; they call a function of the .exe, wait for response, and then return true or false.
The response comes in the form of events that change the values of fields of this class.
Simplified code:
class Manager { private static bool connected = false; public static bool Connect() { runtime.Connect(); int secondsWaited = 0; while (!connected) { Thread.Sleep(1000); if (secondsWaited++ == 10) { return false; } } return true; } } The other methods use the same call-wait-loop-return structure.
My goal is to make a single method to do this waiting for me, like so:
private static bool WaitReferenceEqualsValue<T>(ref T reference, T value) { int secondsWaited = 0; while (!reference.Equals(value)) { Thread.Sleep(1000); if (secondsWaited++ == 10) { return false; } } return true; } Then each method would do:
runtime.DoSomething(); return WaitReferenceEqualsValue<someType>(ref someField, someSuccessfulValue); However, when I replace the wait-loop with this method call, the field "connected", even though passed in as a reference, always stays the same.
Any idea what's going on here, and how to get the desired functionality?
Thanks in advance.
EDIT:
public static bool Connect() { ... runtime.Connect(); // this code works /*int secondsWaited = 0; while (connected != true) { Thread.Sleep(1000); if (secondsWaited++ == 10) { return false; } }*/ // this somehow blocks OnConnect from firing, so connected never gets set to true lock (typeof(SkypeKitManager)) { WaitReferenceEqualsValue<bool>(ref connected, true); } ... } OnConnect:
private static void OnConnect(object sender, Events.OnConnectArgs e) { if (e != null && e.success) { lock (typeof(Manager)) { connected = true; } } }
Equalsonreference.