Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Creating a thread inside a thread

Posted on 2004-09-27
6
184 Views
Last Modified: 2012-05-05
Hi, I am using multithreading and I have to create some threads inside a thread. Is that possible ? I have tried but I'm getting an error. Here is a part of my code.

Public class ProcessNewFile
[Code]

    Public Sub Go
    [Code]
    Dim I, Answer(2) as integer
    Dim objSendIt(2) as integer
    For I = 0 to 2
         Answer(I) = New Threading.Thread(AddressOf Me.SendIt)
    Next
    End sub

    Public Function SendIt() as integer
    [Code]
    End Function

End Class

I am then calling a new thread starting the sub Go from my main process. But for the line when I create the thread inside the thread, its giving me the error: Method 'Public Function SendIt() As Integer' does not have the same signature as delegate 'Delegate Sub ThreadStart()'.

Any idea ???
Thanks,
Eric
0
Comment
Question by:e_poirier
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
KGreg earned 25 total points
ID: 12163433
You can't start a thread with that function.. the signatures do not match

'Delegate Sub ThreadStart()'  not matching 'Public Function SendIt() As Integer'

means that your function needs to be declared like this:

Public Sub SendIt()
     '''''Code
End Sub

Hope this helps,

KGreg
0
 
LVL 3

Assisted Solution

by:jazduck
jazduck earned 25 total points
ID: 12166463
I think your approaching the problem wrong.  Have a look at the following code, the first thread you create uses a MethodInvoker to pass control back to your main thread, which can then start a second (or more) thread.

Imports System.Threading
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(160, 78)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim t1 As New Thread(AddressOf StartThread1)
        t1.Start()
    End Sub

    Sub StartThread1()
        Dim mi As New MethodInvoker(AddressOf Me.StartThread2)
        BeginInvoke(mi)
        Do
            Debug.WriteLine("Running Thread 1")
            Thread.CurrentThread.Sleep(500)
        Loop
    End Sub

    Sub StartThread2()
        Dim t2 As New Thread(AddressOf Thread2Work)
        t2.Start()
    End Sub

    Sub Thread2Work()
        Do
            Debug.WriteLine("Running Thread 2")
            Thread.CurrentThread.Sleep(500)
        Loop
    End Sub
End Class

Hope it Helps,
Jaz
0
 
LVL 3

Expert Comment

by:jazduck
ID: 12365942
Both answers are correct LearnedOne, mine just goes into more detail and shows a different way of achieving the same thing.

For 50 points, to split or not to split barely makes a difference. :)

Jaz
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question