?
Solved

Creating a thread inside a thread

Posted on 2004-09-27
6
Medium Priority
?
192 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
3 Comments
 
LVL 6

Accepted Solution

by:
KGreg earned 100 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 100 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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 …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

850 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