Stream starts slowly when Invoke UI-Element

I am programming a test tool that reads a bunch of variables from a local server in a stream.

            int countReads = 1000;

            Int64 count = 0;

            for (int i = 0; i < countReads; i++)
            {
                Thread.CurrentThread.Priority = ThreadPriority.Highest;
                DateTime start = DateTime.Now;

                session.Read(null, 0, TimestampsToReturn.Neither, idCollection, out ReadResults, out diagnosticInfos);

                DateTime stop = DateTime.Now;
                Thread.CurrentThread.Priority = ThreadPriority.Normal;

                TimeSpan delay = (stop - start);

                double s = delay.TotalMilliseconds;
                count += (Int64)s;

                Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
                {
                    progressBar1.Value = i;
                }));
            }

            double avg = (double)count / countReads;

            Dispatcher.Invoke(DispatcherPriority.Input, new Action(() =>
            {
                listBox1.Items.Add(avg);
            }));

I calculate the time it took to continue reading and get the average amount of time at the end.

            DateTime start = DateTime.Now;

            session.Read(null, 0, TimestampsToReturn.Neither, idCollection, out ReadResults, out diagnosticInfos);

            DateTime stop = DateTime.Now

if I run the code without updating the control panel, it took about 5 ms. but if I ran it using

            Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
            {
                progressBar1.Value = i;
            }));

It takes about 10 ms on average.

My question is why is this time longer when using progressbar? I am just calculating the time to read. Not including progress bar update.

Is there a way to evacuate the ui-picture so that it does not change the reading time?

Thank you for your help.

Regards

+3
source share
3 answers

Invoke . . , , . Invoke - - , ( ) .

.

  • , Invoke , .
  • , , . , .
  • , .

, , session.Read . push ( Invoke) pull ( ). , . , , .

+7

MSDN Dispatcher.Invoke

, .

, , Dispatcher.Invoke , .

Dispatcher.BeginInvoke.

+1

If the current executable thread is associated with the Dispatcher that you are using, it will Invoke()block this thread, so in this case, try using Dispatcher.BeginInvoke (), it will do the job asynchronously.

MSDN, Dispatcher.Invoke Method :

Invoke is a synchronous operation; therefore, control will not return to the caller until the callback returns.

BTW, an interesting example DispatcherPriority.Send

0
source

All Articles