Welcome to MSDN Blogs Sign in | Join | Help

Threading issue with VB.NET Default Instances

This post has been moved to my personal blog: Tobin Titus

Published Tuesday, April 11, 2006 5:40 PM by Tobin Titus
Filed under: ,

Attachment(s): DefaultInstanceProblem.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Threading issue with VB.NET Default Instances

This is totally lame - instead I'd just use

frmMain = ExtendedConvert(frmMain) Mwuahahahahaha
Thursday, April 20, 2006 3:34 PM by Bill

# re: Threading issue with VB.NET Default Instances

Bill, as always, your smallest contributions typically make me laugh the most.  Thanks dude! :)
Thursday, April 20, 2006 3:41 PM by Tobin Titus

# re: Threading issue with VB.NET Default Instances

Thank you, very good explanation.  I have been trying to figure this out for a while now.  What really threw me off was that the .Text was updating in the debuggin watch window.  So i spent my time trying .Refresh() etc...

-Thanks, A
Monday, May 15, 2006 10:22 AM by Aaron

# re: Threading issue with VB.NET Default Instances

Bless you, my son!  My use of invoke was causing my application to hang if the desktop was locked and unlocked, and this solved that problem.
Monday, June 19, 2006 4:47 PM by Tony Hinkle

# re: Threading issue with VB.NET Default Instances

No problem at all Tony. I'm glad it worked for you.
Monday, June 19, 2006 5:34 PM by Tobin Titus

# re: Threading issue with VB.NET Default Instances

I couldn't figure out why my textbox wasn't updating. Everything makes so much sense now!
Thank you a lot!
Tuesday, June 27, 2006 3:02 AM by marky

# re: Threading issue with VB.NET Default Instances

Glad you enjoyed it Marky. Good luck.
Tuesday, July 11, 2006 5:03 PM by Tobin Titus

# re: Threading issue with VB.NET Default Instances

Great post, i have solved my problem. Thanks a lot.

Friday, March 09, 2007 12:58 PM by Minh Thanh

# re: Threading issue with VB.NET Default Instances

Thanx for the post. I've also solved my problem. Greetz from Holland

Wednesday, June 20, 2007 5:15 AM by Duckie

# re: Threading issue with VB.NET Default Instances

I'm having a problem.  I have console app that creats a form to display its output unless you pass it a command line parameter telling it to run quietly.  The program runs fine until it tries to update the output form from an async callback.

The module that contains Sub Main() also contains:

   Delegate Sub OutputPrintCallback(ByVal OutputText As String)

   Public Sub OutputPrint(ByVal OutputText As String)

       If OutputForm IsNot Nothing Then OutputForm.OutputTextBox.Invoke(New OutputPrintCallback(AddressOf OutputForm.OutputPrint), New Object() {OutputText})

       If LogStream IsNot Nothing Then LogStream.Write(vbCrLf & Now.ToString("hh:mm:ss - ") & OutputText)

       Debug.Write(vbCrLf & Now.ToString("hh:mm:ss - ") & OutputText)

       Application.DoEvents()

   End Sub

and on the Log Form:

Public Class LogForm

   Public Sub OutputPrint(ByVal OutputText As String)

       OutputTextBox.Text &= vbCrLf & Now.ToString("hh:mm:ss - ") & OutputText

       OutputTextBox.SelectionStart = OutputTextBox.Text.Length

       OutputTextBox.ScrollToCaret()

   End Sub

End Class

This works well until an async thread tries to OutputPrint. Then everything just locks up.  

Everything is started from Sub Main, the form is just used for output.

OutputForm is an instance of LogForm created of the program's not running in quiet mode.

Any suggestions?

Should the Delegate be in the Form or the Module?

Friday, July 13, 2007 7:23 PM by Patrick

# re: Threading issue with VB.NET Default Instances

Thanks goodness someone understands this stuff.

You have saved yet another .NET weakling from a slow lingering death, racking their brains to find out why , oh WHY isn't this control updating?

Friday, August 03, 2007 6:04 PM by John

# re: Threading issue with VB.NET Default Instances

Very neat! I had the same wacky problem too. Since I only have one form I used .OpenForms(0) instead in my app.

Tuesday, August 21, 2007 6:43 AM by passbyer

# re: Threading issue with VB.NET Default Instances

Excellent.. this solved my problem with updating my form from my ManagementEventWatcher!

Thanks!

Tuesday, September 04, 2007 3:11 PM by Bas van den Berg

# re: Threading issue with VB.NET Default Instances

If your using multiple threads you will get a first chance exception thrown if two threads want to update the same form. Use this code to check to see if you successfully pulled a reference to your form,

Public Sub UpdateStatusLabel(ByVal strMessage As String())

       Try

            Dim f As Form1 = My.Forms.Form1.ActiveForm

               If f.ActiveForm Is Nothing Then

                   Debug.Write("Nothing" & vbNewLine)

               Else

                   f.Invoke(New UpdateStatusLabelHandler(AddressOf f.UpdateStatusLabelMethod), _

                            New Object() {strMessage})

               End If

       Catch ex As Exception

       End Try

   End Sub

Wednesday, January 30, 2008 1:43 PM by Jeff Perkins

# re: Threading issue with VB.NET Default Instances

Thank you so much!!!

I had a similar problem but mine was obfuscated by 3 levels of events and a module.  After definitely diagnosing the problem as a separate instance of my form, I went searching for answers and eventually found your article, and realized I was calling the default instance of the form!  Again, thanks for your clear explanation of a complicated problem.

-Ross

Thursday, May 15, 2008 4:40 PM by C. Ross Eskridge

# re: Threading issue with VB.NET Default Instances

A forum friend used this page to explain a similar Problem for a text box not updating on a networking threaded solution. Basically, after delegating and invoking the text box - everything works like a charm now!

Thanks!

Sunday, October 05, 2008 7:07 PM by Alexander

# re: Threading issue with VB.NET Default Instances

Thank you Tobin! It is working perfectly!

Regards form Slovakia.

Thursday, November 20, 2008 1:30 PM by IDzOR

# re: Threading issue with VB.NET Default Instances

Thanks for your explanation. I was trying to figure that out for about 2 hours :)

Greets from Latvia :)

Wednesday, February 11, 2009 11:19 AM by Tomy

# re: Threading issue with VB.NET Default Instances

Thank you, thank you, thank you!  This has been driving me mad!

Monday, March 23, 2009 10:44 AM by Finn

# re: Threading issue with VB.NET Default Instances

More thanks...I had tired numerous google search postings on vb.net threading and this is the only one that actually worked. All the others would have some sort of fatal flaw that caused run time errors or just plain would not compile. Even on the code posted on MSDN for VS 2005(which is what I am using) failed.

With this code, I have a program as a basis for learning about threading.

Thanks again!

Tuesday, April 07, 2009 9:29 PM by bartj

# re: Threading issue with VB.NET Default Instances

Just want to say thanks for explaining the solution to a problem that has wasted a lot of time so far this afternoon, and would have wasted a lot more were it not for your post!

Thursday, April 16, 2009 3:24 PM by Ian

# re: Threading issue with VB.NET Default Instances

Thanks Tobin!

Been googling for a over day for a solution for this problem!

Tuesday, December 22, 2009 1:47 AM by Peter

# re: Threading issue with VB.NET Default Instances

Thanks a ton! Now how do you determine if the current thread is the main form or not.

Sunday, January 10, 2010 11:42 AM by Joe

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker