I have two forms

the first FrmRecipe.vb form has all my calulations and functions in it
the second FrmReports has a crystal report viewer with loads of reports.

I am trying to call the report as a thread so that the main from is still working however when the treads are complete
my application closes

I am calling Me.BeginInvoke(New MethodInvoker(AddressOf FrmReports.PrintMasterFormulationToprinter))

Can anyone point me in the right direction?

Look for any code in the invoked method that closes objects or exits. Why? Begininvoke works like this:

"Executes the specified delegate asynchronously with the specified arguments, on the thread that the control's underlying handle was created on."

Since you invoked and are in the same thread and handle, if you close an object or exit, your app exits.

I found the only way to truly leverage a multi-core machine was to avoid invoke and just launch a new app with arguments of what it needed to do. ie. run an app. That way I was assured it was on its own thread.

PS. My code that does this is in C# but same rules apply to vb.net.

twoForms.PNG twoForms2.PNG I would do the following:
A) Create a console application
B) Add two forms: form1 and form2
1) form1 has one button to open and show form2 and another button to copy a text from a textbox to a textbox in form2
2) each button will raise an event to be catched by the console application
3) form2 has a textbox to paste the text coming from form1
C) Add references to System.Windows.Forms and InteropServices

Then the code for the console application:

Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Module Module1
    WithEvents f1 As New Form1
    Dim f2 As Form2
    Sub Main()
        Const SW_HIDE As Int32 = 0
        'Const SW_SHOW As Int32 = 5
        Try
            Dim hwnd As IntPtr
            hwnd = GetConsoleWindow()
            ShowWindow(hwnd, SW_HIDE)
            f1.Show()
            While Not f1.IsDisposed OrElse _
                 (f2 IsNot Nothing AndAlso Not f2.IsDisposed)
                System.Windows.Forms.Application.DoEvents()
            End While
        Catch ex As Exception
        End Try
    End Sub
    Private Sub f1_showForm2() Handles f1.showForm2
        showF2()
    End Sub
    Private Sub f1_textToForm2(text As String) Handles f1.textToForm2
        showF2()
        f2.tbTextFromForm1.Text = text
    End Sub
    Sub showF2()
        If f2 Is Nothing Then
            f2 = New Form2
            f2.Show()
        End If
    End Sub
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Sub ShowWindow(hWnd As IntPtr, nCmdShow As Int32)
    End Sub
    <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Function GetConsoleWindow() As IntPtr
    End Function
End Module

the code for Form1:

Public Class Form1
    Event showForm2()
    Event textToForm2(text As String)
    Shared WithEvents f1 As New Form1
    Shared f2 As Form2
    Private Sub btnShowF2_Click(sender As System.Object, e As System.EventArgs) Handles btnShowF2.Click
        RaiseEvent showForm2()
    End Sub
    Private Sub btnTransferTxtToForm2_Click(sender As System.Object, e As System.EventArgs) Handles btnTransferTxtToForm2.Click
        RaiseEvent textToForm2(tbTextToForm2.Text)
    End Sub
    Shared Sub f1_showForm2() Handles f1.showForm2
        showF2()
    End Sub
    Shared Sub f1_textToForm2(text As String) Handles f1.textToForm2
        showF2()
        f2.tbTextFromForm1.Text = text
    End Sub
    Shared Sub showF2()
        If f2 Is Nothing Then
            f2 = New Form2
            f2.Show()
        End If
    End Sub
End Class

Ok, my fault, code for form1 is just:

Public Class Form1

    Event showForm2()
    Event textToForm2(text As String)
    Shared WithEvents f1 As New Form1
    Private Sub btnShowF2_Click(sender As System.Object, e As System.EventArgs) Handles btnShowF2.Click
        RaiseEvent showForm2()
    End Sub
    Private Sub btnTransferTxtToForm2_Click(sender As System.Object, e As System.EventArgs) Handles btnTransferTxtToForm2.Click
        RaiseEvent textToForm2(tbTextToForm2.Text)
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.