Solved

Creating a thread inside a thread

Posted on 2004-09-27
6
183 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

785 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