threading problem... I think?

  • Thread starter Thread starter éric
  • Start date Start date
É

éric

Scenario:

Main thread [form's trhead] contains a custom panel which downloads async
via http
the download is on a worker thread.

When the worker thread is operating it updates the panel with info on its
status ie how much is downloaded

Problem:

While the worker thread is running and the user interacts with the panel by
clicking a button in it or changing it's size, the whole application freezes
no error codes

Is there something wrong with how I am proceeding?

Regards,

Éric
 
Yes. You cannot update the UI from a worker thread. You must use Control
Invoke to tell the main thread to do the UI update.
 
Hi,

Here is a snip from some code that I use to process an event from the
receive thread in my CFSerialIO class. The idea would be the same for your
progress update.

Private Sub ReceiveEvent() Handles SerialPort.OnComm



ScreenBuff.Append(SerialPort.InputString)

Me.Invoke(New EventHandler(AddressOf DisplayData))

End If

End Sub

Private Sub DisplayData(ByVal sender As Object, ByVal e As EventArgs)

'This marshals receive data from the receive thread context (OnComm or
CommError) to the Windows Form STAThread context

With txtTerm

..Text = ScreenBuff.ToString

..SelectionStart = .Text.Length

If (ErrorMessage.Length > 0) Then

..SelectedText = ErrorMessage

ErrorMessage = ""

End If

End With

With ScreenBuff

If .Length > 0.8 * .Capacity Then

ScreenBuff.Remove(0, CInt(0.6 * .Capacity))

End If

End With

End Sub

Instead of displaying data in the DisplayData routine, you could Invoke a
DisplayProgress routine. When you call Me.Invoke (or Control.Invoke, which
amounts to the same thing - this is in the STAThread context of the form),
you transfer UI update to the STAThread in the form, away from the free
thread that generated the event. You do have to pass the actual data
indirectly via a private (or equivalently scoped) variable -- this is a
limitation of the Compact Framework.

Dick
--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Safe, Simple Multithreading in Windows Forms, Part 1
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms01232003.asp

Check out part 2 and 3 also!

HTH,

--
Scott
http://www.OdeToCode.com


Ah ha!

Thanks... any articles to get me started?

Regards,

éric

éric said:
Scenario:

Main thread [form's trhead] contains a custom panel which downloads async
via http
the download is on a worker thread.

When the worker thread is operating it updates the panel with info on its
status ie how much is downloaded

Problem:

While the worker thread is running and the user interacts with the panel by
clicking a button in it or changing it's size, the whole application freezes
no error codes

Is there something wrong with how I am proceeding?

Regards,

Éric
 
I ended up using the sample posted on OpenNETCF but I needed your example,
Dick, to understand it.

Thanks again

Regards,

éric
 
Back
Top