0

I have a some code:

    public MainPage()
    {
         InitializeComponent();
         MyPostRequest.GetDataFromService((result) =>
         {
            Dispatcher.BeginInvoke(() => { //Update UI from web service}); 
         });
         System.Threading.Thread myThread = new System.Threading.Thread(new System.Threading.ThreadStart(MyThread));
         myThread.Start();
    }

    private void MyThread()
    {
        //do something
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        Dispatcher.BeginInvoke(() => 
        {
             stopwatch.Stop();
             long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
             Debug.WriteLine("time: {0} ms", elapsedMilliseconds);
            //update UI
        });
    }

Basically, the measured time is about 100 ms, but seldom 1000 - 5000 ms. Before you upgrade UI, I check the data in the local database and the data is sampled. I am confused, what exactly is the delay when switching to the UI thread. In what could be the problem?

Alexandr
  • 1,891
  • 3
  • 32
  • 48
  • 1
    I don't think there is a problem. I think there has to be some delay when sending data across threads. There could even be OS-level delays if there are actually multiple OS threads available. I think the solution is to live in harmony with the fact that Dispatcher.BeginInvoke will execute when it gets around to it. – AndyClaw Jun 13 '13 at 13:29
  • 5000 ms destroy the harmony with OS :) – Alexandr Jun 13 '13 at 13:37
  • The program just starts and possibly not ready yet to execute tasks in the main thread. Try the same after initialization finished - what result do you have? – Alex F Jun 13 '13 at 13:41
  • 1
    This is always going to be slow. The required thread context switch is fairly expensive by itself. Latency is by far the biggest chunk, the UI thread must be idle before it can start executing the delegate target. If it is busy with something else then the invoke request is queued until the UI thread goes idle again and re-enters the dispatcher loop. The code looks a bit fake, I'd assume you are actually using a loop. So in effect you are measuring how long Debug.WriteLine() takes. Which is expensive. – Hans Passant Jun 13 '13 at 14:00
  • Thank all for your ideas. @HansPassant, I don't use loop in my app, but you are right, bit fake here. I update my code here. The fact that I'm using another asynchronous request with callback func. I just forgot about it. It may block a user interface. But I checked the callback function is called after I measure time. Debug.WriteLine() is really so expensive? – Alexandr Jun 13 '13 at 15:33
  • @AlexFarber what do you mean? InitializeComponent()? Sorry, I'm a little misunderstood you. Could you elaborate? – Alexandr Jun 13 '13 at 15:38
  • I mean that when the program just starts, main thread may be busy running some initialization tasks, and BeginInvoke waits in the queue. What happens if the same code runs in a stable state, for example, when some button is clicked? – Alex F Jun 13 '13 at 18:38
  • @AlexFarber sorry for the later answer, I resolved my issue, problem was in other method working in UI thread. Thank. – Alexandr Jun 18 '13 at 14:01

0 Answers0