Increasing Stack Size for UI Thread: C# WinForms Application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cmFuZHkxMjAw?=

    #1

    Increasing Stack Size for UI Thread: C# WinForms Application

    Visual Studio 2005, C# WinForms application:

    Here’s the question: How can I increase the standard 1 MB stack size of the
    UI thread in a C# WinForms application?

    Here’s why I ask:

    I’ve inherited some code that at the view (User Interface) layer kicks off a
    background worker thread. At the service layer (think CAB service layer),
    there’s quite a lot of the following:

    worker.ReportPr ogress(n, new string[] { cat, mesg });

    The _BackgroundWork er_ProgressChan ged() event gets these events, and does a
    large amount of string processing. When the user chooses to process all
    available data, the _ProgressChange d() event eventually throws a
    StackOverflow exception with the following in the details section:

    "Cannot evaluate expression because the current thread is in a stack
    overflow state."

    If I take the string processing out of the _ProgressChange d() event, I don’t
    get the StackOverflow, so I’m pretty confident that I’ve isolated the
    problem. Increasing the stack size if definitely a temp fix, but something I
    need to consider in the short term.

    Thanks,
    Randy

  • Willy Denoyette [MVP]

    #2
    Re: Increasing Stack Size for UI Thread: C# WinForms Application

    "randy1200" <randy1200@disc ussions.microso ft.comwrote in message
    news:61025FB2-9D3A-4478-A0B7-BE61DE78F7C8@mi crosoft.com...
    Visual Studio 2005, C# WinForms application:
    >
    Here’s the question: How can I increase the standard 1 MB stack size of
    the
    UI thread in a C# WinForms application?
    >
    Here’s why I ask:
    >
    I’ve inherited some code that at the view (User Interface) layer kicks off
    a
    background worker thread. At the service layer (think CAB service layer),
    there’s quite a lot of the following:
    >
    worker.ReportPr ogress(n, new string[] { cat, mesg });
    >
    The _BackgroundWork er_ProgressChan ged() event gets these events, and does
    a
    large amount of string processing. When the user chooses to process all
    available data, the _ProgressChange d() event eventually throws a
    StackOverflow exception with the following in the details section:
    >
    "Cannot evaluate expression because the current thread is in a stack
    overflow state."
    >
    If I take the string processing out of the _ProgressChange d() event, I don’t
    get the StackOverflow, so I’m pretty confident that I’ve isolated the
    problem. Increasing the stack size if definitely a temp fix, but something
    I
    need to consider in the short term.
    >
    Thanks,
    Randy
    >

    Using "editbin /stack" allows you to set the stack size for all threads in
    the process.
    running "editbin /stack:2000000 some.exe"
    will set the default stack (reserve) to 2000000 bytes.

    Willy.



    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Increasing Stack Size for UI Thread: C# WinForms Application

      Randy,

      Do you have any recursive calls here? The StackOverflowEx ception occurs
      when you have a very deep call stack, and I doubt that you are doing
      non-recursive calls which are testing this limit.

      I am almost certain it is a recursion issue, and if you find where you
      are making an unbounded recursive call, you will fix the issue.

      Assuming this is the case, increasing the call stack in this case will
      do nothing for you.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "randy1200" <randy1200@disc ussions.microso ft.comwrote in message
      news:61025FB2-9D3A-4478-A0B7-BE61DE78F7C8@mi crosoft.com...
      Visual Studio 2005, C# WinForms application:
      >
      Here's the question: How can I increase the standard 1 MB stack size of
      the
      UI thread in a C# WinForms application?
      >
      Here's why I ask:
      >
      I've inherited some code that at the view (User Interface) layer kicks off
      a
      background worker thread. At the service layer (think CAB service layer),
      there's quite a lot of the following:
      >
      worker.ReportPr ogress(n, new string[] { cat, mesg });
      >
      The _BackgroundWork er_ProgressChan ged() event gets these events, and does
      a
      large amount of string processing. When the user chooses to process all
      available data, the _ProgressChange d() event eventually throws a
      StackOverflow exception with the following in the details section:
      >
      "Cannot evaluate expression because the current thread is in a stack
      overflow state."
      >
      If I take the string processing out of the _ProgressChange d() event, I don't
      get the StackOverflow, so I'm pretty confident that I've isolated the
      problem. Increasing the stack size if definitely a temp fix, but something
      I
      need to consider in the short term.
      >
      Thanks,
      Randy
      >

      Comment

      • =?Utf-8?B?cmFuZHkxMjAw?=

        #4
        Re: Increasing Stack Size for UI Thread: C# WinForms Application

        That was exactly what I needed. Many thanks!

        Randy

        "Willy Denoyette [MVP]" wrote:
        "randy1200" <randy1200@disc ussions.microso ft.comwrote in message
        news:61025FB2-9D3A-4478-A0B7-BE61DE78F7C8@mi crosoft.com...
        Visual Studio 2005, C# WinForms application:

        Here’s the question: How can I increase the standard 1 MB stack size of
        the
        UI thread in a C# WinForms application?

        Here’s why I ask:

        I’ve inherited some code that at the view (User Interface) layer kicks off
        a
        background worker thread. At the service layer (think CAB service layer),
        there’s quite a lot of the following:

        worker.ReportPr ogress(n, new string[] { cat, mesg });

        The _BackgroundWork er_ProgressChan ged() event gets these events, and does
        a
        large amount of string processing. When the user chooses to process all
        available data, the _ProgressChange d() event eventually throws a
        StackOverflow exception with the following in the details section:

        "Cannot evaluate expression because the current thread is in a stack
        overflow state."

        If I take the string processing out of the _ProgressChange d() event, I don’t
        get the StackOverflow, so I’m pretty confident that I’ve isolated the
        problem. Increasing the stack size if definitely a temp fix, but something
        I
        need to consider in the short term.

        Thanks,
        Randy
        >
        >
        Using "editbin /stack" allows you to set the stack size for all threads in
        the process.
        running "editbin /stack:2000000 some.exe"
        will set the default stack (reserve) to 2000000 bytes.
        >
        Willy.
        >
        >
        >
        >

        Comment

        • =?Utf-8?B?cmFuZHkxMjAw?=

          #5
          Re: Increasing Stack Size for UI Thread: C# WinForms Application

          Many thanks for the response. Increasing the stack size did eliminate the
          StackOverflow exception. When I reset the stack size back to 1 MB using
          editbin and reran, the stack overflow exception returned.

          To widen the scope a bit, the background_work er thread actually passes a
          list of "customers" to a be queued to a threadpool to be "processed. " The
          threadpool does a great job of throttling itself as it works through the
          list. When the threadpool threads complete, a lot (many hundreds) of
          worker.ReportPr ogress() calls are sending text back to
          _BackgroundWork er_ProgressChan ged(), which is in turn doing a lot of string
          processing. I think ultimately, the right answer is to do all the string
          processing on the threadpool threads. _BackgroundWork er_ProgressChan ged()
          does not seem to have any problems if it's only task is to assign text to a
          UI display control.



          "Nicholas Paldino [.NET/C# MVP]" wrote:
          Randy,
          >
          Do you have any recursive calls here? The StackOverflowEx ception occurs
          when you have a very deep call stack, and I doubt that you are doing
          non-recursive calls which are testing this limit.
          >
          I am almost certain it is a recursion issue, and if you find where you
          are making an unbounded recursive call, you will fix the issue.
          >
          Assuming this is the case, increasing the call stack in this case will
          do nothing for you.
          >
          >
          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m
          >
          "randy1200" <randy1200@disc ussions.microso ft.comwrote in message
          news:61025FB2-9D3A-4478-A0B7-BE61DE78F7C8@mi crosoft.com...
          Visual Studio 2005, C# WinForms application:

          Here's the question: How can I increase the standard 1 MB stack size of
          the
          UI thread in a C# WinForms application?

          Here's why I ask:

          I've inherited some code that at the view (User Interface) layer kicks off
          a
          background worker thread. At the service layer (think CAB service layer),
          there's quite a lot of the following:

          worker.ReportPr ogress(n, new string[] { cat, mesg });

          The _BackgroundWork er_ProgressChan ged() event gets these events, and does
          a
          large amount of string processing. When the user chooses to process all
          available data, the _ProgressChange d() event eventually throws a
          StackOverflow exception with the following in the details section:

          "Cannot evaluate expression because the current thread is in a stack
          overflow state."

          If I take the string processing out of the _ProgressChange d() event, I don't
          get the StackOverflow, so I'm pretty confident that I've isolated the
          problem. Increasing the stack size if definitely a temp fix, but something
          I
          need to consider in the short term.

          Thanks,
          Randy
          >
          >
          >

          Comment

          Working...