-1

I have a simple C# .NET WPF app that should display all pictures of a folder, each for half a second using an image element. PollPic is a property (of variable pollPic). currImageFilename was declared above. I aimed to use Invoke/BeginInvoke in order to update the UI. The function where this code belongs to is a async function, that is called (with await) from a button click event.

When I have 6 pictured in the folder, each has been read and and sleep was called 6 times but only the last picture was displayed in the end. Where is my general thinking mistake here?

Thanks everybody.

       if (picPath != "")
        {
            string[] pollPicList = Directory.GetFiles(picPath);
            if (pollPicList.Length > 0)
            {
                for (int i = 0; i < pollPicList.Length; i++)
                {
                    currImageFilename = pollPicList[i];
                    PollPic = new BitmapImage(new Uri(currImageFilename));
                    this.Dispatcher.BeginInvoke(new Action(() => ShowDetectPic(PollPic)));
                    System.Threading.Thread.Sleep(500);

                }
            }
        }

Unsuccessfully tried to use Task.Run instead. Also not working Task t1 = new Task(() => ShowDetectPic(PollPic));

sup-team
  • 41
  • 1
  • 7
ansgar
  • 17
  • 1
    Do you run this part of code on UI thread? What exactly ShowDetectPic does? – emoacht Dec 30 '22 at 00:01
  • *DispatcherTimer* would do the 500msec-scheduling for you, no need to write that code again. https://learn.microsoft.com/de-de/dotnet/api/system.windows.threading.dispatchertimer?view=windowsdesktop-7.0 – lidqy Dec 30 '22 at 00:45
  • If you do Thread.Sleep on the UI Thread that new picture isn't likely rendered properly once it's assigned to an Image source. – lidqy Dec 30 '22 at 00:46
  • Hi, thanks for your fast answers. I have dev experience in other languages but my last contact with c# was around 20 year ago. As I was keen on experimenting with ML.Net, I started with c# again but all the .net/wpf staff is new to me. The funny thing is, that the core functionality (having a ML model that can predict what is shown on a picture) works very well, but I'm heavily stuggeling with "that small things" like interacting with the UI and when thread handling is required or helpful. So, I'm going to check all your answers and hints tonight and will keep you updated. Thanks anyway. – ansgar Dec 30 '22 at 16:34

1 Answers1

0

You can't sleep the GUI thread or nothing will render. Your async code needs to be the waiting, not whatever else it is you're doing there.

    async ... Function()
    {
        string[] pollPicList = Directory.GetFiles(picPath);
        if (pollPicList.Length > 0)
        {
            for (int i = 0; i < pollPicList.Length; i++)
            {
                currImageFilename = pollPicList[i];
                PollPic = new BitmapImage(new Uri(currImageFilename));
                ShowDetectPic(PollPic);
                await Task.Delay(500);
            }
        }
    }
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Thank you so much, that exactly was the cause. Changed it and it works :-) – ansgar Dec 30 '22 at 16:37
  • Perhaps a studid question, I struggle currently a lot with this: What is the best way to update wpf controls - from any thread. Thanks. – ansgar Dec 30 '22 at 16:45
  • From any thread, you need to invoke the change back to the main thread. – Blindy Dec 30 '22 at 19:54
  • how to? call an event on the main thread? – ansgar Dec 30 '22 at 22:57
  • The WPF way is to use [`Dispatcher.BeginInvoke`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher.begininvoke?view=windowsdesktop-7.0) (hence, I said to invoke the change). You can also use the generic .Net TPL to post to the main sync context [`SynchronizationContext.Post`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.synchronizationcontext.post?view=netframework-4.8#system-threading-synchronizationcontext-post(system-threading-sendorpostcallback-system-object)). Same result. – Blindy Dec 30 '22 at 23:04
  • thanks again, that is helpful. so my initial thought was not toooooo far away... – ansgar Dec 31 '22 at 00:04