Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd29a5e2

Browse files
v2mKevinRansom
authored andcommitted
reimplement async reading from redirected stdout\stderr
1 parentf0eca16 commitd29a5e2

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

‎vsintegration/src/vs/FsPkgs/FSharp.VS.FSI/sessions.fs‎

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,48 @@ let determineFsiPath () =
171171
else
172172
raise(SessionError(VFSIstrings.SR.couldNotFindFsiExe fsiRegistryPath))
173173

174+
letreadLinesAsync(reader:System.IO.StreamReader):IEvent<string>=
175+
letnewLine= Event<_>()
176+
letbuffer= System.Text.StringBuilder(1024)
177+
letbyteBuffer= Array.zeroCreate128
178+
letencoding= System.Text.Encoding.UTF8
179+
letdecoder= encoding.GetDecoder()
180+
letasync0= async.Return0
181+
letcharBuffer=
182+
letmaxCharsInBuffer= encoding.GetMaxCharCount byteBuffer.Length
183+
Array.zeroCreate maxCharsInBuffer
184+
185+
let recfindLinesInBuffer pos=
186+
if pos>= buffer.Lengththen max(buffer.Length-1)0// exit and point to the last char
187+
else
188+
letc= buffer.[pos]
189+
if c='\r'|| c='\n'then
190+
letline= buffer.ToString(0, pos)
191+
newLine.Trigger line
192+
193+
letdeletePos=
194+
if c='\r'&&(pos+1)< buffer.Length&& buffer.[pos+1]='\n'then pos+2else pos+1
195+
buffer.Remove(0, deletePos)|> ignore
196+
findLinesInBuffer0
197+
else
198+
findLinesInBuffer(pos+1)
199+
200+
let recread pos=
201+
async{
202+
let!bytesRead=
203+
try
204+
reader.BaseStream.AsyncRead(byteBuffer,0, byteBuffer.Length)
205+
with
206+
|:? IOException-> async0
207+
if bytesRead<>0then
208+
letcharsRead= decoder.GetChars(byteBuffer,0, bytesRead, charBuffer,0)
209+
buffer.Append(charBuffer,0, charsRead)|> ignore
210+
letnewPos= findLinesInBuffer pos
211+
return! read newPos
212+
}
213+
Async.StartImmediate(read0)
214+
newLine.Publish
215+
174216
letfsiStartInfo channelName=
175217
letprocInfo=new ProcessStartInfo()
176218
letfsiPath= determineFsiPath()
@@ -208,16 +250,13 @@ let fsiProcess (procInfo:ProcessStartInfo) =
208250
letoutW,outE=lete=new Event<_>()in e.Trigger, e.Publish
209251
leterrW,errE=lete=new Event<_>()in e.Trigger, e.Publish
210252
letexitE=(cmdProcess.Exited|> Observable.map(fun x-> x))// this gives the event the F# "standard" event type IEvent<'a> rather than IEvent<_,_>
211-
212-
// wire up output (to both stdout and stderr)
213-
cmdProcess.OutputDataReceived|> catchAll|> Observable.add(fun data->
214-
//System.Windows.Forms.MessageBox.Show (sprintf "OutputDataRecieved '%s'\n" data.Data) |> ignore
215-
outW(data.Data));
216-
cmdProcess.ErrorDataReceived|> catchAll|> Observable.add(fun data-> errW(data.Data));
217253

218254
let_= cmdProcess.Start()
219-
cmdProcess.BeginOutputReadLine();
220-
cmdProcess.BeginErrorReadLine();
255+
// wire up output (to both stdout and stderr)
256+
readLinesAsync cmdProcess.StandardOutput|> catchAll|> Observable.add(fun data->
257+
//System.Windows.Forms.MessageBox.Show (sprintf "OutputDataRecieved '%s'\n" data.Data) |> ignore
258+
outW(data));
259+
readLinesAsync cmdProcess.StandardError|> catchAll|> Observable.add(fun data-> errW(data));
221260

222261
// wire up input
223262
// Fix 982: Force input to be written in UTF8 regardless of the apparent encoding.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp