Me.invoke in forms

  • Thread starter Thread starter spiazzi
  • Start date Start date
S

spiazzi

HI,
I have a form with a progress bar a and a label.
I start a thread in form load that update the value
of the progress bar and the label.
For label I have use :
me.invoke(new EventHandler(AdressOf SafeUpdate))

where SafeUpdate is a method of the form that update the
label with a text.

This beacuse update label in a thread causes the application to time wait.

But when I call the form from the main method of my module, the application
stop in the
me.invoke(new EventHandler(AdressOf SafeUpdate)),
and nothings happen.

How can I do?
 
Show us the full code and in particular SafeUpdate.

Also can you elaborate on "nothings happen". Exception, freeze/hung, what?

Cheers
Daniel
 
freeze:



....
public strLabel as String

Private Sub FrmLoad_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load


Dim screen As Rectangle =
System.Windows.Forms.Screen.PrimaryScreen.Bounds()
Me.Location = New Point((screen.Width - Me.Width) / 2,
(screen.Height - Me.Height) / 2)
prgBar.Maximum = 10

Dim thread As Thread = New Thread(New ThreadStart(AddressOf Start))
thread.Start()

End Sub

Private Sub Start()

prgBar.Value =1
strLabel = "First step..."
Me.Invoke(New EventHandler(AddressOf SafeUpdate))
doSomething



prgBar.Value = 5
strLabel = "Second step..."
Me.Invoke(New EventHandler(AddressOf SafeUpdate))
doSomethingElse


prgBar.Value = 10
strLabel = "Third step..."
Me.Invoke(New EventHandler(AddressOf SafeUpdate))
doFinish

End Sub

'Update Label

Sub SafeUpdate(ByVal o As Object, ByVal e As EventArgs)
Label1.Text = strLabel
End Sub
 
freeze:



....
public strLabel as String

Private Sub FrmLoad_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load


Dim screen As Rectangle =
System.Windows.Forms.Screen.PrimaryScreen.Bounds()
Me.Location = New Point((screen.Width - Me.Width) / 2,
(screen.Height - Me.Height) / 2)
prgBar.Maximum = 10

Dim thread As Thread = New Thread(New ThreadStart(AddressOf Start))
thread.Start()

End Sub

Private Sub Start()

prgBar.Value =1
strLabel = "First step..."
Me.Invoke(New EventHandler(AddressOf SafeUpdate))
doSomething



prgBar.Value = 5
strLabel = "Second step..."
Me.Invoke(New EventHandler(AddressOf SafeUpdate))
doSomethingElse


prgBar.Value = 10
strLabel = "Third step..."
Me.Invoke(New EventHandler(AddressOf SafeUpdate))
doFinish

End Sub

'Update Label

Sub SafeUpdate(ByVal o As Object, ByVal e As EventArgs)
Label1.Text = strLabel
End Sub
 
At a glance, progressbar cannot be touched in thread method either.

You could use the BackgroundWorker from SDF (from opennetcf) for this
task...

Cheers
Daniel
 
You're hitting the progressbar.value in the thread - that's killing you.

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate
 
Back
Top