Solved

Creating a thread inside a thread

Posted on 2004-09-27
6
181 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

16 Experts available now in Live!

Get 1:1 Help Now