Windows Form and Thread question

M

Michael Howes

I and trying to detect idle time or sorts in the applications I'm
building.
To do this the main Form also inherits from IMessageFilter and I
listen for WM_MOUSEMOVE. When that happens I reset a DateTime.

To check the time between last mouse move I have a timer
(System.Threading.Timer)
If the the DateTime math meets a certain threshold I want to show our
login dialog, essentially locking people out of this unattended app.

When I try and ShowDialog() on my login form I get a threading error
because of the owner I try and pass in.

this failed so I tried packing up the main form in the thread so it
was passed as an argument (object state) of the timer and use that
instead of this in the timer callback but that didn't work either.

what's the proper way to show the login dialog when shown because of a
timer?

thanks
mike
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

It appears that you are using the timer in the System.Timers namespace
or the System.Threading.Timer class, and not the timer in the
System.Windows.Forms.Timer component. This is fine, but this means that the
thread that the event fires on is not guaranteed to be on the UI thread.

What you have to do is create a method which will call ShowDialog, and
then a delegate with a signature that matches the signature of the method
which will perform this call. Then, pass an instance of that delegate to
the Invoke method on a control on the form (or the form itself) and the call
to ShowDialog will be performed on the UI thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top