472,968 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,968 software developers and data experts.

How to find if Application has Focus

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

--
Georges Pompidou: Un uomo di stato è un politico che dona se stesso al
servizio della nazione. Un politico è un uomo di stato che pone la
nazione al suo servizio.
Jun 27 '08 #1
4 5310
For your definition of the application having focus, testing (Form.ActiveForm
!= null) should work.

"Albe V°" wrote:
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

--
Georges Pompidou: Un uomo di stato è un politico che dona se stesso al
servizio della nazione. Un politico è un uomo di stato che pone la
nazione al suo servizio.
Jun 27 '08 #2
On May 14, 5:59*am, Albe V° <vaccariTO...@hotmail.comwrote:
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

Jun 27 '08 #3
Ignacio Machin ( .NET/ C# MVP ), grazie al ripristinarsi del
collegamento fra i due neuroni, mercoledì ha scritto:
On May 14, 5:59*am, Albe V° <vaccariTO...@hotmail.comwrote:
>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?
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

--
La forza di volontà è guardare negli occhi una ragazza in topless.
Jun 27 '08 #4
On Wed, 14 May 2008 07:42:27 -0700, Albe V° <va**********@hotmail.com>
wrote:
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?
That's an unfortunate bug.

My first thought is to wonder whether "refreshes are frequently called" is
so frequent that it wouldn't matter to the user to simply slow them down
all the time. At what frequency does a graphical update result in a
crash? Would it be feasible to simply reduce your visual refresh
frequency to some rate below this critical rate?

Barring that, I don't believe what you want is possible using only the
..NET Framework. I haven't noticed anything like this available directly
in the framework. However, Windows certainly provides that information
and you can get at it through various means. I'd guess the most direct
way would be to use the Application.AddMessageFilter() to watch for
WM_ACTIVATEAPP messages. Your application will receive one with it
changes state from foreground to background or vice a versa.

If that doesn't do it, you might submit a support request to Microsoft via
their web site. They provide two free support requests per product, and
my experience has been that a) they extend this to development questions,
and b) if the support request is a consequence of some bug in their
products, they credit back the request to your product ID. I've had about
a 50% success rate getting actual solutions for bugs in Windows this way,
and have always at least received a courteous effort to try to solve the
problem.

Granted, my sample size is low (maybe a half-dozen at most over the last
ten years or so), but it's worth a try if the above doesn't help. The bug
you're dealing with is fairly serious and seems clearly specific to a
particular Microsoft product; I would expect them to be reasonably
proactive in trying to help you deal with it.

Pete
Jun 27 '08 #5

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

Similar topics

0
by: titouille | last post by:
Hi everybody ! first, sorry for my poor english, I'm french mothertongue ;) technologies : -------------- Python 2.3 Twisted Matrix 1.2.0 wxPython 2.5.1
2
by: Gil | last post by:
can you set focus to the current access application through vba? lets say i tell access to open another application and that new application gains the focus and causes access to loose the focus....
0
by: euthymos | last post by:
Dear developers, I'm developing an application which uses MSWord Interop interface. When my application launches MSWord, my application itself loses the focus (even if MSWord is not visible). ...
4
by: Octavius Khan | last post by:
I have an application that runs in the system tray and I use global hotkeys to perform functions. When certain hotkeys are pressed, my system tray application may display a message box and...
3
by: Kbalz | last post by:
Hello - I've tried to search this, but the word Focus in searching always returns lots of control focusing. I would like my C# Win App to minimize, and focus a program that is already running,...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
2
by: petinboy | last post by:
Hi All! I am developing an accessibility keyboard application with Visual Studio 2005 Professional Edition and with C#. The application is a keyboard with buttons that interacts with any...
10
by: Gary | last post by:
I'm trying to find a way to determin which window Within-Another-Program currently has the focus. THE SITUATION: (Skip down to "My Question" if you don't want the background...) * This is...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.