Mini C# Lab ver 1.2 supports VB.NET now
10 |
After Mini C# Lab 1.1 was released, the most frequently asked quesitons are: 1) Could it support VB.NET? 2) Where is the source code?
Now, here comes Mini C# Lab ver 1.2 and some wishful features are added.
First, it supports VB.NET now! Yes, **VB.NET**!! Although it's Mini "C#" Lab, many people still want to use it to test VB.NET code. I found that the CodeDomProvider provides the abstract class concept, so we can use the almost same code to add VB.NET support easily. Since it's not difficult at all, thanks to .NET RD team, I put the VB.NET support into Mini C# Lab ver 1.2.
Second, since many people asked about the source code, here you are. In my life of coding, I learned a lot from open source projects and always believe the spirit of sharing over Internet makes the world better day by day. So, here is my two cents, I have put Mini C# Lab on CodePlex now, if you are interested in how it works, you can find the source code there. One more thing, if you find any lousy or dirty code inside my source, please let me know.
Since it's on CodePlex now, besides my blog, you can give your feedback on CodePlex, too. Thank you!
[中文說明]
Mini C# Lab釋出後,最常被問到的兩個問題是: 1) 可不可以支援VB.NET呢? 2) 哪裡可以下載原始碼?
薑 薑 薑 薑~~~ Mini C# Lab ver 1.2來了,裡面加了幾個大家渴望的功能。
第一,支援VB.NET,是的,VB.NET!! 雖然這個專案叫做Mini "C#" Lab,但詢問VB.NET的人真不少。我研究了一下CodeDomProvider,發現它具備抽象類別的觀念,因此加入VB.NET動態編譯支援比想像中簡單許多。原本想丟出原始碼讓需要VB.NET的人自己動手,一時發了佛心,就順手把VB.NET的功能加進去,希望大家會喜歡。:P
第二,原始碼大放送! 回首自己學習程式的歷程,一路上也是喝Internet眾多Open Source Code的奶水長大的,而我一直深信這種分享精神是讓世界快速進步的重要原動力。由於這個專案算是目前做過的幾個小工具中嚴謹度較高的,較上得了檯面,索性連同Source Code放上CodePlex分享給有興趣的人囉! 如果有人發現程式裡用了什麼歪七扭八的怪寫法,願意開示更簡潔的有效做法,也請發揮愛心,留個言大力指點一下。
Comments
# by Jasper
謝謝您,望爾後小弟也能將自己所學分享給大家...(您真是好人...給你好人卡一張...:))
# by rosbicn
拜读了你的代码,谢。提个小小建议。你重定向Console输出的时候,是建立了一个MemorySteam,用定时器触发代码,反复从MemorySteam里面读出内容,然后写入RichTextBox。这样似乎是兜了个圈子。我建议写个类,继承TextWriter,把RichTexeBox传递进去,然后Override方法Write(String),直接用RichTextBox输出就可以。这样不需要用定时器,Console.Write的同时,屏幕上也有了。小小建议,盼考虑之。
# by Jeffrey
to rosbicn, 當初之所以繞圈子,另外用Timer去讀資料再寫入RichTextBox,主要是因為我用了另了一條Thread去跑程式,如果Control直接透過TextWriter去更新了RichTextBox,怕會產生Cross-Thread Operation Exception(http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/09/30/tips-about-ui-thread-limitation.aspx)。不過這次用的是Console.SetOut,是否能避開UI Thread限制,我倒沒實測過,有空再來玩看看。謝謝您的建議~~~
# by sholfen
感謝開放原始碼,這樣我也可以偷來改一下...(逃)
# by chicken
記得透過 delegate 或是用 event 是可以達到跨 threads 進行溝通的動作的... 包裝成一個 TextWriter 應該是個可行的作法... 參考看看 :D
# by rosbicn
to Jeffery, 正如你前文所说,不同Thread如果出现冲突,无非是用Invoke或BeginInvoke解决。只要在Override Write中做一下就可以。我的程序中恰好有一个函数是跨Thread写入ListView的,用了Invoke,参考你的代码后,用CodeDom为程序增加了个Addon的小功能,Console重定向用的就是继承TextWriter,采用了前述函数,测试下来完全没有问题。
# by 小熊子
試用了一下,很簡單使用,建議 Build succeeded in ....sec 沒有 Run succeded in ....sec 按了半天,沒有 Console.WriteLine 時,不知道是在 prepare to run 還是 run completed ?!
# by shallow
請教各位大大,關於Invoke and Delegate有一種寫法,在一般書上都沒寫的用法,因為是初學者查了很多資料還是看不懂,所以是否各位大大們能否幫忙加註解,或是哪裡可以找到解答,或是如何簡化的。 原程式: http://coad.net/Blog/Resources/SerialPortTerminal.zip private void Log(LogMsgType msgtype, string msg) { rtfTerminal.Invoke(new EventHandler(delegate { rtfTerminal.SelectedText = string.Empty; rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont, FontStyle.Bold); rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype]; rtfTerminal.AppendText(msg); rtfTerminal.ScrollToCaret(); })); }
# by Jeffrey
to shallow, 由程式看起來,Log有可能在多執行緒模式下執行,當它試圖將文字加入rtfTerminal時,會改變rtfTerminal的顯示,違反了只有UI Thread才能更動UI元素的鐵律,所以就用Invoke的方式間接將程式碼交給UI Thread執行。參考: http://blog.darkthread.net/post-2007-09-30-tips-about-ui-thread-limitation.aspx
# by shallow
to Jeffrey, 這是marlon(marlon)大大回的。 還在研究中... 把 delegate {} 裡面的程式碼編譯成一個 EventHandler 型別的方法委派給 rtfTerminal 等同 private void Log(LogMsgType msgtype, string msg) { rtfTerminal.Invoke(new EventHandler(tmpMethod)) } void tmpMethod(object sender, EventArgs e) { rtfTerminal.SelectedText = string.Empty; rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont, FontStyle.Bold); rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype]; rtfTerminal.AppendText(msg); rtfTerminal.ScrollToCaret(); }