how to interact with a thread

how to interact with a thread

I have a windows app. doing post-processing of computational results from an older console driven app. The console app. is run as a thread started from the windows app. I have now found out that I would like to be able to send 'commands' from the windows app. to the console app. which is prompting/waiting for user input via a READ statement.

which is the prefered (a possible?) way to do this?


7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

The console app. is run as a thread started from the windows app

The sentence above, as is, is meaningless -- an application cannot be run in thread. Did you just spawned the old app using CreateProcess/RUNQQ (in which case, there's no new thread, but a new process) or did you turned it to a subroutine and ran it via CreateThread?


Hi Jugoslav,

the console 'app.' has been turned into a subroutine and it is launched by a CreateThread call.


Well, certainly the clearest and best method would be to recode the old app to receive its inputs through normal arguments; (since thread functions allow only a single argument, you can pack all required arguments into a TYPE and pass that).

If that's not acceptable for some reason (you mustn't/don't want to change the old code), try (untested):

hStdIn = GetStdHandle(STD_INPUT_HANDLE)
IF (.NOT.WriteConsoleInput(hStdIn, LOC("2.3456"C), &
LEN("2.3456"C), LOC(nBytes)) &
   iErr = GetLastError()

If it fails, try WriteFile instead. By the way, Fortran run-time library must use same calls to ReadFile/WriteFile or ReadConsoleInput/WriteConsoleInput underneath, so maybe Write(5,"('2.3456',/)") will succeed as well (if it's not protected internally).

For simple synchronization, global LOGICAL variables are usually sufficient (so that one thread knows when it should read/write). WriteFile/WriteConsoleInput are buffered so this may not be even necessary.


...I overlooked that WriteConsoleInput doesn't take a simple CHARACTER(*), but an INPUT_RECORD structure. So, WriteFile, if it works, should be simpler. Note also that you need a CHAR(13) at the end of char buffer to simulate ENTER key.

many thanks Yugolav,
it works like a charm with WriteConsoleInput.
I havn't tried WriteFile.

sorry about the sloppy spelling
thanks Jugoslav

Leave a Comment

Please sign in to add a comment. Not a member? Join today