I have a multi-threaded C# WPF application that is a plug-in to a larger software (Princeton Instruments LightField, for those who are interested).
My plug-in invokes a synchronous method on a thread separate from the GUI. This method is from a camera API and captures an n-second exposure from the camera and returns the frame that was captured.
The situation looks something like this:
private static volatile bool _STOP = true;
// Runs when the "Run" button is clicked
private void Run_Click(object sender, RoutedEventArgs e)
{
object time = InputTime.Text; // a non-negative integer provided by the user
_STOP = false;
Thread run_thread = new Thread(Running);
run_thread.Start(time);
}
// Runs when the "Stop" button is clicked
private void Stop_Click(object sender, RoutedEventArgs e)
{
_STOP = true;
}
// Separate Thread from GUI
private void Running(object t)
{
int time = Convert.ToInt32(t);
FrameObject f;
while(!_STOP)
{
// synchronously capture a t-second exposure
f = CameraAPI.CaptureImage(t);
// display the exposure on a viewer controlled by the parent thread
Application.Current.Dispatcher.BeginInvoke(new Action(Viewer.Display(f));
}
}
This code (or, rather, it's more complex sibling) works perfectly fine on the computer I developed it on (Computer A). I click Run and the entire application remains responsive while the code is running.
When I try to run it on the computer where it will be hosted regularly (Computer B), however, the following behavior appears:
CameraAPI.CaptureImage(n);
method). This occurs for any positive integer n and continues as the
while loop executes this method each loop cycle (i.e. the application freezes for n seconds, there is a brief unfrozen moment while the Display method is called, and then the application freezes again for n seconds).Thread.Sleep(n); in place of
CameraAPI.CaptureImage(n);, the application does not freeze.Application.Current.Dispatcher.BeginInvoke(new Action(Viewer.Display(f)); line does not stop the GUI freezing. This is not the problem method.So, on two seemingly identical systems, the same code has different results. On System A, it works as intended (no GUI freezing), and on System B, the GUI of the entire application -- not just of the plug-in that I am writing -- freezes each time a synchronous method is called.
This error behavior exceeds my understanding of computers, so now I'm here. Any ideas as to what is causing this difference in behavior?
We reinstalled the parent application and everything works fine. Still have no idea why this happened, but closing the question since the problem is resolved. A true case of "turn it off and back on again."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With