Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Form created on BackGroundThread

Reply
 
Old 06-10-2008   #1 (permalink)
Pieter


 
 

Form created on BackGroundThread

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





My System SpecsSystem Spec
Old 06-10-2008   #2 (permalink)
Patrice


 
 

Re: Form created on BackGroundThread

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@xxxxxx> a écrit dans le message de groupe
de discussion : uyi3LUvyIHA.4896@xxxxxx...
Quote:

> 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/libr...28(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)
>
>
>
>
My System SpecsSystem Spec
Old 06-10-2008   #3 (permalink)
Pieter


 
 

Re: Form created on BackGroundThread

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/scribe/> wrote in message
news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@xxxxxx
Quote:

> 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

My System SpecsSystem Spec
Old 06-10-2008   #4 (permalink)
Patrice


 
 

Re: Form created on BackGroundThread

The strange thing is that the message looks like a UI restricted ClickOnce
operation
(http://msdn.microsoft.com/en-us/libr...ermission.aspx).
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@xxxxxx> a écrit dans le message de groupe
de discussion : #Xvhc8vyIHA.2068@xxxxxx
Quote:

> 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/scribe/> wrote in message
> news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@xxxxxx
Quote:

>> 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
>
>
My System SpecsSystem Spec
Old 06-11-2008   #5 (permalink)
Pieter


 
 

Re: Form created on BackGroundThread

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/scribe/> wrote in message
news:4E157960-BA02-4957-AAB8-929CEE8A979B@xxxxxx
Quote:

> The strange thing is that the message looks like a UI restricted ClickOnce
> operation
> (http://msdn.microsoft.com/en-us/libr...ermission.aspx).
> 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@xxxxxx> a écrit dans le message de
> groupe de discussion : #Xvhc8vyIHA.2068@xxxxxx
Quote:

>> 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/scribe/> wrote in message
>> news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@xxxxxx
Quote:

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

My System SpecsSystem Spec
Old 06-11-2008   #6 (permalink)
Pieter


 
 

Re: Form created on BackGroundThread

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/archi...ustomCode.aspx
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/scribe/> wrote in message
news:4E157960-BA02-4957-AAB8-929CEE8A979B@xxxxxx
Quote:

> The strange thing is that the message looks like a UI restricted ClickOnce
> operation
> (http://msdn.microsoft.com/en-us/libr...ermission.aspx).
> 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@xxxxxx> a écrit dans le message de
> groupe de discussion : #Xvhc8vyIHA.2068@xxxxxx
Quote:

>> 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/scribe/> wrote in message
>> news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@xxxxxx
Quote:

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

My System SpecsSystem Spec
Old 06-11-2008   #7 (permalink)
Patrice


 
 

Re: Form created on BackGroundThread

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

--
Patrice

"Pieter" <pieterNOSPAMcoucke@xxxxxx> a écrit dans le message de groupe
de discussion : e7bf8g6yIHA.2384@xxxxxx...
Quote:

> 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/archi...ustomCode.aspx
> 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/scribe/> wrote in message
> news:4E157960-BA02-4957-AAB8-929CEE8A979B@xxxxxx
Quote:

>> The strange thing is that the message looks like a UI restricted
>> ClickOnce operation
>> (http://msdn.microsoft.com/en-us/libr...ermission.aspx).
>> 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@xxxxxx> a écrit dans le message de
>> groupe de discussion : #Xvhc8vyIHA.2068@xxxxxx
Quote:

>>> 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/scribe/> wrote in message
>>> news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@xxxxxx
>>>> 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
>>>
>>>
>>
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Form Elements - How To? VB Script
Files created by Powershell are over 2X bigger than files created bytext editor or cmd.exe. PowerShell
form problems PowerShell
Sent of Upgrade form (What do i do now?) Vista General
Feedback form Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46