MULTITHREADING: Transform simple sample program using threads

Tech-Archive recommends: Speed Up your PC by fixing your registry




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

'----------------------------------------------------------------------------------------------------------------------

.



Relevant Pages

  • Re: Can I run two macros in the same time?
    ... Sub ABC() ... Dim i As Integer ... Private Sub UserForm_Activate ... Windows in its nature is multithreading, so if you are able to split ...
    (microsoft.public.excel.programming)
  • Multithreading and ASP.net
    ... i am trying to write a multithreading asp.net application ... Private Sub Page_Load(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.general)
  • RE: jpgs not showing on forms
    ... Rather than embed the pictures in the database store the paths to the JPEG ... Private Sub cmdAddImage_Click ... Dim strAdditionalTypes As String, strFileList As String ... Private Sub cmdDeleteImage_Click ...
    (microsoft.public.access.gettingstarted)
  • Re: Newbie problem: Long list of user choices
    ... Private Sub Form_DblClick ... Private Sub VScroll1_Change ... Dim cnt As Long ... With Picture1 ...
    (comp.lang.basic.visual.misc)
  • Re: webBrowser control
    ... Dim DoNotExitWeArePrinting As Boolean ... Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ... Private Sub ScreenToAbsolute ... ' When it is simulating this click, this window MUST be the only window ...
    (microsoft.public.vb.general.discussion)