Opening a Thread

PrivateSub

New member
Joined
Jun 25, 2007
Messages
2
Programming Experience
3-5
Hi guys,

I am a newbie here, so please be gentle! Hahahahaahahahahah
I am working a vb.net project and i am facing this tiny little BIG problem!

I have a form that needs to open a word control inside it. But it takes too long to load the control....So i've come up with this idea...When loading the whole project, i'm opening a new thread so the word control loads in it. So when i open the other form, the word control loads quicker than earlier. But i have this tiny little BIG problem as i mentioned...

In the frmMain_Closing event (closing event of the whole project) i try to dispose or close the winword.exe process (from task manager) but it doesn't work.
Is it maybe because this process belongs to another thread than the one that opened the whole project?
Take in consideration that i use the thread.abort command....

Any help??
 
I struggled with form safe threading for a long time, but once I found a good example, and got use to it, it seems simple now. Here's a quick example demonstrating a threaded ping that updates a listbox on the main form :

VB.NET:
Imports System.Threading
Imports System.Net
Imports System.Net.sockets

Public Class Form1
    Dim btn2 As Integer
    Dim x As Integer = 0

    'Create a delegate to handle the message you're going to update to your form
    Delegate Sub msgDelegate(ByVal msg1 As String)
    Dim tsafeDelegate As msgDelegate

    'This is the subroutine that will handle updating the form
    Sub showmessage(ByVal msg1 As String)
        Me.ListBox1.Items.Add(msg1)
        Me.Refresh()
        Me.ListBox1.SelectedItem = msg1
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Create and start the thread
        Dim t As New Thread(AddressOf DoWork)
        t.Start("delstar")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        btn2 = 1
    End Sub

    Sub DoWork(ByVal arg As Object)
        Dim iptest As NetworkInformation.PingReply
        Dim pinger As New NetworkInformation.Ping

        'Initialize the delegate
        tsafeDelegate = New msgDelegate(AddressOf showmessage)
        While btn2 = 0
            iptest = pinger.Send(arg.ToString)
            'Invoke the delegate, passing the message you want in the second field
            Me.Invoke(tsafeDelegate, iptest.Status.ToString)
        End While
        btn2 = 0
    End Sub
End Class

Hope this helps
 
Back
Top