-2

sometime is ok but sometime is very slow. why?

void function(PassVehicleInfo vehicle)
{   //a
    System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
    {//b
        //do something
    }));
 }

run from a to b sometimes cost 1xx ms,sometimes cost 8 s.

AlgerDu
  • 21
  • 4
  • A *firehose bug* is the 3rd most common threading bug. About as hard to diagnose, you can look at the code for ages but never see it. But certainly noticeable when the UI thread starts burning 100% core. Review all the code that uses BeginInvoke, you have to slow it down and/or chunk it more. The human eye is easy to please, updating the UI more than 50 times per second is wasted effort. – Hans Passant Aug 13 '18 at 13:50

2 Answers2

1

BeginInvoke puts your action into a queue to be processed by the UI thread.

If UI thread is busy with other stuff (rendering heavy UI), or has a long queue of other actions to invoke (most likely), latency of every action grows.

Pavel Tupitsyn
  • 8,393
  • 3
  • 22
  • 44
1

I had found what makes this problem. Today, I debug my program`s threads by vs. Finally I found why the UI thread blocked.

Do not communicate with server in the UI thread. It`s very unexpected.

Thanks very much for other`s.

AlgerDu
  • 21
  • 4