unosquare / ffmediaelement

FFME: The Advanced WPF MediaElement (based on FFmpeg)
https://unosquare.github.io/ffmediaelement/
Other
1.17k stars 238 forks source link

Loop exit condition in StepTimer.cs #577

Open igvk opened 2 years ago

igvk commented 2 years ago

Issue Title

There is an endless loop in Primitives/StepTimer.cs Presumably, there should be an exit condition in it.

Issue Categories

Version Information

Sample Code

This is the code of the ExecuteCallbacks( function that never returns:

        private static void ExecuteCallbacks(object state)
        {
            while (true)
            {
                TickCount++;
                if (TickCount >= 60)
                {
                    Resolution = TimeSpan.FromMilliseconds(Stopwatch.Elapsed.TotalMilliseconds / TickCount);
                    Stopwatch.Restart();
                    TickCount = 0;

                    // Debug.WriteLine($"Timer Resolution is now {Resolution.TotalMilliseconds}");
                }

                Parallel.ForEach(RegisteredTimers, (t) =>
                {
                    if (t.IsRunningCycle || t.IsDisposing)
                        return;

                    t.IsRunningCycle = true;

                    Task.Run(() =>
                    {
                        try
                        {
                            t.UserCallback?.Invoke();
                        }
                        finally
                        {
                            t.IsRunningCycle = false;
                        }
                    });
                });

                while (PendingAddTimers.TryDequeue(out var addTimer))
                    RegisteredTimers.Add(addTimer);

                while (PendingRemoveTimers.TryDequeue(out var remTimer))
                    RegisteredTimers.Remove(remTimer);

                Task.Delay(Constants.DefaultTimingPeriod).Wait();
            }
        }
Mitra-M commented 2 years ago

I think it is a static Callback method not a bug.

igvk commented 2 years ago

Ok, the problem with this is that there is no way to exit the thread and unload ffme objects when closing video window. This could be ok when this is all that a program does, and closing video means exiting the program.. And what if playing some video file is only one of the functions in software?

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.

jcyuan commented 11 months ago

Ok, the problem with this is that there is no way to exit the thread and unload ffme objects when closing video window. This could be ok when this is all that a program does, and closing video means exiting the program.. And what if playing some video file is only one of the functions in software?

that's a background thread, so if there is no umanaged object in the queue, i think it should be fine.