How to find if Application has Focus

A

Albe V°

In my Application, I need to make a certain graphical refresh,
interrogating SqlServer, only if Application has focus (i.e. the title
bar is blue).

Interrogating .Focused property of various forms of the project is not
the way (maybe, enumerting forms and checking if at least one of them
has focus may work, but I don't think this to be the most beautiful
solution).

Any idea?

Thanks in advance

Alberto
 
F

Family Tree Mike

For your definition of the application having focus, testing (Form.ActiveForm
!= null) should work.
 
I

Ignacio Machin ( .NET/ C# MVP )

In my Application, I need to make a certain graphical refresh,
interrogating SqlServer, only if Application has focus (i.e. the title
bar is blue).

From where you do the refresh?

I think that you need to check not if a form is focused but if the
form where you are painting is active.
are y ou painting from a worker thread?
In any case you use Form.ActiveForm to get the active form of the app
 
A

Albe V°

Ignacio Machin ( .NET/ C# MVP ), grazie al ripristinarsi del
collegamento fra i due neuroni, mercoledì ha scritto:
From where you do the refresh?

One of the form of the solution has a timer, and the timer calls a
refresh procedure.
Unfortunately, there is a .Net documented bug that makes the app crash
if graphical refreshes are frequently called when the app itself is in
background ina terminal server session.
Now, a customer is in this unlucky situation: the operators use then
client in each own terminal server session, the client makes a
graphical layout refresh according to some SqlServer data, but if the
operator opens another application, and puts my client application in
background or minimized, the app itself crashes.
At the present, MS does not provide any solution, so the only thing I
can do is to try to avoid this refresh.

But: How can I determine if the app is in background?
I think that you need to check not if a form is focused but if the
form where you are painting is active.

No, the form has to be repainted if the app has the focus.
If the app has the focus, but the operator clicked on a menu or on a
treeview menu, the form itself is active, but with no focus. If the app
is minimized, the form is anyway active, with no focus, but this causes
the crash.
are y ou painting from a worker thread?

I don't exactly know what a worker thread is...

In any case you use Form.ActiveForm to get the active form of the app

But the app has always an active form, even if the app is in
background.
I also tried enumerating and testing on Application.Openforms, but if I
click on a treeview menu (by example), I don't find any form with
..Focused property True.


Bye

Alberto
 

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