Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application (mine) that uses WriteFile() to write directly to the console, but I also have to share that console with another application that (as far as I know writes to stdout). Does anybody know of a way that I can force synchronisation of the output - so that there is no output overlap/corruption. My application uses a console lock to prevent this happening - but I have no control over a third-party app.

Regards.
James
Posted
Comments
Philippe Mori 20-Aug-11 11:48am    
The short answer is : You cannot do that.

The only solution I can think of if you don't have control over other applications would be to redirect the output of other application to your application and then code your application to write the output of other application at appropriate time.

You might want to scan the output of other applications to decide how to split the data (for example to accumulate complete line or find some known patterns).

You might also have some timeout so that if the other application do not print a whole line in a time frame, you would output what you have.
 
Share this answer
 
The thing is - I do not launch the third-party app. I have no control over it what-so-ever - so redirected output is not an option. Does anyone know of an internal lock that gets set when writing to stdout/err. I know the new version of C++ (0x) includes thread-safe awareness - but any tip would be appreciated.

James.
 
Share this answer
 
Comments
mbue 20-Aug-11 15:10pm    
Please dont post your response not as solution, rather as comment to the appropriate solution or comment.
Regards.
Philippe Mori 20-Aug-11 19:47pm    
Effectively... Worst by responding at the wrong place, the person for which the answer is intended would not receive an email notification.... thus might never respond to your comment. By chance, I was curious if someone else would have another solution as I don't think it is possible to do what you want.
Philippe Mori 20-Aug-11 19:38pm    
As far as I know stdout/err are thread-safe to use but the result will be intermixed. Flushing after every line/block might help having whole line together but I don't think that there is a way to guarantee that any given block of text is outputted sequentially without content from other applications.

From my understanding it works that way from decades... and I don't expect it will change. As I said, the only way I think that it would be possible to have the control would be that your application would receive the output from other ones and scan the result to make it somehow ordered.

Anyway, if there would be a trick, then you would have to modify other applications to uses that trick as you have noticed, without doing anything output is not previsible but intermixed.

Thus even if your application would write at proper time, any other application that would not have been coded to use that lock (if it would exist), would write while yout application is writing since it does not check the lock...
TenFiftyTwo 21-Aug-11 4:58am    
I agree .. I am beginning to think that this is not possible afterall! Thank you for your update. Much appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900