Form created on BackGroundThread

  • Thread starter Thread starter Pieter
  • Start date Start date
P

Pieter

Hi,

I have some generic module that handles exceptions. simply you call it like
this: "LogManager.ErrorMessage(ex)".

Worked great, until we now discovered that it isn't Thread-safe. What
actially happens is this: A Form is made, labels and buttons are made etc
with the exception-details, but when trying to show the Form
(MyExceptionForm.ShowDialog) we get an System.Security.SecurityException
(see udnerneath for more info).

Seems completely normal to me, because we are accessing the UI from another
Thread. But how to find a solution?


Thanks a lot in advance,


Pieter


PS 1 : I see 2 possiblities:
1. Call InvokeRequired with Invoke like we usally do when accessing controls
from another thread
(http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx). BUT:
MyExceptionForm.InvokeRequired returns false... I guess this is because it
has been constructed in a background Thread?
2. To get around this problem: Figure out when we are on a background thread
(via Threading.Thread.CurrentThread.IsBackground: is this true??) and than
get via an event an an Invoke or something like that on the Main-htread and
call the form from there?
3. ...


PS 2: The exception when doing a MyExceptionForm.ShowDialog:
System.Security.SecurityException was caught
Message="Request for the permission of type
'System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
Source="mscorlib"
StackTrace:
at System.Security.CodeAccessSecurityEngine.Check(Object demand,
StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Form.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at LoggingManager.LogManager.ExceptionToUI(LogException MyException)
 
I put this line "LogManager.ErrorMessage(ex)" in every Try-Catch-block (in
the catch offcourse :-) ).
So what it actually does is: it writes the error in a database, sends it via
an email, and shows it on the screen. But showing on the screen only works
when the exception handled on the GUI-thread. so I want it be shown always,
whiwhever thread was used...
 
Hi Patrice,

Thansk a lot for your efforts!

It's a VB.NET 2.0 Windows Forms application.
We have this problem whenever we want to show the form, when the exception
happened on a Thread other than the one of the GUI.
I'm quit sure it doesn't has any to do with permissions, because of these 2
reasons:
- If we run the same code on the GUI thread and generate an exception there,
it shows it nicely.
- If even happens when trying to do a Cursor.Current =
System.Windows.Forms.Cursors.WaitCursor
Although, I didn't test it yet with some sampel application if it really has
only something to do with a different Thread. We noticed it because of an
error generated by the ReportViewer (which uses another Thread). And because
the exception-handlign screw up whenever it accessed the GUI, I presumed it
was because of this... I'll try to isolate the problem...

Handlign the exceptions in another way won't be possible unfortunately.


Pieter
 
Back
Top