Results 1 to 24 of 24

Thread: increasing stack size on my ui thread

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Question increasing stack size on my ui thread

    how can i increase the stack size on my ui thread from within my app?
    my app recursively draws onto a bitmap which is then displayed in a picturebox + only causes an error when i increase the size of the bitmap. i.e. the same drawing operation on a smaller bitmap works ok.

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: increasing stack size on my ui thread

    I think you'd need to edit the MSIL to do that... but anyways, if you're overflowing the stack, you need to change your code to use loops instead of recursion (or something else). Can you post your code, please? Or at least the recursively called part?

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: increasing stack size on my ui thread

    ok here's the code:

    vb Code:
    1. Private Sub picPaint_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picPaint.MouseDown
    2.  
    3.     mousePos = New Point(((picPaint.AutoScrollOffset.X + e.X) - (picPaint.AutoScrollOffset.X + e.X) Mod zoomFactor), ((picPaint.AutoScrollOffset.Y + e.Y) - (picPaint.AutoScrollOffset.Y + e.Y) Mod zoomFactor))
    4.     'Stop
    5.     lorButton = e.Button
    6.     Select Case selectedTool
    7.       Case 7 'rectangle
    8.         startPos = New Point(mousePos.X, mousePos.Y)
    9.  
    10.         undoStack.Push(DirectCast(pic.Clone, Bitmap))
    11.         RibbonButton32.Enabled = CBool(undoStack.Count)
    12.  
    13.     End Select
    14.  
    15.     Application.DoEvents()
    16.  
    17. End Sub
    18.  
    19. Private Sub picPaint_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picPaint.MouseMove
    20.  
    21.     mousePos = New Point(((picPaint.AutoScrollOffset.X + e.X) - (picPaint.AutoScrollOffset.X + e.X) Mod zoomFactor), ((picPaint.AutoScrollOffset.Y + e.Y) - (picPaint.AutoScrollOffset.Y + e.Y) Mod zoomFactor))
    22.     lblCoordinates.Text = mousePos.X & "," & mousePos.Y
    23.  
    24.     objectSize = Nothing
    25.  
    26.     Select Case selectedTool
    27.      
    28.       Case 7 'rectangle
    29.         If startPos <> Nothing Then
    30.           tempPic = DirectCast(pic.Clone, Bitmap)
    31.           Dim gr As Graphics = Graphics.FromImage(tempPic)
    32.           Dim r As Rectangle
    33.           If fillStyle = 1 Then
    34.             Dim color1 As Color
    35.             If lorButton = Windows.Forms.MouseButtons.Left Then
    36.               color1 = leftColor
    37.             ElseIf lorButton = Windows.Forms.MouseButtons.Right Then
    38.               color1 = rightColor
    39.             End If
    40.  
    41.             If mousePos.X > startPos.X And mousePos.Y > startPos.Y Then
    42.               r = New Rectangle(startPos.X, startPos.Y, mousePos.X - startPos.X, mousePos.Y - startPos.Y)
    43.               gr.DrawRectangle(New Pen(color1, lineWidth * zoomFactor), r)
    44.             ElseIf mousePos.X < startPos.X And mousePos.Y > startPos.Y Then
    45.               r = New Rectangle(mousePos.X, startPos.Y, startPos.X - mousePos.X, mousePos.Y - startPos.Y)
    46.               gr.DrawRectangle(New Pen(color1, lineWidth * zoomFactor), r)
    47.             ElseIf mousePos.X > startPos.X And mousePos.Y < startPos.Y Then
    48.               r = New Rectangle(startPos.X, mousePos.Y, mousePos.X - startPos.X, startPos.Y - mousePos.Y)
    49.               gr.DrawRectangle(New Pen(color1, lineWidth * zoomFactor), r)
    50.             ElseIf mousePos.X < startPos.X And mousePos.Y < startPos.Y Then
    51.               r = New Rectangle(mousePos.X, mousePos.Y, startPos.X - mousePos.X, startPos.Y - mousePos.Y)
    52.               gr.DrawRectangle(New Pen(color1, lineWidth * zoomFactor), r)
    53.             End If
    54.           ElseIf fillStyle = 2 Then
    55.             Dim color1 As Color
    56.             Dim color2 As Color
    57.             If lorButton = Windows.Forms.MouseButtons.Left Then
    58.               color1 = leftColor
    59.               color2 = rightColor
    60.             ElseIf lorButton = Windows.Forms.MouseButtons.Right Then
    61.               color1 = rightColor
    62.               color2 = leftColor
    63.             End If
    64.             If mousePos.X > startPos.X And mousePos.Y > startPos.Y Then
    65.               r = New Rectangle(startPos.X, startPos.Y, mousePos.X - startPos.X, mousePos.Y - startPos.Y)
    66.               gr.FillRectangle(New SolidBrush(color2), New Rectangle(startPos.X, startPos.Y, mousePos.X - startPos.X, mousePos.Y - startPos.Y))
    67.               gr.DrawRectangle(New Pen(color1, lineWidth * zoomFactor), r)
    68.             ElseIf mousePos.X < startPos.X And mousePos.Y > startPos.Y Then
    69.               r = New Rectangle(mousePos.X, startPos.Y, startPos.X - mousePos.X, mousePos.Y - startPos.Y)
    70.               gr.FillRectangle(New SolidBrush(color2), New Rectangle(mousePos.X, startPos.Y, startPos.X - mousePos.X, mousePos.Y - startPos.Y))
    71.               gr.DrawRectangle(New Pen(color1, lineWidth * zoomFactor), r)
    72.             ElseIf mousePos.X > startPos.X And mousePos.Y < startPos.Y Then
    73.               r = New Rectangle(startPos.X, mousePos.Y, mousePos.X - startPos.X, startPos.Y - mousePos.Y)
    74.               gr.FillRectangle(New SolidBrush(color2), New Rectangle(startPos.X, mousePos.Y, mousePos.X - startPos.X, startPos.Y - mousePos.Y))
    75.               gr.DrawRectangle(New Pen(color1, lineWidth * zoomFactor), r)
    76.             ElseIf mousePos.X < startPos.X And mousePos.Y < startPos.Y Then
    77.               r = New Rectangle(mousePos.X, mousePos.Y, startPos.X - mousePos.X, startPos.Y - mousePos.Y)
    78.               gr.FillRectangle(New SolidBrush(color2), New Rectangle(mousePos.X, mousePos.Y, startPos.X - mousePos.X, startPos.Y - mousePos.Y))
    79.               gr.DrawRectangle(New Pen(color1, lineWidth * zoomFactor), r)
    80.             End If
    81.           ElseIf fillStyle = 3 Then
    82.             Dim color1 As Color
    83.             If lorButton = Windows.Forms.MouseButtons.Left Then
    84.               color1 = leftColor
    85.             ElseIf lorButton = Windows.Forms.MouseButtons.Right Then
    86.               color1 = rightColor
    87.             End If
    88.             If mousePos.X > startPos.X And mousePos.Y > startPos.Y Then
    89.               r = New Rectangle(startPos.X, startPos.Y, mousePos.X - startPos.X, mousePos.Y - startPos.Y)
    90.               gr.FillRectangle(New SolidBrush(color1), r)
    91.             ElseIf mousePos.X < startPos.X And mousePos.Y > startPos.Y Then
    92.               r = New Rectangle(mousePos.X, startPos.Y, startPos.X - mousePos.X, mousePos.Y - startPos.Y)
    93.               gr.FillRectangle(New SolidBrush(color1), r)
    94.             ElseIf mousePos.X > startPos.X And mousePos.Y < startPos.Y Then
    95.               r = New Rectangle(startPos.X, mousePos.Y, mousePos.X - startPos.X, startPos.Y - mousePos.Y)
    96.               gr.FillRectangle(New SolidBrush(color1), r)
    97.             ElseIf mousePos.X < startPos.X And mousePos.Y < startPos.Y Then
    98.               r = New Rectangle(mousePos.X, mousePos.Y, startPos.X - mousePos.X, startPos.Y - mousePos.Y)
    99.               gr.FillRectangle(New SolidBrush(color1), r)
    100.             End If
    101.           End If
    102.           objectSize = New Size(r.Width, r.Height)
    103.           lblSize.Text = objectSize.Width & "," & objectSize.Height
    104.           picPaint.Image = tempPic
    105.         End If
    106.      
    107.     End Select
    108.  
    109.     Application.DoEvents()
    110.  
    111. End Sub

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: increasing stack size on my ui thread

    Does the Application.Doevents have to exist? Your already in an event and have no code after to release for so whats the purpose?

  5. #5
    Member
    Join Date
    May 2010
    Posts
    60

    Re: increasing stack size on my ui thread

    remove the doevents

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: increasing stack size on my ui thread

    The Application.DoEvents would make sense if the method were called recursively, but I'm missing the recursion... could you highlight the line, please?

  7. #7

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: increasing stack size on my ui thread

    the recursion is in the mousemove event which fires repeatedly

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: increasing stack size on my ui thread

    That is normal, the mouse event will fire near all the time, its something that is a real pain in debugging. There should be no recursion, as in, the mouseevent should not be calling the mouse event and its not. Because you have a doevents in your likely to be running the same event back-to-back, (your blocking the first event to allow the 2nd, which blocks to allow the 3rd) which as been suggested you should take out unless you have a specific reason and completly understand where to use it. Its not recursion your seeing, its bad use for the doevents .

  9. #9
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: increasing stack size on my ui thread

    Ah, so not recursion I get it now.

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: increasing stack size on my ui thread

    I also suggest that you change the undo system to resemble Paint.NET's (save to disk, not to memory).

  11. #11

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: increasing stack size on my ui thread

    Quote Originally Posted by Grimfort View Post
    That is normal, the mouse event will fire near all the time, its something that is a real pain in debugging. There should be no recursion, as in, the mouseevent should not be calling the mouse event and its not. Because you have a doevents in your likely to be running the same event back-to-back, (your blocking the first event to allow the 2nd, which blocks to allow the 3rd) which as been suggested you should take out unless you have a specific reason and completly understand where to use it. Its not recursion your seeing, its bad use for the doevents .
    i removed the doevents. same problem. i'm looking at increasing the stack size for the app with editbin.exe

  12. #12

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: increasing stack size on my ui thread

    Quote Originally Posted by minitech View Post
    I also suggest that you change the undo system to resemble Paint.NET's (save to disk, not to memory).
    that's not the problem. i isolated the drawing in a new project + removed any unnecessary code + it still happens

  13. #13
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: increasing stack size on my ui thread

    Use ilasm/ildasm. But, what else is recursive??! What is the exception? System.StackOverflowException or something else?

  14. #14

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: increasing stack size on my ui thread

    System.StackOverflowException

    it is recursive because it redraws the rectangle everytime i move the mouse. it's definitely a stack size problem as it doesn't happen with a smaller bitmap

  15. #15
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: increasing stack size on my ui thread

    Oh and I'm 100&#37; sure you don't need to edit the IL because I've made a photo editor that actually returned a Bitmap object every time the mOuse moved and it didn't even run slowly.

  16. #16
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: increasing stack size on my ui thread

    Quote Originally Posted by .paul. View Post
    System.StackOverflowException

    it is recursive because it redraws the rectangle everytime i move the mouse. it's definitely a stack size problem as it doesn't happen with a smaller bitmap
    That is not recursion.

  17. #17

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: increasing stack size on my ui thread

    Quote Originally Posted by minitech View Post
    Oh and I'm 100% sure you don't need to edit the IL because I've made a photo editor that actually returned a Bitmap object every time the mOuse moved and it didn't even run slowly.
    ok. try it + see...
    Attached Files Attached Files

  18. #18
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: increasing stack size on my ui thread

    Show us the exception and call stack

  19. #19
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: increasing stack size on my ui thread

    I won't try it and see, because I trust that you're not lying about the error and I can't download zip files on my iPod Touch I'm just saying that I've made a graphics program before similar to this and it works fine. It has slightly less effecient design than yours because I want it to be extensible. It returns a whole new Bitmap object each time with rectangles drawn "recursively" on it and it doesn't overflow the stack. It's probably just a coding problem.

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: increasing stack size on my ui thread

    That isn't recursion, at least any typical recursion. When the exception occurs, take a look at the call stack. Post a screenshot of it, if you can, but take a look at it. There should be one function on there MANY times. Which function is it?
    My usual boring signature: Nothing

  21. #21

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: increasing stack size on my ui thread

    i can't recreate the exception now. i decicded there was no easy answer + i'm trying a different tack.

    thanks to everyone who answered.

  22. #22
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: increasing stack size on my ui thread

    Quote Originally Posted by .paul. View Post
    i can't recreate the exception now
    Does that not mean that removing doevents might have fixed it

  23. #23

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: increasing stack size on my ui thread

    Quote Originally Posted by Grimfort View Post
    Does that not mean that removing doevents might have fixed it
    i did remove doevents, as i told you in post #11
    try the simplified app i posted in post #17. i guarantee you won't fix it

  24. #24
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: increasing stack size on my ui thread

    I can't recreate it.

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