<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tobin Titus : VB6 Migration</title><link>http://blogs.msdn.com/tobint/archive/tags/VB6+Migration/default.aspx</link><description>Tags: VB6 Migration</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>SDK Sample: Programmatically change the runtime version of an application pool in IIS 7</title><link>http://blogs.msdn.com/tobint/archive/2006/06/28/650397.aspx</link><pubDate>Thu, 29 Jun 2006 06:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:650397</guid><dc:creator>Tobin Titus</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/tobint/comments/650397.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tobint/commentrss.aspx?PostID=650397</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tobint/rsscomments.aspx?PostID=650397</wfw:comment><description>I have posted a new sample on IIS.NET demonstrating how to programmatically change the managed runtime version of an application pool in IIS 7.0. I have posted code in VB.NET and C#. Enjoy....(&lt;a href="http://blogs.msdn.com/tobint/archive/2006/06/28/650397.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=650397" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tobint/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tobint/archive/tags/VB6+Migration/default.aspx">VB6 Migration</category></item><item><title>Online Training: Upgrade from VB 6.0 to VB.NET and Absolute Beginners Guide to VB.NET Express Edition</title><link>http://blogs.msdn.com/tobint/archive/2006/05/15/597824.aspx</link><pubDate>Mon, 15 May 2006 11:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:597824</guid><dc:creator>Tobin Titus</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/tobint/comments/597824.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tobint/commentrss.aspx?PostID=597824</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tobint/rsscomments.aspx?PostID=597824</wfw:comment><description>Microsoft Learning has put together a great series of training courses which are available now at a substantial discount. Take advantage of the deal while it lasts (sale ends June 30th)! https://www.microsoftelearning.com/visualstudio2005/#upgradingfromVB60...(&lt;a href="http://blogs.msdn.com/tobint/archive/2006/05/15/597824.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=597824" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tobint/archive/tags/VB6+Migration/default.aspx">VB6 Migration</category></item><item><title>Threading issue with VB.NET Default Instances</title><link>http://blogs.msdn.com/tobint/archive/2006/04/11/DotNetThreading.aspx</link><pubDate>Wed, 12 Apr 2006 03:40:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:573772</guid><dc:creator>Tobin Titus</dc:creator><slash:comments>17</slash:comments><comments>http://blogs.msdn.com/tobint/comments/573772.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tobint/commentrss.aspx?PostID=573772</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tobint/rsscomments.aspx?PostID=573772</wfw:comment><description>&lt;P&gt;Here is an issue that showed up for one of our customers in the Microsoft forums for Visual Basic .NET.&amp;nbsp; It is NOT the typical ”cross-threaded UI update” issue that you might think. No, this one, IMO, is slightly harder to catch since no exception is thrown to let you know something has gone wrong.&lt;/P&gt;
&lt;P&gt;One of the many new features of Visual Basic .NET are &lt;A href="http://msdn.microsoft.com/vbasic/default.aspx?pull=/library/en-us/dnvs05/html/LanguageEnhancements.asp#languageenhancements_topic16" mce_href="http://msdn.microsoft.com/vbasic/default.aspx?pull=/library/en-us/dnvs05/html/LanguageEnhancements.asp#languageenhancements_topic16"&gt;default form instances&lt;/A&gt;.&amp;nbsp; As the feature lists explain, a default form instance prevents you from having to "new up" an instance of a form before acting on it.&amp;nbsp; So, instead of using:&lt;/P&gt;&lt;PRE style="WIDTH: 100%; BACKGROUND-COLOR: #c0c0c0"&gt;  Dim frm As New frmMain
  frm.Show()&lt;/PRE&gt;
&lt;P&gt;I can simply use:&lt;/P&gt;&lt;PRE style="WIDTH: 100%; BACKGROUND-COLOR: #c0c0c0"&gt;  frmMain.Show()&lt;/PRE&gt;
&lt;P&gt;This is handy, particularly for the folks coming from VB6 who are used to forms that behave in this manner.&amp;nbsp; However, default instances present a very interesting problem.&lt;/P&gt;
&lt;P&gt;Take, for instance, a project that I create with a form (named frmMain) and a module (named BackgroundMethods.vb) .&amp;nbsp; For the form, I have&amp;nbsp;simply added a multi-line text box (named txtOutput) and a button control (named button1).&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Here is the code I have in my project:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;[frmMain]&lt;/STRONG&gt;&lt;BR&gt;&lt;/P&gt;&lt;PRE style="WIDTH: 100%; BACKGROUND-COLOR: #c0c0c0"&gt;Imports System.Threading

Public Class frmMain
  Private Sub Button1_Click( ByVal sender As System.Object, _
                             ByVal e As System.EventArgs) _
                             Handles Button1.Click
     Dim t As Thread = New Thread(AddressOf GetData)
     t.Start()
     txtOutput.Text &amp;amp;= "Updates complete"
     ' Break after the call above to read the value
     ' in txtOutput.Text
  End Sub
End Class&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;[BackgroundMethods.vb]&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE style="WIDTH: 100%; BACKGROUND-COLOR: #c0c0c0"&gt;Imports System.Threading
Module BackgroundMethods
   Public Sub GetData()
      WaitForData("Message 1")
      WaitForData("Message 2")
   End Sub
   Public Sub WaitForData(ByVal strMessage As String)
      ' Presumably this method would actually be 
      ' waiting for data from a network connection,&amp;lt;
      ' serial port, or other source
      Thread.Sleep(2000)
      My.Forms.frmMain.txtOutput.Text &amp;amp;= (vbCrLf &amp;amp; Now().ToShortTimeString() &amp;amp; _
      vbTab &amp;amp; strMessage)
      ' Break after the call above to read the value
      ' in My.Forms.frmMain.txtOutput.Text
   End Sub
End Module&lt;/PRE&gt;
&lt;P&gt;So the simple example is that I have a button which, when clicked, will execute&amp;nbsp;the "GetData"&amp;nbsp;method asynchronously in a module.&amp;nbsp; That method is going to call the WaitForData method that updates the UI with two different messages ("message1" and "message2").&amp;nbsp; WaitForData is supposed to simulate a long-running process, so I threw in the typical "thread.sleep" call to make this illusion.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;If you run this code, you will notice that no exceptions are thrown, but the UI for your form is also not updated.&amp;nbsp; Why is this?&amp;nbsp; You would have at least expected a cross-thread exception, right?&lt;/P&gt;
&lt;P&gt;In any other managed language, this likely wouldn't happen -- namely because the "My" application is specific to VB, as well as default form instances!&amp;nbsp; In C#, if I try to update the UI from another thread, I would get an exception stating: "Cross-thread operation not valid: Control 'txtSerialIn' accessed from a thread other than the thread it was created on." which could be solved by using the "Invoke" method.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The issue with Visual Basic, in this instance, is that the default form instances are thread-specific.&amp;nbsp; So, when I try to access the form using My.Forms.frmMain from within the worker thread, a &lt;STRONG&gt;&lt;EM&gt;NEW&lt;/EM&gt;&lt;/STRONG&gt; default instance is created under the covers.&amp;nbsp;&amp;nbsp;My calls to update the text box are then executed on the NEW instance of the form which resides in the same thread as the call to update it -- hence it doesn't throw a cross-thread exception. In the mind of the VB program, the request to update the textbox occurred without an error.&amp;nbsp; When the worker thread dies, the second instance of the form (which was never displayed) is now a candidate for garbage collection.&amp;nbsp; Meanwhile, back on the original thread and original form the textbox is left blank.&amp;nbsp; You can validate this by running the program (I have attached sample code to this blog post) and setting breakpoints on the textbox update line in the WaitForData method, and in the last line of the button1_click event handler of the code.&amp;nbsp; You will notice that after the second call to WaitForData (before the debugger exits the Sub) that My.Forms.frmMain.txtOutput.Text has been properly set to the value you expected.&amp;nbsp; However, remember that this is on a second background instance of the form, not the original one you expected.&amp;nbsp; Once the debugger hits the last line in the button1_click event handler, read the value of txtOutput.Text and realize THAT instance of the textbox was never updated.&lt;/P&gt;
&lt;P&gt;So what is&amp;nbsp;the solution?&lt;/P&gt;
&lt;P&gt;First off, you still have to use the "&lt;A href="http://codemilitia.com/blogs/tobin.titus/archive/2005/04/10/12.aspx" mce_href="http://codemilitia.com/blogs/tobin.titus/archive/2005/04/10/12.aspx"&gt;Invoke solution&lt;/A&gt;" that is often times bandied about in threading discussions.&amp;nbsp; To do this in VB.NET (and particularly in our solution), do the following:&lt;/P&gt;
&lt;P&gt;1. Add the following code to the top of your BackgroundMethods.vb file:&lt;/P&gt;&lt;PRE style="WIDTH: 100%; BACKGROUND-COLOR: #c0c0c0"&gt;        Delegate Sub UpdateTextHandler(ByVal strMessage As String)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; This allows you to create a delegate that can be invoked on the UI.&lt;/P&gt;
&lt;P&gt;2. Add the following method to your frmMain file:&lt;/P&gt;&lt;PRE style="WIDTH: 100%; BACKGROUND-COLOR: #c0c0c0"&gt;    Public Sub UpdateTextMethod(ByVal strMessage As String)
       txtOutput.Text &amp;amp;= (vbCrLf &amp;amp; Now().ToShortTimeString() &amp;amp; vbTab &amp;amp; strMessage)
    End Sub&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; This creates the method that will actually be executed via your delegate &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; from the worker thread.&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp;Change your WaitForData method as follows:&lt;/P&gt;&lt;PRE style="WIDTH: 100%; BACKGROUND-COLOR: #c0c0c0"&gt;    Public Sub WaitForData(ByVal strMessage As String)
        ' Presumably this method would actually be 
        ' waiting for data from a network connection
        ' serial port, or other source
        Thread.Sleep(2000)
        Dim f As frmMain = My.Application.OpenForms("frmMain")
        f.Invoke(New UpdateTextHandler(AddressOf f.UpdateTextMethod), _  
                 New Object() {strMessage})
    End Sub&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; This method now uses the form's "Invoke" method (actually defined in&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Control)&amp;nbsp;to execute UpdateTextMethod on the original form via the&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UpdateTextHandler delegate.&lt;/P&gt;
&lt;P&gt;So what is happening is that your new thread is getting an instance to the existing&amp;nbsp;frmMain instance by going through the OpenForms call.&amp;nbsp; Once I have that instance, I can invoke a delegate that points to the "UpdateTextMethod" of the existing form (&amp;nbsp;passing in the message in the object array ).&amp;nbsp; By invoking, I am able to get back on the UI's thread and that call can execute any updates to the UI that it wishes.&lt;/P&gt;
&lt;P&gt;Keep this in mind the next time you are not receiving errors and your UI isn't getting updated how you would expect -- particularly if you code communicates with the network, a serial device, or other device which communicates asynchronously.&lt;/P&gt;&lt;PRE&gt;&lt;/PRE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=573772" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/tobint/attachment/573772.ashx" length="108896" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/tobint/archive/tags/VB6+Migration/default.aspx">VB6 Migration</category><category domain="http://blogs.msdn.com/tobint/archive/tags/Threading/default.aspx">Threading</category></item><item><title>Free Microsoft Press Book: Introducing Microsoft Visual Basic 2005 for Developers</title><link>http://blogs.msdn.com/tobint/archive/2006/04/04/free-microsoft-press-book-introducing-microsoft-visual-basic-2005-for-developers.aspx</link><pubDate>Tue, 04 Apr 2006 20:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:568269</guid><dc:creator>Tobin Titus</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/tobint/comments/568269.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tobint/commentrss.aspx?PostID=568269</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tobint/rsscomments.aspx?PostID=568269</wfw:comment><description>&lt;P&gt;Microsoft has a free book available for download for anyone who wants it.&amp;nbsp; Download the whole book, or download it chapter-by-chapter (8 in all).&amp;nbsp; Check it out at &lt;A href="http://msdn.microsoft.com/vbasic/learning/introtovb2005/" mce_href="http://msdn.microsoft.com/vbasic/learning/introtovb2005/"&gt;http://msdn.microsoft.com/vbasic/learning/introtovb2005/&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=568269" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tobint/archive/tags/VB6+Migration/default.aspx">VB6 Migration</category></item><item><title>202 VB.NET Samples</title><link>http://blogs.msdn.com/tobint/archive/2006/04/03/202-vb-net-samples.aspx</link><pubDate>Tue, 04 Apr 2006 02:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:567723</guid><dc:creator>Tobin Titus</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/tobint/comments/567723.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tobint/commentrss.aspx?PostID=567723</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tobint/rsscomments.aspx?PostID=567723</wfw:comment><description>I just wanted to bring some attention to a resource that I think is extremely valuable for VB developers (and really, any .NET developer using any other language for that matter).&amp;nbsp; MSDN has a website with &lt;A href="http://msdn.microsoft.com/vbasic/downloads/code/101samples/" mce_href="http://msdn.microsoft.com/vbasic/downloads/code/101samples/"&gt;101 Code Samples for VB.NET 2005&lt;/A&gt;.&amp;nbsp; There is also another website for those of you that haven't upgraded to 2005 yet: &lt;A href="http://www.microsoft.com/downloads/details.aspx?familyid=08e3d5f8-033d-420b-a3b1-3074505c03f3&amp;amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?familyid=08e3d5f8-033d-420b-a3b1-3074505c03f3&amp;amp;displaylang=en"&gt;101 Code Samples for VB.NET 2003&lt;/A&gt;.&amp;nbsp; Both of these sites have very valuable sample code for doing things such as network programming, using regular expressions, executing transactions, handling/changing ACL’s on files, accessing data, performing bulk data transactions, executing asynchronous operations (one of my favorite topics), using ClickOnce, creating ASP.NET pages and more!&amp;nbsp; It is a great resource for those of you that just want to get it done and don't want to waste a lot of time reading specifications on each class in System.Net or System.Data.&amp;nbsp; The samples are robust and provide some great starting points for anyone interested in the many topics available in these samples.&amp;nbsp; Check them out and let me know what you think!&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=567723" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tobint/archive/tags/VB6+Migration/default.aspx">VB6 Migration</category></item></channel></rss>