0

I have a list of 1 to 4 mediaplayer objects depending on how may are required at the time by the user I need to call the Play method on each 1-4 at the same time.

I don't need them to run at the same time in parallel I just need to call the play method so all in the list start at the same time.

At the moment im doing this

    foreach(MyMediaPlayer player in lsPlayers)
     {
       player.Play();
     }

which works ok but I want to know if there is a better way.

I have tried

Parallel.ForEach(ObjectList, (obj) =>
   {
   // Do parallel work here on each object
    });

and Parallel.BeginInvoke

but these all seem to launch in a new thread and that causes an error of trying to access controls on different threads.

I just want to initiate the Play() method at the same time for a possible 1 to 4 media player objects

Manity
  • 21
  • 1
  • 6
  • It isn't possible to call multiple methods at the same time on objects that must be accessed by the UI thread –  Feb 11 '19 at 02:52
  • using your approch you can use a dispatcher to avoid the error with accessing the controls – Denis Schaf Feb 11 '19 at 06:13
  • @Denis Schaf 3 Tried that just could not get it working, the same thread error kept coming up – Manity Feb 11 '19 at 10:02
  • can you paste the error here? – Denis Schaf Feb 11 '19 at 10:05
  • by the way even if you get it to work the UI runs on a single thread so even a dispatcher from 4 parallel threads will have to wait for its turn to access the UI So this only makes sense if your player "wastes" time with non UI related tasks – Denis Schaf Feb 11 '19 at 10:08

1 Answers1

0

Well, you can't run 4 things at the same time in the Main Thread. Imagine it as a stack where each row is processed once a time, in that case each row is a function.

For what you are doing it should be okay to call all four play methods in series, in the Main Thread, since you shouldn't perceive any delay in all 4 players. Threading works better when you have compute-intensive tasks that would block the interface. For that you should refer to this answer: https://stackoverflow.com/a/11625264/11042363