C
Chris Dunaway
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_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
lockRegister();
_idleTimer.Start();
}
private void btnLock_Click(object sender, EventArgs e)
{
_idleTimer.Stop();
lockRegister();
_idleTimer.Start();
}
private void lockProgram()
{
LoginForm frmLogin = new LoginForm(_user, "Program
Locked!", true);
frmLogin.StartPosition = FormStartPosition.Manual;
frmLogin.Size = this.pnlDataEntry.Size;
frmLogin.Location = this.pnlDataEntry.Location;
frmLogin.ShowDialog(this);
if (frmLogin.DialogResult == DialogResult.OK)
_user = frmLogin.CurrentUser;
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
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_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
lockRegister();
_idleTimer.Start();
}
private void btnLock_Click(object sender, EventArgs e)
{
_idleTimer.Stop();
lockRegister();
_idleTimer.Start();
}
private void lockProgram()
{
LoginForm frmLogin = new LoginForm(_user, "Program
Locked!", true);
frmLogin.StartPosition = FormStartPosition.Manual;
frmLogin.Size = this.pnlDataEntry.Size;
frmLogin.Location = this.pnlDataEntry.Location;
frmLogin.ShowDialog(this);
if (frmLogin.DialogResult == DialogResult.OK)
_user = frmLogin.CurrentUser;
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