Solved

Creating a thread inside a thread

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now