By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,106 Members | 1,080 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,106 IT Pros & Developers. It's quick & easy.

Increasing Stack Size for UI Thread: C# WinForms Application

P: n/a
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

Jan 11 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"randy1200" <ra*******@discussions.microsoft.comwrote in message
news:61**********************************@microsof t.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.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.

Jan 11 '08 #2

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"randy1200" <ra*******@discussions.microsoft.comwrote in message
news:61**********************************@microsof t.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.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

Jan 11 '08 #3

P: n/a
That was exactly what I needed. Many thanks!

Randy

"Willy Denoyette [MVP]" wrote:
"randy1200" <ra*******@discussions.microsoft.comwrote in message
news:61**********************************@microsof t.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.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.

Jan 11 '08 #4

P: n/a
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"randy1200" <ra*******@discussions.microsoft.comwrote in message
news:61**********************************@microsof t.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.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


Jan 11 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.