I have a program that is supposed to test various settings in windows image. Each test is labeled a 'plugin'. Now all my plugins are being run in worker threads using the ThreadPool.QueueUserWorkItem.
An issue occured when we wanted a test with user input where he/she would have to test the Sound/Video and Touch screen of the device. To test this, I have created a secondary WPF window inside one of my plugin classes, so this plugin opens that window, waits for the user input and then closes. This window consists of a label, 3 radio buttons a MediaElement and a few buttons.
When i open up my application, it loads in all my .dll files, I then have the option to press my "Run Tests" button, which runs through a list of my plugins and runs them.
Now because we needed this user interaction thread I needed this to be different than a ThreadPool thread. The code below shows how i start all my threads, the first part is the user interaction plugin. The code is messy because I have tested several solutions now, some I tried to incorporate into others..
if (availablePlugin.RequirementNumber.Equals("13996"))
{
try
{
plugin.Status = VerdictEnum.InProgress;
// var uiThread = new Thread(plugin.TestMethod);
Action startUIThread = () =>
{
Thread uiThread = new Thread(new ThreadStart(() =>
{
SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher));
plugin.TestMethod();
}));
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.IsBackground = true;
uiThread.Start();
uiThread.Join();
};
Dispatcher.CurrentDispatcher.Invoke(startUIThread);
}
catch (Exception ex)
{
uiContext.Post(
(o) => plugin.ErrorDescription += "Test case threw an unexpected exception: " + ex.Message, null);
uiContext.Post((o) => plugin.Status = VerdictEnum.Inconclusive, null);
}
finally
{
//Once the thread has finished it sets its personal ManualResetEvent to true, indicating it has finished its job
numberOfRunningThreads--;
eventlist[localCounter].Set();
Debug.Print("TEST ENDED");
}
}
//All other tests are created as worker threads using the ThreadPool.
else
{
ThreadPool.QueueUserWorkItem(
new WaitCallback(s =>
{
try
{
plugin.Status = VerdictEnum.InProgress;
plugin.TestMethod();
}
catch (Exception ex)
{
uiContext.Post(
(o) => plugin.ErrorDescription += "Test case threw an unexpected exception: " + ex.Message, null);
uiContext.Post((o) => plugin.Status = VerdictEnum.Inconclusive, null);
}
finally
{
//Once the thread has finished it sets its personal ManualResetEvent to true, indicating it has finished its job
numberOfRunningThreads--;
eventlist[localCounter].Set();
Debug.Print("TEST ENDED");
}
}));
}
A look on how i present my window inside the plugin class:
var videoAudioTest = new VideoAudioPopup();
videoAudioTest.Show();
videoAudioTest.ResultReady += videoAudioTest_ResultReady;
// videoAudioTest.Closed += (s, e) => System.Windows.Threading.Dispatcher.ExitAllFrames();
// videoAudioTest.Closed += (s, e) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
videoAudioTest.Closed += (s, e) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
Dispatcher.Run();
And here comes the interesting part. When i run this test on my computer, everything works without any issues. I have never have it fail to load my MediaElement.
When i run my application on the devices that needs to be tested, it will (almost, it has a few times) never work on the FIRST run (First time i press the 'Run tests' button). If i keep the application open, and press the 'Run Tests' button again, it ALWAYS works.
The video it loads for the MediaElement is the W7 standard afaik 'Wildlife.wmv'. The Media player itself never presents any issues when it is being run externally.
I am assuming that this is happening because I am messing something up with my threads? -As said the code is a bit messy right now, I have tried some other methods that has worked on my computer, but it has never worked like intended on the devices, they almost always have trouble loading the MediaElement in the new window.
'numberofrunningthreads' is only present for debug.print purposes.
Any help is very appreciated, if you need any more code snippets, or if I was unclear on anything, please ask and I will respond as fast as I can.