By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,583 Members | 3,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,583 IT Pros & Developers. It's quick & easy.

How to make calls across threads?

P: n/a
The following code below is single threaded. When I click the start button,
it hogs the message pump so none of the UI works. How can I convert this to
make it multi-threaded so I can continue to be user friendly and service my
message pump while the timer loop does something time consuming? I will have
problems if I execute the "lblTimeDisplay.Text = newval" in a thread that is
different from the one that created it, won't I?


Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.VisualBasic.DateAndTime
Public Class Form1
Inherits Form
Public WithEvents btnStart As Button
Private WithEvents mText as TimerState = New TimerState
Public lblTimerDisplay as Label
Public Sub New()
btnStart = New Button()
btnStart.Size = New Size(80, 20)
btnStart.Location = New Point(10, 10)
btnStart.Text = "start timer"

lblTimerDisplay = new Label()
lblTimerDisplay.Location = New Point(10,40)
lblTimerDisplay.Text = "Timer Display"
lblTimerDisplay.Size = New Size(80,30)
End Sub 'New
Private Sub btnStart_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles btnStart.Click
End Sub
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
Private Sub mText_UpdateTime(ByVal Jump as Double) Handles
lblTimerDisplay.Text = Microsoft.VisualBasic.Format(Jump, "##0.00")
End Sub
End Class
Class TimerState
Public Event UpdateTime(ByVal Jump As Double)
Public Sub TimerTask(ByVal Duration As Double)
Dim Start as Double
Dim Second as Double
Dim SoFar as Double
Start = Microsoft.VisualBasic.DateAndTime.Timer
SoFar = Start
Do While Microsoft.VisualBasic.DateAndTime.Timer < Start + Duration
If Microsoft.VisualBasic.DateAndTime.Timer - SoFar >= 0.1 Then
SoFar = SoFar + 0.1
RaiseEvent UpdateTime(Microsoft.VisualBasic.DateAndTime.Timer-
End If
End Sub
End Class
Oct 22 '07 #1
Share this Question
Share on Google+
1 Reply

P: n/a
"Siegfried Heintze" <si*******@heintze.comwrote in
I will have
problems if I execute the "lblTimeDisplay.Text = newval" in a thread
that is different from the one that created it, won't I?
You need to marshal the call back to the UI thread:
Oct 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.