2010-09-27 26 views
0

我正在silverlight 4和WinForms(net4)中構建一個IronRuby控制檯。我可以將輸出重定向沒有問題:如何將ironruby的輸入重定向到一個文本框(WinForms和Silverlight 4)

MyRuntime = Ruby.CreateRuntime(); 
msOutput = new MemoryStream(); 
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8); 
MyEngine = MyRuntime.GetEngine("rb"); 
MySource = MyEngine.CreateScriptSourceFromString("a='123'\nputs a", SourceCodeKind.Statements); 
MySource.Execute(); 
textBox2.Text = ReadFromStream(msOutput); 

現在,我想重定向輸入還可以,但總是讓從腳本一個「零」:

MyRuntime = Ruby.CreateRuntime(); 
msOutput = new MemoryStream(); 
msInput = new MemoryStream(); 
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8); 
MyRuntime.IO.SetInput(msInput, Encoding.UTF8); 
MyEngine = MyRuntime.GetEngine("rb"); 
MySource = MyEngine.CreateScriptSourceFromString("a=gets\nputs a", SourceCodeKind.Statements); 
byte[] byteArray = Encoding.UTF8.GetBytes("123"); 
msInput.Write(byteArray, 0, byteArray.Length); 
MySource.Execute(); 
textBox2.Text = ReadFromStream(msOutput); 

我找不到重定向的任何樣本輸入,請你舉個例子嗎?謝謝。

回答

1

我沒有任何示例代碼立即可用,但不需要使用MemoryStream,而需要實現流。在讀取流時,您需要將文本框的「內容」發送到流。您需要一些機制來確定您何時發送內容 - 例如當用戶點擊返回時。您可能還需要設置一個阻塞讀取的線程,並可能使用AutoResetEvent阻塞,直到文本框指示輸入已完成。

相關問題