[RESOLVED] keeping console visible with writing console output to text file
Hello,
I have the following problem with which I would like some help,
I have a program running which normally outputs to the console. I would now like to save this output to a text file. I have tried the following. I now have the text file created and the output written to it. However I no longer can see the console. Is it possible to make it visible or alternatively would it be possible to display the console output into a textbox (I have had no luck with that) while it is writing to the text file?
Dim fs As New FileStream("C:\Test.txt", FileMode.Create) 'see comment
Dim tmp As TextWriter = Console.Out 'see comment
Dim sw As New StreamWriter(fs) 'see comment
While (xlStatus <> XLClass.XLstatus.XL_ERR_QUEUE_IS_EMPTY)
Console.SetOut(sw) 'see comment
' if receiveing succeed....
If (xlStatus = XLClass.XLstatus.XL_SUCCESS) Then
' ...print rx data to console
CANDemo_RxChannel.xlPrintRx(receivedEvent)
Console.SetOut(tmp) 'see comment
End If
End While
'Comment: These are the bit I’ve added to this section of the original program to have it write to text file but the console is no longer visible :(
I would be grateful if I could get any help with this problem.
thanks,
Re: keeping console visible with writing console output to text file
Can nobody really help me with this problem...
Re: HELP: keeping console visible with writing console output to text file
Frankly I'm sure I'm not the only one who is totally lost here. If you no longer output to console then there is no console to see, surely? You can't have two destinations for the same stream. But then I can't see why you would be sending to console in the first place? As I say, just baffled!
Re: HELP: keeping console visible with writing console output to text file
Thank you for your reply. I am using a piece of kit that comes with its codes. "xlPrintRx" outputs to console as only visual output. In an attempt to save the data I added those lines. However I still would like to see what it being recorded real-time. Hence my question regarding whether or not it is possible to have the console window visible. Since this does not appear to be the case, it is possible to populate a textbox with the recieved data realtime?
Re: HELP: keeping console visible with writing console output to text file
Yes, it's possible. If you capture the data in a variable you can then pass that variable round to as many displays and files as you want. There may be some cross threading problems to solve but we can cross that bridge when and if we come to it.
Re: HELP: keeping console visible with writing console output to text file
I've tried to store the output as a string but this causes an error "...cannot be converted to string...". What type of variable would you save the output?
Thanks
Re: [RESOLVED] keeping console visible with writing console output to text file
I've been doing some research and found my solution. Thought it might be good for me to share it. You can find some more information at the following link:
http://msdn.microsoft.com/en-us/libr...nsole.out.aspx
Base on that, this I is what I did to my code:
Public Shared Sub RX_Thread()
' object to be fill with received data
Dim receivedEvent As XLClass.xl_event = New XLClass.xl_event
' result of XL driver func. calls
Dim xlStatus As XLClass.XLstatus = XLClass.XLstatus.XL_SUCCESS
' WaitForSingleObject values
Dim waitResult As WaitResults
Dim fs As FileStream
Dim sw As StreamWriter
While (True)
' wait for hardware events
waitResult = WaitForSingleObject(CANDemo_RxChannel.eventHandle, 1000)
' if event occured...
If (waitResult <> WaitResults.WAIT_TIMEOUT) Then
' init xlStatus first
xlStatus = XLClass.XLstatus.XL_SUCCESS
' while hw queue not empty
While (xlStatus <> XLClass.XLstatus.XL_ERR_QUEUE_IS_EMPTY)
' ...receive data from hardware queue...
xlStatus = CANDemo_RxChannel.xlReceive(receivedEvent)
' if receiveing succeed....
If (xlStatus = XLClass.XLstatus.XL_SUCCESS) Then
' ...print rx data
CANDemo_RxChannel.xlPrintRx(receivedEvent) 'output to console window
fs = New FileStream("C:\Test.txt", FileMode.Append)
sw = New StreamWriter(fs)
sw.AutoFlush = True
Console.SetOut(sw) ' redirect console output to file
CANDemo_RxChannel.xlPrintRx(receivedEvent) 'Data saved into file
' Close previous output stream and redirect output to standard output.
Console.Out.Close()
sw = New StreamWriter(Console.OpenStandardOutput())
sw.AutoFlush = True
Console.SetOut(sw)
End If
End While
End If
' no event occured
End While
End sub