Freezing a computer while cycling a large list of images in C # - Wpf

So, I have a very simple software to call a list of several images
and show them in the format < Next ) + ( Previous ):

enter image description here

and its work is great for me, but when I Hold on the (NEXT) button to quickly transfer all objects , after 10 or 20 elements the whole window freezes and Lag , some recherche say use a desktop background to prevent this, so I tried to paste this :

 var getImage = Directory.EnumerateFiles(DirName, Ext, SearchOption.TopDirectoryOnly); 

inside of this:

 Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => /*### the Images output Here ###*/ )); 

but the same problems that still occur

How to make it work correctly?
and if there is any other way to do this, I will be happy to know about it.

-1
source share
1 answer

Dispatcher.Invoke assigns a delegate to execute on the user interface thread. You do not want to execute any potentially long-term code in the user interface thread, as this is what your application freezes.

If you want to call Directory.EnumerateFiles in the background thread, you can run the task:

 Task.Factory.StartNew(()=> { //get the files on a background thread... return Directory.EnumerateFiles(DirName, Ext, SearchOption.TopDirectoryOnly); }).ContinueWith(task => { //this code runs back on the UI thread IEnumerable<string> theFiles = task.Result; //... }, System.Threading.CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); 

Please note that you cannot access any user interface control in the background thread, although you should only do lengthy work on the background thread, and then you can use the ContinueWith method if you want something with the results to return the user interface thread. for example, to set the ItemsSource property of an ItemsControl element or to set the Visibility property of a ProgressBar to Collapsed or something like that.

+2
source

Source: https://habr.com/ru/post/1265171/


All Articles