how to interact with a thread

how to interact with a thread

Hi,
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?

/bjrn

7 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.

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?

Jugoslav

Jugoslav www.xeffort.com

Hi Jugoslav,

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

regs
/Bjrn

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):

INTEGER hStdIn
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.

Jugoslav

Jugoslav www.xeffort.com

...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.

Jugoslav www.xeffort.com

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

sorry about the sloppy spelling
thanks Jugoslav
/Bjrn

Kommentar hinterlassen

Bitte anmelden, um einen Kommentar hinzuzufügen. Sie sind noch nicht Mitglied? Jetzt teilnehmen