MULTITHREADING: Transform simple sample program using threads
- From: pamela fluente <pamelafluente@xxxxxxxxx>
- Date: Thu, 14 Aug 2008 07:33:14 -0700 (PDT)
Hi. I have a very simple program that I want to to transform in a
multithreading program.
This is an extreme exemplification of a program of mine I made in
order to understand multithreading.
The code below is such a sample. You can just cut and paste it into
any form.
The program changes the text of the form on any keypress.
What I am looking for is a practical explanation and possibly an
actual example on how
to transform this program into a multithread program.
What I whish is to understand the general scheme on how to open the
independent processors
(here for instance I have considered 1000 processors) each on 1 thread
and being able to
interact with the initial form.
A code translation into multithreading would be really great! I know
how to start a thread and so on. What I am missing is how to put all
the pieces toghether.
Here is my sample program (it does not make much sense but is just to
understand how to implement the multithreading).
Thanks you very much for any help.
-P
'----------------------------- sample code
------------------------------------------------------------
Public Class Form1
Private SomeProcessors As New Dictionary(Of Integer,
SomeProcessor)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles _
MyBase.Load
For i As Integer = 0 To 1000
Dim s As New SomeProcessor
With s
.InitializeProcessor(i)
End With
Me.SomeProcessors.Add(i, s)
Next i
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) _
Handles Me.KeyDown
Dim Code As Integer = e.KeyCode()
If Me.SomeProcessors.ContainsKey(Code) Then
Me.Text = "Message from process " & Code & ": " &
Me.SomeProcessors(Code).MsgFromProcessor
Else
Me.Text = "Process " & Code & " does not exist"
End If
End Sub
End Class
Class SomeProcessor
Private WithEvents Timer As Timer
Private Index As Integer
Public MsgFromProcessor As String
Sub InitializeProcessor(ByVal Index As Integer)
Me.Timer = New Timer
With Me.Timer
Me.Index = Index
.Interval = 10
.Start()
End With
End Sub
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Timer.Tick
Me.MsgFromProcessor = "Hello from Processor " & Me.Index & "
at " & Now.ToLongTimeString
End Sub
End Class
'----------------------------------------------------------------------------------------------------------------------
.
- Prev by Date: Re: AutoComplete ComboBox Search on Substring for KeyPress
- Next by Date: Re: How would you translate this?
- Previous by thread: How would you translate this?
- Next by thread: What thread does Timer operate on
- Index(es):
Relevant Pages
|