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 4 7767
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_Elapsed function to this:
///
void _idleTimer_Elapsed ( object sender, System.Timers.ElapsedEventArgs e )
{
this.Invoke( new SafeLockRegisterDelegate( SafeLockRegister ) );
_idleTimer.Start();
}
private delegate void SafeLockRegisterDelegate ( );
private void SafeLockRegister ( )
{
lockRegister();
}
///
That should do the trick!
--
Hope this helps,
Tom Spink
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.Timer also. Set the timer's
SynchronizingObject 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*********************@r2g2000cwb.googlegrou ps.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_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
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_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
//marshal the call to the correct thread using a delegate
this.Invoke(new lockdelegate(lockRegister));
_idleTimer.Start();
}
This way, the lock method is executed on the UI thread.
Thanks,
Chris
Chris,
I want to say it one more time in case you missed it in my post. If you use
System.Timers.Timer 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
SynchronizingObject property to reference a control on your UI.
--
HTH
Stoitcho Goutsev (100)
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.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_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //marshal the call to the correct thread using a delegate this.Invoke(new lockdelegate(lockRegister)); _idleTimer.Start(); }
This way, the lock method is executed on the UI thread.
Thanks,
Chris This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |