Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 Diskussionen

catching a WM_CLOSE Win32 message

send_to_alan
Einsteiger
2.610Aufrufe
Dear experts, I am faced with a problem where a fortran command line application, when running on a CONDOR cluster of WinXP machines must kill itself on receiving a WM_CLOSE Win32 message from CONDOR.
Quote from a condor mailing list
Condor will send a WM_CLOSE Win32 message to your app - if your
app wants, it can catch that message and do whatever it wants to shut itself
down.
Does anybody has any idea how I can go about this, my program is an MD code, which is effectively an infinite loop! On sending a WM_CLOSE Win32 message condor will give 10 minutes before starting a hard kill, before then I have to intercept it and kill my app. Also there should be no performance hit(or very very little) from this procedure, so basically i need a very elegent, optimised solution!!
IFC version 8.something plugged into VS2003.NET
Thanks!
0 Kudos
17 Antworten
anthonyrichards
Neuer Beitragender III
2.610Aufrufe
What is an MD code?
In order to intercept a Windows message, your application will have to be running a Windows message loop (look up 'message loop' in Fortran help) and hence it should be set up as a win32 application (not difficult, the wizard will help).
How about then modifying your number-crunching code to check, at regular intervals,a global flag (set to a'start' or 'continue' value at the begining),which can be set to a value to indicate 'close' by the procedure that senses the WM_CLOSE command? HTH

Message Edited by anthonyrichards on 09-24-2004 06:13 AM

send_to_alan
Einsteiger
2.610Aufrufe
Thanks for the info.
MD code stands for molecular dynamics code or program. This is an atomic simulation program that is numerically intensive and run on compute clusters. Generally fortran is a popular language for these codes due to speed requirements.
sadly the intel fortran help does not have a message loop section. will try elsewhere for the info.
I have a feeling, may be i am wrong, but will the program have like a window? will it make it slow? as my code is written to be cross platform and it runs well on all major OS/hardware, so i really have no experience in windows API!!
anthonyrichards
Neuer Beitragender III
2.610Aufrufe
Code:
I am relaxing at home at present and do not have
access to the relevant info,
but will try and get something off on monday.

Regarding your query about 'will it have a window', I'm not sure...
do you run your code as a batch job? This might make things harder,
I'm not sure. since a 'Windows WM_CLOSE' message is referred to, it must follow that
we are talking about running the program on a Windows system.
send_to_alan
Einsteiger
2.610Aufrufe
Thaks for the help!
The jobs are submited to a Condor master, where, it is distributed to condor clients(esentially workstations running win XP pro, with the condor service). Condor runs the jobs at off peak time (after office and weekends) till it is interupted by a user of the machine, whereupon it kills the job. Basically theWM_CLOSE Win32 message is passed before the hard killwhich occurs 10minutes later.
The jobs are serial jobs 1 per machine and save recovery information, if they shut down gracefully the condor service transfers all files back to the sender via the master, if the jobs are running while the hard kill is issued the jobs are lost.
Normally my jobs run in windows with a black DOS box, which will close if the job terminates, condor runs the jobs as an administrator. (these machines are on a network in a university teaching center, 40 of them for now, later on several 100s if we can use them. I did not set the system up, but am a guinea pig!!)
I suppose even if it had a window the window can be made to close on exit? sorry for being so thick but I havent done any windows programming at all. mostly in linux/unix!
These jobs do run on a windows system, and the binaries are all self contained by static linking. the input and output files are transfered back and forth, from sender to client and vice versa.
Thanks
wkramer
Einsteiger
2.610Aufrufe
As Anthony already pointed out, you need a windows message loop to poll the message queue and a main windows procedure that catches the WM_CLOSE message.
A good starting point would be to create a windows application and choose simpleSingle Document Interface (SDI) to create an application framework (File|NewProject|{ntel Fortran Projects|Windows Application|Aplication Settings).
This will provide you with a basic message loop and main windows procedure. All actions your program requires are initiated by messages send through the main windows procedure (you can add actions for opening files, running your personal code etc.). Messages send can be either dispatched by your code (SendMessage) or by user interaction (buttons, menu entries etc.). (In your case probably also by Condor?).
Add WM_CLOSE message to MainWndProc (in a similar way as WM_DESTROY) and add the required action (probably a call to a procedure that will gracefully finish your running program).
Add a call to DestroyWindow to send a WM_DESTROY message.
 
Code:
! WM_CLOSE: Close window
  case (WM_CLOSE)
    !!!! your code !!!!
    ret=DestroyWindow(hwnd)
    MainWndProc = 0
    return
I hope this gives you some idea about how you can proceed to catch WM_CLOSE messages.You better dig into the documentation to make yourself at least abit familiar with windows programming.
Walter Kramer

Message Edited by wkramer on 09-26-2004 01:51 AM

send_to_alan
Einsteiger
2.610Aufrufe
Thanks, will give it a try. Though it seems my code will have to be changed as it is old
and uses lot ofold stuff which may not work correctly with the windows source,
also it uses implicit naming.
Thanks
wkramer
Einsteiger
2.610Aufrufe
I don't think that your "old stuff" will pose a lot of problems.
A very large portion of todays fortran codeis in fact what you call "old stuff".
Fortran programmers are usually not in the business of reinvention (is that an existing word?).
The best way to go about this is it is to keep your old code as much as possible in-tact and separate from your windows interface code.
Justcreate some explixcit interfaces to your old code (or perhaps put your old code in one or more modules).
Implicit naming shouldn't pose a problem (I wouldn't use it for new code though).
Succes,
Walter

Message Edited by wkramer on 09-26-2004 11:48 AM

anthonyrichards
Neuer Beitragender III
2.610Aufrufe
From my experiments, it would appear that you will have more problems than at first thought.
I have found that if your computation engine is started (say some big loop), then you cannot cycle through your message loop, and vice versa. You will almost certainly have to use a multithreaded process, with one thread handling the message loop and the other doing your computations. They can communicate using a flag set when the WM_CLOSE command is recieved. The computation code should test this flag at regular intervals and begin tidying up when its value is changed from its initial value to whatever value you choose. For what it is worth, here is a zipped workspace (CompaqVisulFortran - so you will have to extract the free-format files F90 and maybe create new projects using them) with a project 'helptest' that opens a window and via message box tells you its windows handle. Write it down.
The second program 'closeprogram'' is a simple dialog application with a button which when pressed sends a WM_CLOSE message to the process with the handle that you edit into the FORTRAN (at a place flagged with a comment)before compiling it and executing it. Use the handle value that 'hellotest' displayed'. You will see how this causes 'hellotest' to terminate, with some informativemessageboxes you will have to close yourself.

Message Edited by anthonyrichards on 09-27-2004 05:31 AM

(I have been trying to add the attachment, but it fails miserably..HELP PLEASE STEVE...)

Message Edited by anthonyrichards on 09-27-2004 05:34 AM

anthonyrichards
Neuer Beitragender III
2.610Aufrufe
My final attempt at adding an attachment...the filename is in the attachment box, it is 27K, approx, here goes!
anthonyrichards
Neuer Beitragender III
2.610Aufrufe
Tomorrow (UK time) I will post code for a program 'Hellotest'that creates and opens a window and starts its message loop, and then starts another threadwhich contains a simplecomputational loop, to represent the main computational engine. Message boxes show some of the windows messages that are processed during window creation, and the window handle is displayed in one so that you will know where eventually to send the WM_CLOSE command. I also include a dialog-based application, into which you can enter the previous program's handle and then press a button to send a 'WM_CLOSE' message to it. Message boxes opened by 'Hellotest' after receipt of the command let you know what's happening. The computational loop in 'Hellotest' occasionally tests a global flag 'WMCLOSEFLAG' (initialised to 1) which is changed to -1 after receipt of WM_CLOSE, and then exits the loop when the value -1 is found. As a sample tidying-up exercise, I print some loop values to a text file 'hellotesttidyup.txt' and send a SYSTEM command to display it in Notepad BEFORE issuing the command to finally destroy the 'Hellotest' window.
send_to_alan
Einsteiger
2.610Aufrufe
Hi,
Thank you very much! I d/l and ran the code, it was fantasitc. At least now it seems to be a million times more clearer than before!!
Are you using Compaq visual fortran, because I found the most annoying bug in the Intel Fortran 8.0 for windows, which seems to exist in version 6 for linux, but has been fixed in version 8.0 for Linux! It basically does some integer math wrongly!
I havent been able to trace the exact problem, but code that runs fine in CVF and IFORT V8 for linux, runs but computes wrongly in IFC 8 for Windows and IFC 6 for Linux! Will send a bug report once i can sort it out.
Thanks a lot.
Alan
anthonyrichards
Neuer Beitragender III
2.610Aufrufe
Here is the code as promised. Once you succeed incompiling andexecuting Hellotest, youcan mimic the receipt of WM_CLOSE by just clicking on the close button in the top right corner of the window that opens - this just generates a WM_CLOSE command, which is processed injust the same way.
Otherwise, compile and execute Closeprogram and use it to send the WM_CLOSE to Hellotest.
I guess you will have to change some code, as I use CVF and its modules are given names like DFPORT and DFMT. Let me know if you have problems with my code- or contact Steve Lionel on this board for advice on Intel Fortran, if you need it.
P.S. The SYSTEM command used to open the tidy-uptext file in Notepad is treated differently in WIN9X and WINNT/2000 systems - on the latter (and probably WIN XP also)you will notice that a console window is left open when Notepad opens.

Message Edited by anthonyrichards on 09-28-2004 03:28 AM

send_to_alan
Einsteiger
2.610Aufrufe
Hi,
Thanks a million for the code. Now i am clear about how to go about solving the problem!
I have CVF and Intel fortran as well. I learnt through the hard way that windows associates all visual studio projects to the latest version, so I have to open as the correct version or the project gets corrupted!
Yes a console window opened when notepad opened but closed up when i closed note pad! (i use Win XP home at home, where i a testing the code)
The intel fotran compiler, though fast seems to be very buggy, need to sort that out before i can use it!
Thanks a lot
Alan
anthonyrichards
Neuer Beitragender III
2.610Aufrufe
It is gratifying to learn that you managed to get the code to work as intended. If you used the Intel compiler, I would be interested to now what changes, if any, you hadd to make. Good luck!
anthonyrichards
Neuer Beitragender III
2.610Aufrufe
Here is a modified version of the test program Hellotest.f90 which corrects some messaging errors that previously prevented fast termination of the program using the transmitted handle...
anthonyrichards
Neuer Beitragender III
2.610Aufrufe
Here are some changes that let you use SHELLEXECUTE (instead of SYSTEM) to start NOTEPAD. This prevents the formation of a console window and returns control to the calling window so that it can be immediately terminated/destroyed, leaving just theinformative diagnosticNOTEPAD window.
send_to_alan
Einsteiger
2.610Aufrufe
Thanks will try them out ASAP!!
I havent tried porting the code to IFORT, but will do so when time permits.
Thanks a lot
Alan
Antworten