Net 2.0 - unbalanced stack

A

AlexS

Hi,

I migrated working 1.1 console project which worked to 2.0 having forms and
console window and started to get this exception in debugger
A call to PInvoke function 'Utils!Utils.ConsoleWindow::GetStdHandle' has
unbalanced the stack

This happens when I issue ShowWindow Win32 call from non-UI thread.

Strange thing it this works w/o any troubles in 1.1.

Main thread fails with cryptic

System.Reflection.TargetInvocationException: Exception has been thrown by
the target of an invocation. ---> System.InvalidOperationException:
Cross-thread operation not valid: Control '' accessed from a thread other
than the thread it was created on.

What should I do?
 
J

Jon Skeet [C# MVP]

AlexS said:
I migrated working 1.1 console project which worked to 2.0 having forms and
console window and started to get this exception in debugger
A call to PInvoke function 'Utils!Utils.ConsoleWindow::GetStdHandle' has
unbalanced the stack

This happens when I issue ShowWindow Win32 call from non-UI thread.

Strange thing it this works w/o any troubles in 1.1.

Main thread fails with cryptic

System.Reflection.TargetInvocationException: Exception has been thrown by
the target of an invocation. ---> System.InvalidOperationException:
Cross-thread operation not valid: Control '' accessed from a thread other
than the thread it was created on.

What should I do?

You need to only access UI elements from the thread which "owns" them -
typically the one which created them.
See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
 
A

AlexS

Hi, Jon

I don't access UI elements from another thread - .Net maybe does. See main
thread exception - I have no controls with '' name

Initial exception in ShowWindow causes final crash with
A first chance exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

A first chance exception of type 'System.NullReferenceException' occurred in
System.Windows.Forms.dll

ShowWindow is Win32 API. As per debugger call which causes exception happens
on main UI thread - it's event handler for VisibleChanged

Any ideas?
 
A

AlexS

By the way,

I did some more debugging and see now that ShowWindow returns ok, then
control goes somewhere inside framework and I get crash.

Please correct initial post: "this happens when I issue ShowWindow Win32
call from UI-thread"

Thx
Alex
 
W

Wavemaker

AlexS said:
Hi,

I migrated working 1.1 console project which worked to 2.0 having
forms and console window and started to get this exception in debugger
A call to PInvoke function 'Utils!Utils.ConsoleWindow::GetStdHandle'
has unbalanced the stack

This happens when I issue ShowWindow Win32 call from non-UI thread.

Strange thing it this works w/o any troubles in 1.1.

Main thread fails with cryptic

System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. ---> System.InvalidOperationException:
Cross-thread operation not valid: Control '' accessed from a thread
other than the thread it was created on.

What should I do?

The windows handle you're passing to ShowWindow, who owns the handle? Is
it the Form that's making the call to ShowWindow? Is it some other
window?
 
W

Willy Denoyette [MVP]

Well, that means that you are passing a handle owned by the UI thread to
another thread, right?
Note that your code is wrong even in v1.x, in Windows it's wrong to use a
windows handle (guess StdHandle you pass to ShowWindow) from a thread that
doesn't own the handle.


Willy.
 

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