Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Update Textbox from a thread

Avatar of jmsjms
jmsjmsFlag for United Kingdom of Great Britain and Northern Ireland asked on
.NET ProgrammingVisual Basic.NET
20 Comments1 Solution2306 ViewsLast Modified:
Hi Experts,

I'm trying to update a text box with a variable from a thread.  I've tried to get my head around delegates but the code below still doesnt work.

When I step through it, it seems that it fails the  Me.txtEvent.InvokeRequired() check when I would have thought it would be true.

Any idea where I'm going wrong?  BTW, this is a test project for a thread that will eventually report back at at various places in the code so I dont want to use a backgroundWorker thread.

Thanks

' Function pointer to method to update GUI
	Private Delegate Sub UpdateGUIDelegate(ByVal Bval As Integer)

	' Method to update the GUI
	Public Sub UpdateGUI(ByVal Bval As Integer)
		' Check to see who is calling this method. If other then GUI thread
		' create delegate to make the call.
		If Me.txtEvent.InvokeRequired() Then
			Dim myD1 As New UpdateGUIDelegate(AddressOf UpdateGUI)
			Me.txtEvent.Invoke(myD1, Bval)
		Else
			Me.txtEvent.Text = "A=" & Bval
		End If
	End Sub
#End Region


The code that the thread runs (in a seperate class)
	Public Sub WorkCount()
		While intA < 10000
			While intB < 100
				intB += 1
				System.Threading.Thread.Sleep(50)
			End While
			Form1.UpdateGUI(intA)
			intB = 0
			intA += 1
		End While
		intA = 0
	End Sub
ASKER CERTIFIED SOLUTION
Avatar of Yannick Lapierre
Yannick LapierreFlag of France imageSoftware Director

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 20 Comments.
See Answers