PaleNeutron
PaleNeutron

Reputation: 3215

How to change my Fortran code to run it as a background process?

I have written a Fortran program on Windows and now I want to make it run as a background process. My code is something like this:

PROGRAM test
implicit none
integer a
a = calculator()
print *, a !now I print the result on the screen and I can also write it to a file.
END PROGRAM

It takes about 10 minutes to get the result and I will call it many times in an other program.

I know in C++ we can simply change /console to /window to hide the command window but it seems that does not work in Fortran.

The command window really bothered me, is there an easy way to make the black window disappear?

Upvotes: 2

Views: 911

Answers (1)

Steve Lionel
Steve Lionel

Reputation: 7267

Well, no, in C++ it takes a bit more.

While I can tell you how to make your Fortran code run without a window, where do you want the screen output to go?

Here's the basics of making the code run without a window. This doesn't make it run "in the background", though. You may need more code to do that. (If your compiler supports EXECUTE_COMMAND_LINE, you can specify WAIT=.FALSE. to run it asynchronously...)

First, you want to have the program built as a Windows, not Console application. Typically this is done with the linker option /subsystem:windows (instead of console).

Now make your Fortran code look like this (this is for Intel Fortran):

integer(DWORD) function WinMain( hInstance, hPrevInstance, lpszCmdLine, nCmdShow )
!DEC$ ATTRIBUTES STDCALL, DECORATE, ALIAS : 'WinMain' :: WinMain

use ifwinty

implicit none

integer(HANDLE) hInstance
integer(HANDLE) hPrevInstance
integer(LPVOID) lpszCmdLine
integer(DWORD) nCmdShow

... Your code here

WinMain = 0
return
end function WinMain

You are changing the code from a program to a function. It will run without any visible user interface.

Upvotes: 4

Related Questions