Results 1 to 9 of 9

Thread: [RESOLVED] How to Reduce CPU usage

  1. #1

    Thread Starter
    Lively Member darkbb's Avatar
    Join Date
    Jan 2016
    Posts
    91

    Resolved [RESOLVED] How to Reduce CPU usage

    Hi, all I just learned Datatables in VB.Net. I implemented it in VB.net App in order to show me the color code of certain software, so that, it would be easy to find the right one and use it. I got a problem in it. This time i used more coding rather than drag&drops. Here's an Screenshot of the app :
    Name:  Sample.jpg
Views: 310
Size:  49.6 KB

    U can see the Stutter right..?? It is caused while dragging... Also the CPU usage pump up to 30-40% of CPU (The project exe file takes upto 20%-25% approx). I don't know how to optimize/reduce the CPU usage. I'm uploading the full project as ZIP with this Thread. Help Please..

    Project Zip :: *Uploaded without bin & obj as Shaggy Hiker instructed
    PrjVJ.zip
    Last edited by darkbb; Oct 1st, 2016 at 10:34 PM. Reason: Reuploaded the attachement to remove exe & dll files as instructed by Shaggy Hiker

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,126

    Re: How to Reduce CPU usage

    Feel free to attach a project, but you must be sure that it does not contain compiled code. Forum rules prohibit compiled code because interesting things can be added to those. Therefore, remove all .exe and .dll files and post just the source code.

    As to the question, it is time to do a bit of profiling. There are profilers built into VS, but it may be easier to just time things yourself using the Stopwatch object. I also have a thin profiler class posted in the .NET CodeBank. The purpose of all of this is to figure out what part of the code is actually taking the time. You may have a fair idea of where the problem lies, but timing different parts will likely allow you to narrow it down a bit more.

    A stutter during dragging probably isn't the dragging itself. The problem, in that case, is largely due to the underlying window taking a long time to redraw itself. So, the problem is likely in the drawing for the underlying control.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member darkbb's Avatar
    Join Date
    Jan 2016
    Posts
    91

    Re: How to Reduce CPU usage

    Quote Originally Posted by Shaggy Hiker View Post
    Feel free to attach a project, but you must be sure that it does not contain compiled code. Forum rules prohibit compiled code because interesting things can be added to those. Therefore, remove all .exe and .dll files and post just the source code.

    As to the question, it is time to do a bit of profiling. There are profilers built into VS, but it may be easier to just time things yourself using the Stopwatch object. I also have a thin profiler class posted in the .NET CodeBank. The purpose of all of this is to figure out what part of the code is actually taking the time. You may have a fair idea of where the problem lies, but timing different parts will likely allow you to narrow it down a bit more.

    A stutter during dragging probably isn't the dragging itself. The problem, in that case, is largely due to the underlying window taking a long time to redraw itself. So, the problem is likely in the drawing for the underlying control.
    Thank you for ur quick reply. Sorry, I've never attached my project before in VBForums. I assure U i willnot do the same in future. I will look into the topic profiling & ur codebank classes.

    Quote Originally Posted by Shaggy Hiker View Post
    A stutter during dragging probably isn't the dragging itself. The problem, in that case, is largely due to the underlying window taking a long time to redraw itself. So, the problem is likely in the drawing for the underlying control.
    Yep... As U say, i can see, its time taken to redraw itself. is there anything else to smoothen the stutter..?? (apart from profiling -- which i understand is for CPU usage)

    and, can i change the looks of TabControl's Gray 3D layout..???

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,126

    Re: How to Reduce CPU usage

    You can attach projects, just remove the .exe and .dll files (there are a few other compiled items, but they aren't usually found in .NET projects). This generally just means removing the bin and obj folders. Those aren't really needed. If you delete them from your project then run it, VS will simply recreate them, so deleting them is something that gets recommended fairly often as solutions to certain types of issues. It may or may not help, but it's easy to do and harmless. So, if you remove those folders, the rest of the project is probably fine.

    As to the stutter, what is that control, and are you painting it? If you are painting it then there is hope. If you are not doing the drawing, the easy thing to try is to see whether DoubleBuffering is set to True (or if it is even visible). Frankly the problem you are talking about doesn't seem like a double buffering issue, but it's just a property for the control, so it's super easy to try out. If the property doesn't exist, then you can expose it easily enough, but it may not be worth that.
    My usual boring signature: Nothing

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: How to Reduce CPU usage

    Hi,

    I had downloaded your project before it got removed and just had a chance to look it over. You came up with a nice visual appearance, but you have a few things working against you.

    The first is that you are using the TabControl. The TabControl has a long history of being a PITA in regards to performance. The worst part is that it does not always cause issues; it appears to be somewhat related to to your graphics card.

    The second issue is that you are effectively creating a grid layout by adding a TableLayoutPanel for each row of your grid and in that TLP you have many nested controls. Overall, there are a ton of controls on your display and all of those are getting drawn.

    I'm thinking that you could achieve a similar look using a DataGridView with custom row painting. I will try to work up an example and post it later on.

  6. #6

    Thread Starter
    Lively Member darkbb's Avatar
    Join Date
    Jan 2016
    Posts
    91

    Re: How to Reduce CPU usage

    Quote Originally Posted by Shaggy Hiker View Post
    You can attach projects, just remove the .exe and .dll files (there are a few other compiled items, but they aren't usually found in .NET projects). This generally just means removing the bin and obj folders. Those aren't really needed. If you delete them from your project then run it, VS will simply recreate them, so deleting them is something that gets recommended fairly often as solutions to certain types of issues. It may or may not help, but it's easy to do and harmless. So, if you remove those folders, the rest of the project is probably fine.
    DONE...!! Re-uploaded the attachment without obj & bin folder.

    Quote Originally Posted by Shaggy Hiker View Post
    As to the stutter, what is that control, and are you painting it? If you are painting it then there is hope. If you are not doing the drawing, the easy thing to try is to see whether DoubleBuffering is set to True (or if it is even visible). Frankly the problem you are talking about doesn't seem like a double buffering issue, but it's just a property for the control, so it's super easy to try out. If the property doesn't exist, then you can expose it easily enough, but it may not be worth that.
    DoubleBuffering?? I'll look into that too.. & reply ASAP.

  7. #7

    Thread Starter
    Lively Member darkbb's Avatar
    Join Date
    Jan 2016
    Posts
    91

    Re: How to Reduce CPU usage

    Quote Originally Posted by TnTinMN View Post
    Hi,

    I had downloaded your project before it got removed and just had a chance to look it over. You came up with a nice visual appearance, but you have a few things working against you.
    Great...!!

    Quote Originally Posted by TnTinMN View Post
    The first is that you are using the TabControl. The TabControl has a long history of being a PITA in regards to performance. The worst part is that it does not always cause issues; it appears to be somewhat related to to your graphics card.
    TabControl as PITA? U might be right, i noticed that while trying to change its visuals too. But, it turns out worse than default layout. and, i dont think its related to graphic card coz, I'm having 16GB RAM, 2GB nVidia Graphics. Does this cause issue..??

    Quote Originally Posted by TnTinMN View Post
    The second issue is that you are effectively creating a grid layout by adding a TableLayoutPanel for each row of your grid and in that TLP you have many nested controls. Overall, there are a ton of controls on your display and all of those are getting drawn.

    I'm thinking that you could achieve a similar look using a DataGridView with custom row painting. I will try to work up an example and post it later on.
    I first looked into DataGridView only (U might find it in FormLoad column). But, I cannot be able to change the cell background color to specific color. The TableLayoutPanel is just a 2X1 table per row.. It might be a problem as you said. Will look into that. Please continue with your workup example. I'm very interested in customizing (Custom Row Painting) of DataGrid View...!!

  8. #8
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: How to Reduce CPU usage

    Give this code a try and see if it helps.

    Call "AddDGV" instead of your "Compnd1" method in your "Form1_Load" method.

    VB.Net Code:
    1. Private Sub AddDGV(dt As DataTable, tp As TabPage)
    2.     Dim dgv As New DataGridView
    3.     AddHandler dgv.RowPrePaint, AddressOf dgvRowPrePaint
    4.  
    5.     With dgv
    6.         .AllowUserToAddRows = False
    7.         .AllowUserToDeleteRows = False
    8.         .AllowUserToResizeColumns = False
    9.         .AllowUserToResizeRows = False
    10.         .AutoGenerateColumns = False
    11.  
    12.         .ScrollBars = ScrollBars.Both
    13.         .Dock = DockStyle.Fill
    14.  
    15.         .RowHeadersVisible = False
    16.         .SelectionMode = DataGridViewSelectionMode.FullRowSelect
    17.  
    18.         .ColumnHeadersVisible = False
    19.         .ColumnCount = 2
    20.         .Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
    21.         .Columns(0).Width = 40
    22.         .Columns(0).CellTemplate.Style.Font = New Drawing.Font("Segoe UI", 7, FontStyle.Bold)
    23.         .Columns(0).DataPropertyName = dt.Columns(4).ColumnName
    24.         .Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    25.         .Columns(1).CellTemplate.Style.Font = New Drawing.Font("Segoe UI", 6)
    26.  
    27.         .RowTemplate.Height = 40
    28.         .Location = Point.Empty
    29.  
    30.         .Parent = tp
    31.         .DataSource = dt
    32.     End With
    33.  
    34. End Sub
    35.  
    36. Private Sub dgvRowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs)
    37.     Static cell0Format As New StringFormat(StringFormat.GenericTypographic) With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center, .FormatFlags = StringFormat.GenericTypographic.FormatFlags Or StringFormatFlags.NoWrap}
    38.  
    39.     If e.RowIndex > -1 Then
    40.         Dim dgv As DataGridView = DirectCast(sender, DataGridView)
    41.         Dim dt As DataTable = DirectCast(dgv.DataSource, DataTable)
    42.         Dim row As DataRow = dt.Rows(e.RowIndex)
    43.         Dim Column0 As DataGridViewColumn = dgv.Columns(0)
    44.         Dim cell0Bounds As Rectangle = dgv.GetCellDisplayRectangle(0, e.RowIndex, False)
    45.         Dim cell1Bounds As Rectangle = dgv.GetCellDisplayRectangle(1, e.RowIndex, False)
    46.         e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceOver
    47.  
    48.         If ContainsAndRemovePart(e.PaintParts, DataGridViewPaintParts.Background) Then
    49.  
    50.             Dim cell0Backcolor As Color = ColorTranslator.FromHtml(row.Item(3).ToString)
    51.             Using br As New SolidBrush(cell0Backcolor)
    52.                 e.Graphics.FillRectangle(br, cell0Bounds)
    53.             End Using
    54.             Using br As New SolidBrush(System.Drawing.Color.FromArgb(83, 83, 83))
    55.                 e.Graphics.FillRectangle(br, cell1Bounds)
    56.             End Using
    57.         End If
    58.         If ContainsAndRemovePart(e.PaintParts, DataGridViewPaintParts.ContentForeground) Then
    59.             Dim cell0Text As String = CStr(row.Item(4))
    60.             Dim textSize As SizeF = e.Graphics.MeasureString(cell0Text, Column0.CellTemplate.Style.Font, Column0.Width, cell0Format)
    61.             Dim textRect As New RectangleF(PointF.Empty, textSize)
    62.             textRect.Inflate(2, 2)
    63.             textRect.X = ((cell0Bounds.Width / 2.0F) - (textRect.Width / 2.0F)) + cell0Bounds.X
    64.             textRect.Y = ((cell0Bounds.Height / 2.0F) - (textRect.Height / 2.0F)) + cell0Bounds.Y
    65.  
    66.             Using br As New SolidBrush(System.Drawing.Color.FromArgb(83, 83, 83))
    67.                 e.Graphics.FillRectangle(br, textRect)
    68.             End Using
    69.             Dim fnt As Font = Column0.CellTemplate.Style.Font
    70.             e.Graphics.DrawString(cell0Text, fnt, Brushes.White, textRect, cell0Format)
    71.             Dim cell1Line1 As String = CStr(row.Item(1))
    72.             Dim linePt As Point = cell1Bounds.Location
    73.             linePt.Offset(5, 5)
    74.             e.Graphics.DrawString(cell1Line1, fnt, Brushes.White, linePt)
    75.  
    76.  
    77.             Dim cell1Line2 As String = CStr(row.Item(2))
    78.             fnt = dgv.Columns(1).CellTemplate.Style.Font
    79.             linePt.Offset(0, fnt.Height + 5)
    80.             e.Graphics.DrawString(cell1Line1, fnt, Brushes.White, linePt)
    81.  
    82.         End If
    83.     End If
    84.  
    85. End Sub
    86.  
    87. Private Function ContainsAndRemovePart(ByRef parts As DataGridViewPaintParts, checkPart As DataGridViewPaintParts) As Boolean
    88.     Dim ret As Boolean = (parts And checkPart) = checkPart
    89.     If ret Then parts = parts And Not checkPart
    90.     Return ret
    91. End Function

  9. #9

    Thread Starter
    Lively Member darkbb's Avatar
    Join Date
    Jan 2016
    Posts
    91

    Re: How to Reduce CPU usage

    Quote Originally Posted by TnTinMN View Post
    Give this code a try and see if it helps.

    Call "AddDGV" instead of your "Compnd1" method in your "Form1_Load" method.
    PERFECT...!! This is the Visual Appearance I've been trying to work on... The code was perfect except cell1Line1 is used instead of cell1Line2

    VB.Net Code:
    1. e.Graphics.DrawString(cell1Line2, fnt, Brushes.White, linePt)]


    Thank you so much, TnTinMN...!! and, the DGV was so perfectly done...!! even the color code inside the color palette... Thank you so much... Have to try another project with your DGV Custom Row Painting..

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width