0

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.

Kenneth
  • 9
  • 2

1 Answers1

0

Finally figured out what was wrong. I have been focusing too much on the fact that I thought I had errors with how i control my threads. It seems to be a CPU issue on the device the test needs to run on. It appears that sometimes the MediaElement will not be created if it does not have enough CPU available, so the test video i was using was simply too high of a quality so it only worked some of the times, and for some reason the CPU usage is always a bit lower the second time the test runs.

I'll leave the question in case someone else happens to have issues with the MediaElement not loading. I used this program to stress my CPU so i can reliably make my program 'crash' the MediaElement to verify that it is when the CPU load is too high that it will not create the element. (Depending on the quality of the video)

http://alax.info/blog/1342/comment-page-1

This helped me to my conclusion, when a user was using multiple MediaElements and ran into the same issue as me, where the above software was also linked by another user 'Roman R': Black video using multiple instances VMR 9

Community
  • 1
  • 1
Kenneth
  • 9
  • 2