[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Form created on BackGroundThread

Pieter

6/10/2008 12:25:00 PM

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/librar...(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)




7 Answers

Patrice

6/10/2008 1:13:00 PM

0

And you call it using ? Do you want your application to continue in case of
an unexpected exception ?

You have also an event that is part of the application framework
(Application_UnhandledException from the top of my head) and that could
well work even for background thread.

--
Patrice

"Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de groupe
de discussion : uyi3LUvyIHA.4896@TK2MSFTNGP03.phx.gbl...
> 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/librar...(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)
>
>
>
>

Pieter

6/10/2008 1:37:00 PM

0

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...

"Patrice" <http://www.chez.com/s... wrote in message
news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@microsoft.com...
> And you call it using ? Do you want your application to continue in case
> of an unexpected exception ?
>
> You have also an event that is part of the application framework
> (Application_UnhandledException from the top of my head) and that could
> well work even for background thread.
>
> --
> Patrice


Patrice

6/10/2008 5:36:00 PM

0

And do you still continue ?

My personal preference instead of catching unexpected errors would be to use
this (global) event that could well handle nicely threading. You can keep a
try block without a catch clause if you want to clean up resources before
your handler runs...

This way you shouldn"t have to care about threading issues and you don't
have to explicitely try/catch everywhere unless you really need...

If you keep this design, it seems a bit weird as the message usually clearly
state this is a threading issue. Here it would make me think rather to a
click once application that doesn't have some UI permissions (don't remember
the details but by default you have some security restrictions applied to
clickonce applications). Is this a clickonce deployed application ?

What if you try to create fresh a small repro. Do you have the same behavior
?

--
Patrice

"Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de groupe
de discussion : #Xvhc8vyIHA.2068@TK2MSFTNGP05.phx.gbl...
> 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...
>
> "Patrice" <http://www.chez.com/s... wrote in message
> news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@microsoft.com...
>> And you call it using ? Do you want your application to continue in case
>> of an unexpected exception ?
>>
>> You have also an event that is part of the application framework
>> (Application_UnhandledException from the top of my head) and that could
>> well work even for background thread.
>>
>> --
>> Patrice
>
>

Patrice

6/10/2008 5:57:00 PM

0

The strange thing is that the message looks like a UI restricted ClickOnce
operation
(http://msdn.microsoft.com/en-us/library/system.security.permissions.uipermi...).
Is this a ClickOnce applciation ? Does it have fulltrust ?

When this is a cross thread operation the exception is an
InvalidOperationException and is much more explicit (translated from french
"Invalid Cross Thread operation : the control "Blah" is being access by
another thread than the one who created it"). You are using .NET 2.0 ?

My first move would be likely to create a new application as simple as
possible that throwns in a background thread to see if I have the same
behavior.

--
Patrice


"Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de groupe
de discussion : #Xvhc8vyIHA.2068@TK2MSFTNGP05.phx.gbl...
> 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...
>
> "Patrice" <http://www.chez.com/s... wrote in message
> news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@microsoft.com...
>> And you call it using ? Do you want your application to continue in case
>> of an unexpected exception ?
>>
>> You have also an event that is part of the application framework
>> (Application_UnhandledException from the top of my head) and that could
>> well work even for background thread.
>>
>> --
>> Patrice
>
>

Pieter

6/11/2008 7:37:00 AM

0

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


"Patrice" <http://www.chez.com/s... wrote in message
news:4E157960-BA02-4957-AAB8-929CEE8A979B@microsoft.com...
> The strange thing is that the message looks like a UI restricted ClickOnce
> operation
> (http://msdn.microsoft.com/en-us/library/system.security.permissions.uipermi...).
> Is this a ClickOnce applciation ? Does it have fulltrust ?
>
> When this is a cross thread operation the exception is an
> InvalidOperationException and is much more explicit (translated from
> french "Invalid Cross Thread operation : the control "Blah" is being
> access by another thread than the one who created it"). You are using .NET
> 2.0 ?
>
> My first move would be likely to create a new application as simple as
> possible that throwns in a background thread to see if I have the same
> behavior.
>
> --
> Patrice
>
>
> "Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de
> groupe de discussion : #Xvhc8vyIHA.2068@TK2MSFTNGP05.phx.gbl...
>> 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...
>>
>> "Patrice" <http://www.chez.com/s... wrote in message
>> news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@microsoft.com...
>>> And you call it using ? Do you want your application to continue in case
>>> of an unexpected exception ?
>>>
>>> You have also an event that is part of the application framework
>>> (Application_UnhandledException from the top of my head) and that could
>>> well work even for background thread.
>>>
>>> --
>>> Patrice
>>
>>
>


Pieter

6/11/2008 9:48:00 AM

0

Hi,

Apparently it isn't because of a different Threa,d but specific to the
Reportviewer. We found the needed information o nthis site:
http://blogs.msdn.com/mosharaf/archive/2005/12/20/LocalReportCusto...
and solved the problem by addign simply this line of code before rendering
the Report:
Me.rpvwReport.LocalReport.ExecuteReportInCurrentAppDomain(AppDomain.CurrentDomain.Evidence)



Thanks for the help! :-)


"Patrice" <http://www.chez.com/s... wrote in message
news:4E157960-BA02-4957-AAB8-929CEE8A979B@microsoft.com...
> The strange thing is that the message looks like a UI restricted ClickOnce
> operation
> (http://msdn.microsoft.com/en-us/library/system.security.permissions.uipermi...).
> Is this a ClickOnce applciation ? Does it have fulltrust ?
>
> When this is a cross thread operation the exception is an
> InvalidOperationException and is much more explicit (translated from
> french "Invalid Cross Thread operation : the control "Blah" is being
> access by another thread than the one who created it"). You are using .NET
> 2.0 ?
>
> My first move would be likely to create a new application as simple as
> possible that throwns in a background thread to see if I have the same
> behavior.
>
> --
> Patrice
>
>
> "Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de
> groupe de discussion : #Xvhc8vyIHA.2068@TK2MSFTNGP05.phx.gbl...
>> 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...
>>
>> "Patrice" <http://www.chez.com/s... wrote in message
>> news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@microsoft.com...
>>> And you call it using ? Do you want your application to continue in case
>>> of an unexpected exception ?
>>>
>>> You have also an event that is part of the application framework
>>> (Application_UnhandledException from the top of my head) and that could
>>> well work even for background thread.
>>>
>>> --
>>> Patrice
>>
>>
>


Patrice

6/11/2008 10:46:00 AM

0

Great you solved it and thanks for letting us know what caused the
problem...

--
Patrice

"Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de groupe
de discussion : e7bf8g6yIHA.2384@TK2MSFTNGP02.phx.gbl...
> Hi,
>
> Apparently it isn't because of a different Threa,d but specific to the
> Reportviewer. We found the needed information o nthis site:
> http://blogs.msdn.com/mosharaf/archive/2005/12/20/LocalReportCusto...
> and solved the problem by addign simply this line of code before rendering
> the Report:
> Me.rpvwReport.LocalReport.ExecuteReportInCurrentAppDomain(AppDomain.CurrentDomain.Evidence)
>
>
>
> Thanks for the help! :-)
>
>
> "Patrice" <http://www.chez.com/s... wrote in message
> news:4E157960-BA02-4957-AAB8-929CEE8A979B@microsoft.com...
>> The strange thing is that the message looks like a UI restricted
>> ClickOnce operation
>> (http://msdn.microsoft.com/en-us/library/system.security.permissions.uipermi...).
>> Is this a ClickOnce applciation ? Does it have fulltrust ?
>>
>> When this is a cross thread operation the exception is an
>> InvalidOperationException and is much more explicit (translated from
>> french "Invalid Cross Thread operation : the control "Blah" is being
>> access by another thread than the one who created it"). You are using
>> .NET 2.0 ?
>>
>> My first move would be likely to create a new application as simple as
>> possible that throwns in a background thread to see if I have the same
>> behavior.
>>
>> --
>> Patrice
>>
>>
>> "Pieter" <pieterNOSPAMcoucke@hotmail.com> a écrit dans le message de
>> groupe de discussion : #Xvhc8vyIHA.2068@TK2MSFTNGP05.phx.gbl...
>>> 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...
>>>
>>> "Patrice" <http://www.chez.com/s... wrote in message
>>> news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@microsoft.com...
>>>> And you call it using ? Do you want your application to continue in
>>>> case of an unexpected exception ?
>>>>
>>>> You have also an event that is part of the application framework
>>>> (Application_UnhandledException from the top of my head) and that
>>>> could well work even for background thread.
>>>>
>>>> --
>>>> Patrice
>>>
>>>
>>
>
>