473,480 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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...8(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(Obj ect demand,
StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Windows.Forms.NativeWindow.CreateHandle(Cre ateParams 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.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at LoggingManager.LogManager.ExceptionToUI(LogExcepti on MyException)


Jun 27 '08 #1
7 887
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" <pi****************@hotmail.coma écrit dans le message de groupe
de discussion : uy**************@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/libr...8(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(Obj ect demand,
StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Windows.Forms.NativeWindow.CreateHandle(Cre ateParams 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.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at LoggingManager.LogManager.ExceptionToUI(LogExcepti on MyException)

Jun 27 '08 #2
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:87**********************************@microsof t.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

Jun 27 '08 #3
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" <pi****************@hotmail.coma écrit dans le message de groupe
de discussion : #X**************@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/scribe/wrote in message
news:87**********************************@microsof t.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

Jun 27 '08 #4
The strange thing is that the message looks like a UI restricted ClickOnce
operation
(http://msdn.microsoft.com/en-us/libr...rmission.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" <pi****************@hotmail.coma écrit dans le message de groupe
de discussion : #X**************@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/scribe/wrote in message
news:87**********************************@microsof t.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

Jun 27 '08 #5
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:4E**********************************@microsof t.com...
The strange thing is that the message looks like a UI restricted ClickOnce
operation
(http://msdn.microsoft.com/en-us/libr...rmission.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" <pi****************@hotmail.coma écrit dans le message de
groupe de discussion : #X**************@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/scribe/wrote in message
news:87**********************************@microso ft.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


Jun 27 '08 #6
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.ExecuteReportInCurrentAp pDomain(AppDomain.CurrentDomain.Evidence)

Thanks for the help! :-)
"Patrice" <http://www.chez.com/scribe/wrote in message
news:4E**********************************@microsof t.com...
The strange thing is that the message looks like a UI restricted ClickOnce
operation
(http://msdn.microsoft.com/en-us/libr...rmission.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" <pi****************@hotmail.coma écrit dans le message de
groupe de discussion : #X**************@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/scribe/wrote in message
news:87**********************************@microso ft.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


Jun 27 '08 #7
Great you solved it and thanks for letting us know what caused the
problem...

--
Patrice

"Pieter" <pi****************@hotmail.coma écrit dans le message de groupe
de discussion : e7**************@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/archi...ustomCode.aspx
and solved the problem by addign simply this line of code before rendering
the Report:
Me.rpvwReport.LocalReport.ExecuteReportInCurrentAp pDomain(AppDomain.CurrentDomain.Evidence)

Thanks for the help! :-)
"Patrice" <http://www.chez.com/scribe/wrote in message
news:4E**********************************@microsof t.com...
>The strange thing is that the message looks like a UI restricted
ClickOnce operation
(http://msdn.microsoft.com/en-us/libr...rmission.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" <pi****************@hotmail.coma écrit dans le message de
groupe de discussion : #X**************@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/scribe/wrote in message
news:87**********************************@micros oft.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


Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1887
by: Bob | last post by:
Declaring a module level form object in multiple froms within a project to show another project form causes a stack overflow and other assorted errors. Can anyone help?
11
18797
by: Jozef | last post by:
I have some old code that I use from the Access 95 Developers handbook. The code works very well, with the exception that it doesn't seem to recognize wide screens, and sizes tab controls so that...
4
6530
by: Altramagnus | last post by:
I have a form first created my the main thread. I have another thread monitoring the IO. Upon receiving certain message, I want to show/unhide the form. When the form is first created ( handle...
15
5058
by: http://www.visual-basic-data-mining.net/forum | last post by:
Does anyone have any idea how to transferring data from TextBox1 in form1 to textBox2 in form2..... That means after i fill in any data in textBox1 and click Next button... It will bring me to...
7
3202
by: Roemer | last post by:
Hi all I stumbled over a new problem: I have a programm with just a class that is asynchronous listening for network connections. As soon as someone connected, a new form needs to be created....
3
8597
by: garyusenet | last post by:
Dear Professionals, I have recently been using the wonderful krypton toolkit and am trying to use them in my small hobby application. I include this bit of info as an aside really because i'm...
11
3404
by: TheDataGuy | last post by:
Using MS ACCESS 2002 I developed a FORM, and within that FORM, I created and added a SEARCH BUTTON, and then I created a SUB-FORM, so that when an the END USER clicks on the FORM it will POP-UP...
15
4739
by: Angelo | last post by:
Hi all, I'm using a FileSystemWatcher to monitor a directory. I want the FSW to pop up my already instantiated but invisible form. However, I'm running into some problems with this. 1) In...
16
4132
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
7
1143
by: Pieter | last post by:
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...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.