473,782 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Control.Invoke with ShowDialog

I have a main form with a "lock" button. When the lock button is
clicked, another form is shown using ShowDialog(this ). The user must
enter their PIN on this form to close it and resume the main app.
Because it is showing using ShowDialog(this ), the main form in the back
cannot be used until the user clears the login form. A feature was
added to the app to automatically lock the app after a period of
inactivity. This uses a timer. If the lock routine is called from the
timer, then I get an illegal cross thread operation exception,
presumably because the lock form is shown on another thread and passing
in "this" causes the exception. Here is the code for the lock routine,
the button click, and the idle timer:

void _idleTimer_Elap sed(object sender,
System.Timers.E lapsedEventArgs e)
{
lockRegister();
_idleTimer.Star t();
}

private void btnLock_Click(o bject sender, EventArgs e)
{
_idleTimer.Stop ();
lockRegister();
_idleTimer.Star t();
}

private void lockProgram()
{
LoginForm frmLogin = new LoginForm(_user , "Program
Locked!", true);

frmLogin.StartP osition = FormStartPositi on.Manual;
frmLogin.Size = this.pnlDataEnt ry.Size;
frmLogin.Locati on = this.pnlDataEnt ry.Location;

frmLogin.ShowDi alog(this);
if (frmLogin.Dialo gResult == DialogResult.OK )
_user = frmLogin.Curren tUser;
else
this.Close();
}

To summarize:

1. If the program is locked using the Lock button, it works correctly.
2. If the timer elapses, I get the cross thread exception.

I know I can check the InvokeRequired to see if I need control invoke,
but how do I pass "this" to the ShowDialog method in that instance so
that the main form cannot be accessed until the dialog is closed?

Thanks,

Chris

Jun 26 '06 #1
4 7823
Chris Dunaway wrote:
I have a main form with a "lock" button. When the lock button is
clicked, another form is shown using ShowDialog(this ). The user must
enter their PIN on this form to close it and resume the main app.
Because it is showing using ShowDialog(this ), the main form in the back
cannot be used until the user clears the login form. A feature was
added to the app to automatically lock the app after a period of
inactivity. This uses a timer. If the lock routine is called from the
timer, then I get an illegal cross thread operation exception,
presumably because the lock form is shown on another thread and passing
in "this" causes the exception. Here is the code for the lock routine,
the button click, and the idle timer:

<snippedy-do-daah>

Hi Chris, change your _idleTimer_Elap sed function to this:

///
void _idleTimer_Elap sed ( object sender, System.Timers.E lapsedEventArgs e )
{
this.Invoke( new SafeLockRegiste rDelegate( SafeLockRegiste r ) );
_idleTimer.Star t();
}

private delegate void SafeLockRegiste rDelegate ( );
private void SafeLockRegiste r ( )
{
lockRegister();
}
///

That should do the trick!

--
Hope this helps,
Tom Spink
Jun 26 '06 #2
Chris,

1. You can use System.Windows. Forms.Timer. It is not very accurate, but the
event handler is executed in the main UI thread and you won't run into this
threading probelms.

2. You can also use System.Timer.Ti mer also. Set the timer's
SynchronizingOb ject to reference one of your controls or the form itself and
then the timer will marshal its Elapsed event to the UI thread and you again
won't run to this problem. From what I see you use exactly this, just need
to set this property; no need of Control.Invoke in this case.
3. If you prefer to use ControlInvoke make sure that you

you create the PIN dialog form in the method that is marshaled using
Control.Invoke. The form needs to be created in the UI thread, it is not
only the ShowDialog that has to be to be called by the UI thread.
--
HTH
Stoitcho Goutsev (100)

"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** *************@r 2g2000cwb.googl egroups.com...
I have a main form with a "lock" button. When the lock button is
clicked, another form is shown using ShowDialog(this ). The user must
enter their PIN on this form to close it and resume the main app.
Because it is showing using ShowDialog(this ), the main form in the back
cannot be used until the user clears the login form. A feature was
added to the app to automatically lock the app after a period of
inactivity. This uses a timer. If the lock routine is called from the
timer, then I get an illegal cross thread operation exception,
presumably because the lock form is shown on another thread and passing
in "this" causes the exception. Here is the code for the lock routine,
the button click, and the idle timer:

void _idleTimer_Elap sed(object sender,
System.Timers.E lapsedEventArgs e)
{
lockRegister();
_idleTimer.Star t();
}

private void btnLock_Click(o bject sender, EventArgs e)
{
_idleTimer.Stop ();
lockRegister();
_idleTimer.Star t();
}

private void lockProgram()
{
LoginForm frmLogin = new LoginForm(_user , "Program
Locked!", true);

frmLogin.StartP osition = FormStartPositi on.Manual;
frmLogin.Size = this.pnlDataEnt ry.Size;
frmLogin.Locati on = this.pnlDataEnt ry.Location;

frmLogin.ShowDi alog(this);
if (frmLogin.Dialo gResult == DialogResult.OK )
_user = frmLogin.Curren tUser;
else
this.Close();
}

To summarize:

1. If the program is locked using the Lock button, it works correctly.
2. If the timer elapses, I get the cross thread exception.

I know I can check the InvokeRequired to see if I need control invoke,
but how do I pass "this" to the ShowDialog method in that instance so
that the main form cannot be accessed until the dialog is closed?

Thanks,

Chris

Jun 27 '06 #3
Thanks Tom, Stoitcho for the suggestions.

What I ended up doing was using a delegate:

private delegate void lockdelegate();

I then changed my timer event as follows:

void _idleTimer_Elap sed(object sender,
System.Timers.E lapsedEventArgs e)
{
//marshal the call to the correct thread using a delegate
this.Invoke(new lockdelegate(lo ckRegister));
_idleTimer.Star t();
}

This way, the lock method is executed on the UI thread.

Thanks,

Chris

Jun 27 '06 #4
Chris,

I want to say it one more time in case you missed it in my post. If you use
System.Timers.T imer class (component) you don't need to marshall the call by
your self. You can let the timer do it for you - just set the
SynchronizingOb ject property to reference a control on your UI.
--
HTH
Stoitcho Goutsev (100)
"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
Thanks Tom, Stoitcho for the suggestions.

What I ended up doing was using a delegate:

private delegate void lockdelegate();

I then changed my timer event as follows:

void _idleTimer_Elap sed(object sender,
System.Timers.E lapsedEventArgs e)
{
//marshal the call to the correct thread using a delegate
this.Invoke(new lockdelegate(lo ckRegister));
_idleTimer.Star t();
}

This way, the lock method is executed on the UI thread.

Thanks,

Chris

Jun 28 '06 #5

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

Similar topics

5
4312
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the exe. The button clicking part is working fine, but I just cannot get the method call. Here is my program snippet. **************************************************************
3
1910
by: Alexander | last post by:
I have a dialog that loads a file from a web server, this is done in an own thread. On finish or failure the thread is supposed to raise an event notifying the dialog. If the WebRequest now raises a timeout exception the invoke of the event is done, but the event is never executed. For testing purposes I exchanged the exception once by a simple divison by zero which works perfectly, the event is fired and executed. I debugged the thread to...
2
3009
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method with arguments I need to do this as the method is being called Asynchronously from elsewhere using delegate's BeginInvoke method. The form is well created/initialized yet the messages states otherwise.
19
6738
by: trint | last post by:
Ok, I start my thread job: Thread t = new Thread(new ThreadStart(invoicePrintingLongRunningCodeThread)); t.IsBackground = true; t.Start(); There are lots of calls to controls and many happen in function calls from invoicePrintingLongRunningCodeThread. I need just an example in
6
15159
by: Valerie Hough | last post by:
I'm not entirely sure what the difference is between these two approaches. In order to avoid reentrant code, I was using Control.BeginInvoke in my UI to cause an asynchronous activity to be done on the UI's message loop. I began to get System.ExecutionEngineException errors so (on the theory of do something different if what you're doing isn't working) I switched to using delegate.BeginInvoke with the appropriate EndInvoke and the problem...
7
55792
by: Sakharam Phapale | last post by:
Hi All, I want to give option to user for selecting directory, just like for selecting file using OpenFileDialog control. How to make it possible?
2
1749
by: Frank | last post by:
Hi, I have an vb6 app that I want to port to .net. I tried the wizard but it doesn't convert it all. In my app I have a form with a datagrid on it. If a user doubleclicks on a row a form is shown (modal) with details (and some more info) about the selected row. Under vb6 it's quite easy to select another row, through code, in de datagrid without closing the detailform. How can this be done in .net? Thanks, Frank
3
5176
by: rn5a | last post by:
The ASPX FileUpload control displays a TextBox along with a 'Browse...' Button. Setting the different properties of this control just reflects the changes in the TextBox but not the Button. For e.g. if the BackColor of a FileUpload control is set to blue, then only the TextBox color changes to blue but the BackColor of the Button doesn't change to blue. Or if the Font-Size of a FileUpload control is set to 15pt, then only the size of the...
2
4426
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small or simple). I am hoping the description will generate some ideas I can check out. PROBLEM: ----------------- Switching to UI thread using Invoke(), everything seems good.
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9939
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7486
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5375
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.