Increasing Stack Size for UI Thread: C# WinForms Application

R

randy1200

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.ReportProgress(n, new string[] { cat, mesg });

The _BackgroundWorker_ProgressChanged() event gets these events, and does a
large amount of string processing. When the user chooses to process all
available data, the _ProgressChanged() 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 _ProgressChanged() 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
 
W

Willy Denoyette [MVP]

randy1200 said:
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.ReportProgress(n, new string[] { cat, mesg });

The _BackgroundWorker_ProgressChanged() event gets these events, and does
a
large amount of string processing. When the user chooses to process all
available data, the _ProgressChanged() 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 _ProgressChanged() 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.
 
N

Nicholas Paldino [.NET/C# MVP]

Randy,

Do you have any recursive calls here? The StackOverflowException 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.
 
R

randy1200

That was exactly what I needed. Many thanks!

Randy

Willy Denoyette said:
randy1200 said:
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.ReportProgress(n, new string[] { cat, mesg });

The _BackgroundWorker_ProgressChanged() event gets these events, and does
a
large amount of string processing. When the user chooses to process all
available data, the _ProgressChanged() 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 _ProgressChanged() 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.
 
R

randy1200

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_worker 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.ReportProgress() calls are sending text back to
_BackgroundWorker_ProgressChanged(), 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. _BackgroundWorker_ProgressChanged()
does not seem to have any problems if it's only task is to assign text to a
UI display control.



Nicholas Paldino said:
Randy,

Do you have any recursive calls here? The StackOverflowException 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]
- (e-mail address removed)

randy1200 said:
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.ReportProgress(n, new string[] { cat, mesg });

The _BackgroundWorker_ProgressChanged() event gets these events, and does
a
large amount of string processing. When the user chooses to process all
available data, the _ProgressChanged() 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 _ProgressChanged() 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top