473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Threading and Forms

I have written a very simple threading proggie in an attempt to teach myself
threading. I have seen it referred to in articles that Forms are not thread
safe. My form has two labels which are written to by different threads in
my example. It works fine so I'm wondering if anyone can tell me what I
need to watch out for with respect to Forms and threading? For what it's
worth, here is my short proggie:

Private Sub SomeTask()
' This thread simply counts upwards, displaying the count in the
label.
Dim i As Integer
For i = 1 To 100000
Me.Label2.Text = CInt(i / 100)
Application.DoEvents()
Next i
End Sub

Private Sub SomeOtherTask()
' This thread simply counts downwards, displaying the count in the
second label.
Dim i As Integer
For i = 100000 To 1 Step -1
Me.Label3.Text = CInt(i / 100)
Application.DoEvents()
Next i
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Thread1 As New System.Threading.Thread(AddressOf SomeTask)
Thread1.Start()
Dim Thread2 As New System.Threading.Thread(AddressOf SomeOtherTask)
Thread2.Start()
End Sub

Nov 20 '05 #1
2 1304
Hi, Tyson

to see conflicts and problems related to thread safety you have to run at
least 2 threads accessing same control. Or while thread is running you have
to touch control from main UI thread.
Documentation says that any instance methods are not guaranteed to be
thread-safe. Which means you might have no issues in some simple cases.

HTH
Alex

"Tyson Ackland" <ty***@ona.gov.au> wrote in message
news:O1**************@TK2MSFTNGP10.phx.gbl...
I have written a very simple threading proggie in an attempt to teach myself threading. I have seen it referred to in articles that Forms are not thread safe. My form has two labels which are written to by different threads in
my example. It works fine so I'm wondering if anyone can tell me what I
need to watch out for with respect to Forms and threading? For what it's
worth, here is my short proggie:

Private Sub SomeTask()
' This thread simply counts upwards, displaying the count in the
label.
Dim i As Integer
For i = 1 To 100000
Me.Label2.Text = CInt(i / 100)
Application.DoEvents()
Next i
End Sub

Private Sub SomeOtherTask()
' This thread simply counts downwards, displaying the count in the
second label.
Dim i As Integer
For i = 100000 To 1 Step -1
Me.Label3.Text = CInt(i / 100)
Application.DoEvents()
Next i
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Thread1 As New System.Threading.Thread(AddressOf SomeTask)
Thread1.Start()
Dim Thread2 As New System.Threading.Thread(AddressOf SomeOtherTask) Thread2.Start()
End Sub

Nov 20 '05 #2
Hi Tyson,

I have no answer on your question for you, however a greath simple sample of
multithreading, gives direct a good idea and I never thought on this sample.

(You have to change this but that is only a detail)
Me.Label3.Text = (i / 100).ToString

Cor
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mahesh Devjibhai Dhola [MVP] | last post by:
We are building Chat like application using Forms and as a result our programming is becoming complicated to display messages received on different threads in the chat window (due to STA...
13
by: RCS | last post by:
I have a UI that needs a couple of threads to do some significant processing on a couple of different forms - and while it's at it, update the UI (set textboxes, fill in listviews). I created a...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
7
by: Terry Olsen | last post by:
I run this code: Private Sub p_recv(ByVal sender As Object, ByVal e As SerialReceivedEventArgs) Handles p.ReceivedEvent txtRecv.Text += p.ReadExisting End Sub I get this error: ...
0
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks...
6
by: hzgt9b | last post by:
Using VS 2003, .NET: I developed a windows application that performs several actions based on an input file. The application displays a progress bar as each action executes. Based on new...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
9
by: tshad | last post by:
I have a Windows App that is doing some work and then writing a "Now Processing..." line to the status line of the window as well as the Textbox on the form. But the problem is that the work is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.