8 Replies - 848 Views - Last Post: 17 August 2017 - 10:41 AM Rate Topic: -----

#1 Jamshidm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-August 16

Why my winform slows down and goes fast every 2-3 seconds?!

Posted 14 August 2017 - 01:37 PM

Hi there,

I have created a winform application and I have used Direct Bitmap class that mentioned here My link
The problem is this: my whole application (including graphs, text showing on labels and so on) slows down every few seconds and then goes fast again!!!
I draw a bitmap on a picturebox. Specially the picturebox image changing goes slow.
This is a chunk of my code that draws a bitmap on picturebox:
  private void DrawFrame()
        {
            //
            _bitmap = new DirectBitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = _bitmap.Bitmap;
            //
            double x = 0;
            double y = 0;

            while (true)
            {
                Draw_tr_lock.WaitOne();
                //
                for (int i = 0; i < pCount; i++)
                {
                    _bitmap.Bits[indexArray[i]] = gainArray[i];
                }
                //
                using (var path2 = new System.Drawing.Drawing2D.GraphicsPath())
                    {
                        path2.AddPie(0, 0, pictureBox1.Width, pictureBox1.Height, (int)angleValue - 95, 15);
                    using (var region = new Region(path2))
                        {
                            pictureBox1.Invalidate(region);
                        }
                    }
                //}
                //pictureBox1.Invalidate();
                //Thread.Sleep(1);
            }
        }



Do you have any suggestion?
What can cause this problem in general?

Is This A Good Question/Topic? 0
  • +

Replies To: Why my winform slows down and goes fast every 2-3 seconds?!

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13390
  • View blog
  • Posts: 53,434
  • Joined: 12-June 08

Re: Why my winform slows down and goes fast every 2-3 seconds?!

Posted 14 August 2017 - 02:00 PM

My guess is that infinite loop maybe be bogging itself down.. and when new memory comes available it eats it up until there isn't.. and so on.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,827
  • Joined: 05-May 12

Re: Why my winform slows down and goes fast every 2-3 seconds?!

Posted 14 August 2017 - 09:42 PM

So you acquire a lock/resource/mutex/semaphore on line 12. If somebody else has it, you will be forced to wait.

Also in the code you showed above, it doesn't look like you ever release the lock/resource/mutex/semaphore.
Was This Post Helpful? 0
  • +
  • -

#4 Jamshidm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-August 16

Re: Why my winform slows down and goes fast every 2-3 seconds?!

Posted 15 August 2017 - 10:45 AM

Thanks for your reply.
@Skydiver
The line 12 is released by another thread that receives data from Tcp socket.

This post has been edited by Jamshidm: 15 August 2017 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,827
  • Joined: 05-May 12

Re: Why my winform slows down and goes fast every 2-3 seconds?!

Posted 15 August 2017 - 12:30 PM

And which thread is this DrawFrame() function running on?
Was This Post Helpful? 0
  • +
  • -

#6 Jamshidm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-August 16

Re: Why my winform slows down and goes fast every 2-3 seconds?!

Posted 15 August 2017 - 12:58 PM

This function is a thread function.
tr_DrawFrame = new Thread(new ThreadStart(DrawFrame));
            tr_DrawFrame.IsBackground = true;
tr_DrawFrame.Start();

This post has been edited by Jamshidm: 15 August 2017 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,827
  • Joined: 05-May 12

Re: Why my winform slows down and goes fast every 2-3 seconds?!

Posted 15 August 2017 - 05:16 PM

That DrawFrame() is not drawing the picture box. All you are doing there is updating the bits of the image, and then marking the client rectangle for the picture box as invalid. The picture box draws on the UI thread when told by Windows that parts of its client rectangle was invalidated.

So either you are doing too much work on your UI thread and therefore starving the paint event, or you are not feeding releasing that mutex at a constant rate to feed image updates at a constant rate. Also, recall that in Windows, the WM_PAINT message has lower priority than other messages.

You will have to add extra tracing into your code to determine how often, and how long each of your components are taking to do their work. For example, just after your WaitOne(), you would need to log what the current tick count is, and the difference since the previous tick count of that line being hit. You shoulf also measure how long that for loop and succeeding using block is taking. You would do the same for your other thread that listens for data from your TCPIP socket. You may have to register for the picture box's Paint event to see how often it is getting called and how consistently.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,827
  • Joined: 05-May 12

Re: Why my winform slows down and goes fast every 2-3 seconds?!

Posted 16 August 2017 - 08:34 AM

modi123_1 maybe also be correct regarding garbage collection getting in your way. If he is correct, I find it sad though that your collections are happening that often. Anyway, to determine if it truly is the garbage collection, you don't even have to modify your existing code. You will just need to use perfmon.exe and look at the garbage collection related performance counters under the .NET Framework category.
Was This Post Helpful? 0
  • +
  • -

#9 Jamshidm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-August 16

Re: Why my winform slows down and goes fast every 2-3 seconds?!

Posted 17 August 2017 - 10:41 AM

Thank you so much, dear Skydriver.
I'll try your suggestions.

This post has been edited by Jamshidm: 17 August 2017 - 10:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1