MediaElement/MediaPlayer/MediaUriElement hangs UI thread while loading a video - c#

hey,
i've got a wpf application that have a doubleanimation used for scrolling text and images (scrollbar) from the left side of the screen, to the right, and a movie playing, in the same window.
everytime a video ends, and a new video loads, the doubleanimation get stuck/hangs for a second, then it continues normally. i assume it's the control blocking the ui thread while loading the video. i've tried to create a seperate UI thread for the scrolling bar but i didn't solve the problem. i've tried using MediaElement, MediaPlayer and MediaUriElement control with no success.
any help would be appreciated.

That's a known issue with MediaElement. You pretty much have to preload your videos...which means block the UI thread no, or later ;).
You can also try my MediaUriElement in my WPF MediaKit project. I did as much as possible off thread so this wouldn't happen.
http://wpfmediakit.codeplex.com

Related

WPF Animation does not animate. (Splash screen)

Background:
I'm trying to add an animation to a splash screen window in my WPF project. (Not using the actual SplashScreen control. I use a window.)
My reason for implementing the splash screen is beacuse there's a slight delay when the application is opened, due to the loading of data. Obviously, the splash screen's purpose is to aid the user experience, so the user knows the application has been executed.
Here's the idea behind my current approach:
Instantiate the splashscreen window at the beginning of the
constructor.
InitializeComponent() is called.
Close the splashscreen window.
Unfortunately, the animation in the splash screen doesn't start until after the InitializeComponent() method of the the main parent window is called; this occurs basically when I'd WANT TO close the splashscreen window anyway. Hence the animation is useless and doesn't start until it's too late.
My question:
Why does it not animate until after InitializeComponent() is called?
My current theory for why this happens is that it's because the splash
screen is being opened on the main UI thread. Am I correct?
Are there any workarounds that don't involve hijacking control of
the program and spawning the splashscreen in a new thread? (I don't want a solution that hackish)
And yes, I did browse this site for quite awhile and was unable to find a viable solution.
Processing time on the UI thread blocks the animation. No real way around it.
The best approach is to NOT spend so much time loading and blocking on the UI thread. I suggest that you could delay the loading of the data bound to your UI elements until after the initial load. That way, the InitializeComponents() should not take so long.
Basically, all your bindings should return no data to the controls until AFTER you initialize. This data initialization would preferably happen on a separate thread, and then notification would occur after the data has been loaded.
This would allow your splash screen to animate and you will have much more joy.
There are a few approaches that I've taken to handle this in the past, though #HighCore is right in the comments as well about your current problem
Use the SplashScreen class in the VisualBasic assembly
Use the WPF Splash Screen class
Regardless, these solutions boil down to threading, since the UI pipeline needs to render and you can't inherently do these things on the UI thread without blocking, particularly if you want animation.

Show Progress Circle when data is loaded in View in WPF

I want to show an Progress Circle, whenever my UI is loading something.
I tried using an boolean in the shellView and setting it true or false.
IsBusy=true;
But as I am using Caliburn Micro for MVVM, the View is not showing the Progress Circle, since the UI freezes on background work. I tried using Background worker, But not working.
I want to show an Progress Indicator when ever my View is busy. May be loading an ComboBox or doing some background task.
If you're doing background tasks (such as retrieving data from Web Services), you can use BackgroundWorker or Task.Factory.StartNew() in .Net 4.0 to perform that operation in a separate thread (as opposed to doing it in the so-called "UI Thread"). This will allow your View to remain responsive while performing the background operations.
Now, in the case of loading the view itself, you cannot do that in a separate thread, and therefore there's no way to prevent the UI from freezing a little moment until its completely loaded.
So, an option is to create an "overlay window" in a separate thread (thus having its own dispatcher), which would be a transparent window placed right "above" (in Z-order) of the window currently loading. The overlay window remains responsive because it has its own dispatcher, so you can show the Loading indicator or animation, and remove it when loading is complete.
Here is an example of what I mean.

C# Show a progress bar in a popup window

I need to have a progress bar in a popup window, while my main thread is doing some heavy calculations.
I'm using an Oracle API , all the heavy work is done calling 1 function of this API, so I can't use the backgroudworker update becasue I don't have the API's code.
I tried to create a new form with an ProgressBar and a Timer, but the timer_Click event is never fired!!(???)
Any Idea??
while my main thread is doing some heavy calculations.
Wrong. Never do any heavy calculations inside your main thread in a Windows application or your users will simply hate you.
If your API doesn't provide a way to report the progress then the best you could do is show some rotating animation while the calculation is in progress because you will not be able to know the exact percentage of work done. So put the calculation inside a BackgroundWorker, show some spinning animation before running the calculation (which should itself be done inside the DoWork event) and hide the animation once the work is done (RunWorkerCompletedEvent).
The ProgressBar has a Style property that you could set to Marquee to emulate an infinite progression.

WPF Animated ProgressBar in WinForms application

I have a WinForms application, and wanted to add some nice WPF controls, one of them being an 'indeterminate' progress bar.. which just animates nicely until I tell it to stop.
I have done this, and was racking my brains as to why it wasn't animating (changing the value of the progress bar).
I eventually tried showing my form (containing the ElementHost) modally, and hey presto, it animates, but I want to show the form non-modally, as I want to continue processing behind the scenes.
Is there some kind of setting that tells the ElementHost window to continue 'animating'?
Thanks
Rich.
There are many ways to do this , the simplest one is to use a backgroundworker for the lengthy task. The Backgroundworker has an event to report progress. Handle this event and in the handler change your progressbar's value. Just having an animation in the main thread while still doing work on the main thread will not work right ...
One other aproach you may try (though is not what I would recommend for a healty app) is to implement a DoEvents function and call it in the main thread when you want the progress bar to get updated ... Here is the link for the DoEvents implementation:
MSDN DoEvents sugestion
I would Strongly recommend the first approach though
As I mention in my comment above, the solution is to run the processing in a separte thread, which allows the .net Main GUI thread do its stuff, and animate the progress bar.

How to show a splash screen to kill the time while data is loaded?

When starting my app I at first have to read in some data, have to init some forms and so on.
For that time the user sees just grey getting-ready to show something forms.
This lasts for a few seconds...
I thought of a Splash Screen that loads the data in a seperate Thread and also shows how long it will take. Or just a status bar?
How would you do something like this?
I'm using C# .NET 3.5 + Winforms
See this CodeProject article: A Pretty Good Splash Screen in C#
It implements a splash screen that:
Runs on a separate thread
Fades in as it appears, and fade out as it disappears
Displays a running status message that is updated using a static method
Displays and update a predictive self-calibrating owner-drawn smooth-gradient progress bar
Displays the number of seconds remaining before load is complete
Here's an example of what it looks like - maybe yours will be prettier. 8-)
With WindowsForm, the easier is to use Backgroundworker.
You can disables controls during loading and display a progress bar on startus bar with label as "loading data...".
MSDN link : http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
If it only lasts a few seconds then displaying the wait
cursor should be fine. Perhaps it can be arranged that the
main window is shown as quickly as possible and the rest is
started after the first screen update (e.g. using a timer).
This will reduce the perceived start-up time.
In order to reduce the startup time you may also consider
postponing some of startup actions if they are not strictly
necessary. It can be done later in the background using a
timer or on-demand.
I recently wrote a similar splash screen using Tom Clements as a basis. Take a look at My Splash Screen to see if it fits your needs.

Categories