Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Why UI is freezing?

$
0
0

In my ViewModel I have the following code:

private void ExecuteRefresh()
        {
            IsBusy = true;
            Task.Factory.StartNew(() =>
            {
                transmissionListService.UpdateList();
            })
            .ContinueWith((task) =>
            {
                IsBusy = false;
                if (task.IsFaulted)
                    messageService.ShowError(task.Exception.InnerException);
                else
                    Transmissions = CollectionViewSource.GetDefaultView(transmissionListService.Transmissions);
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }

The transmissionListService.UpdateList():

public void UpdateList()
        {
            Transmissions.Update(webService.GetLiveTransmissions());
            UpdateTransmissionsDiskUsage();
        }

And the webservice.GetLiveTransmissions():

 public IEnumerable<Transmission> GetLiveTransmissions()
        {
            var transmissions = ExecuteUntilItWorks(() => webService.ListaTransmissoesAoVivo());
            return adapter.Adapt<IEnumerable<Transmissao>, IEnumerable<Transmission>>(transmissions);
        }

        private T ExecuteUntilItWorks<T>(Func<T> action)
        {
            Monitor.Enter(locker);

            T output;
            while (true)
            {
                try
                {
                    output= action.Invoke();
                    break;
                }
                catch (SoapException se)
                {
                    if (!IsNoSessionException(se))
                    {
                        Monitor.Exit(locker);
                        throw se;
                    }
                }
                catch (WebException we)
                {
                    //Log.Erro(we);
                    Thread.Sleep(5000);
                }
                catch (Exception e)
                {
                    //Log.Erro(e);
                    //MessageBox.Show("Não foi possível conectar ao servidor.", "Erro de WebService");
                    Monitor.Exit(locker);
                    throw e;
                }
            }

            Monitor.Exit(locker);
            return output;
        }

I know while(true) is really bad, but I didn't wrote it. It's legacy code and I'm stuck with it.

But the thing is that the UI is freezing, despite I'm running this code in another thread, ain't I?


Take a look at WPF FlashMessage
About.me


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>