Home All Groups Group Topic Archive Search About

Simple thread implementation not working.

Author
27 Oct 2006 5:06 PM
ctbfalcon@gmail.com
So I have a progress bar that I would like to be diplayed as the
program is working on adding a network printer.  I want to do this
because while the program is "thinking" the user is not sure if it is
locked up or actually doing somthing.  So I wanted to start another
thread (form3) with the progress bar on top of the main form (form1).

So I have this. This is the button the kicks off the add printer
function.  which also starts the second thread.

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        'button is to add/create the selected printer to windows.

        Dim t As Thread
        t = New Thread(AddressOf Progressbar.Show)
        t.Start()

        Button2.Enabled = False
        Dim WshNetwork As Object = CreateObject("WScript.Network")
        Dim PrinterQueueNameSelection As String =
GetcboComboBoxSelectedItem() 'GetComboBox1SelectedItem()
        Dim pp As String = GetPrinterPath()
        If Not GetifPrinterExsists(pp) Then
            If PrinterQueueNameSelection <> "" Then
                Try
                    WshNetwork.AddWindowsPrinterConnection(pp)
                    MsgBox("Successfully installed printer: " & pp)
                Catch Exc As Exception
                    MsgBox(GetErrorText(3))
                End Try
            Else
                MsgBox(GetErrorText(4))
            End If
        Else
            MsgBox(GetErrorText(9))
        End If
        t.Abort()
        Button2.Enabled = True
    End Sub

I dont care how long the progress bar is run so I abort it once the add
printer function is done successfully or not.

If I run the Progressbar.show in the same thread by itself it works
fine, just the way i want it too.
And of course if i run the progressbar.show in the same thread along
with the add printer it does not start until the printer function is
done.  Then continues to display the bar.

If I put the progressbar.show in a separate thread along with the add
printer function is "blinks" up, does not display anything.  This is
were i would expect it to show itself continue to display the bar as
the main thread adds the printer.

I hope i explained this ok, if you need anymore code let me know.
Thanks for any help in advance.

Author
27 Oct 2006 7:29 PM
Theo Verweij
Calling the show method (even via a new thread) doesn't make the
progressbar form running on an other thread.

You have to create the complete form on the other thread.
And when you do this, you cannot simply abort the thread; you have to
close and dispose the form before closing the thread.

Threading is not an easy task, especially when used on or within winforms.

You are telling that it is working like you want to when you are running
it on the main thread. So, why do you want to run it on a seperate one?


ctbfal***@gmail.com wrote:
Show quoteHide quote
> So I have a progress bar that I would like to be diplayed as the
> program is working on adding a network printer.  I want to do this
> because while the program is "thinking" the user is not sure if it is
> locked up or actually doing somthing.  So I wanted to start another
> thread (form3) with the progress bar on top of the main form (form1).
>
> So I have this. This is the button the kicks off the add printer
> function.  which also starts the second thread.
>
>     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button2.Click
>         'button is to add/create the selected printer to windows.
>
>         Dim t As Thread
>         t = New Thread(AddressOf Progressbar.Show)
>         t.Start()
>
>         Button2.Enabled = False
>         Dim WshNetwork As Object = CreateObject("WScript.Network")
>         Dim PrinterQueueNameSelection As String =
> GetcboComboBoxSelectedItem() 'GetComboBox1SelectedItem()
>         Dim pp As String = GetPrinterPath()
>         If Not GetifPrinterExsists(pp) Then
>             If PrinterQueueNameSelection <> "" Then
>                 Try
>                     WshNetwork.AddWindowsPrinterConnection(pp)
>                     MsgBox("Successfully installed printer: " & pp)
>                 Catch Exc As Exception
>                     MsgBox(GetErrorText(3))
>                 End Try
>             Else
>                 MsgBox(GetErrorText(4))
>             End If
>         Else
>             MsgBox(GetErrorText(9))
>         End If
>         t.Abort()
>         Button2.Enabled = True
>     End Sub
>
> I dont care how long the progress bar is run so I abort it once the add
> printer function is done successfully or not.
>
> If I run the Progressbar.show in the same thread by itself it works
> fine, just the way i want it too.
> And of course if i run the progressbar.show in the same thread along
> with the add printer it does not start until the printer function is
> done.  Then continues to display the bar.
>
> If I put the progressbar.show in a separate thread along with the add
> printer function is "blinks" up, does not display anything.  This is
> were i would expect it to show itself continue to display the bar as
> the main thread adds the printer.
>
> I hope i explained this ok, if you need anymore code let me know.
> Thanks for any help in advance.
>
Author
2 Nov 2006 4:36 PM
ctbfalcon@gmail.com
I simple wanted to point out that the creation/function of the second
form works on its own not in conjuction with the threading code.  So I
did not have to post the code for the form here.  everyone could assue
that code was correct.

Theo Verweij wrote:

Show quoteHide quote
> You are telling that it is working like you want to when you are running
> it on the main thread. So, why do you want to run it on a seperate one?
Author
27 Oct 2006 9:21 PM
FishingScout
You might consider doing the printer work in a thread and allowing the
GUI work to be done in the main thread.

Call the progress show then spawn a thread to add a printer.  When the
thread is done working it can invoke a method to hide the progress bar.

Steve

ctbfal***@gmail.com wrote:
Show quoteHide quote
> So I have a progress bar that I would like to be diplayed as the
> program is working on adding a network printer.  I want to do this
> because while the program is "thinking" the user is not sure if it is
> locked up or actually doing somthing.  So I wanted to start another
> thread (form3) with the progress bar on top of the main form (form1).
>
> So I have this. This is the button the kicks off the add printer
> function.  which also starts the second thread.
>
>     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button2.Click
>         'button is to add/create the selected printer to windows.
>
>         Dim t As Thread
>         t = New Thread(AddressOf Progressbar.Show)
>         t.Start()
>
>         Button2.Enabled = False
>         Dim WshNetwork As Object = CreateObject("WScript.Network")
>         Dim PrinterQueueNameSelection As String =
> GetcboComboBoxSelectedItem() 'GetComboBox1SelectedItem()
>         Dim pp As String = GetPrinterPath()
>         If Not GetifPrinterExsists(pp) Then
>             If PrinterQueueNameSelection <> "" Then
>                 Try
>                     WshNetwork.AddWindowsPrinterConnection(pp)
>                     MsgBox("Successfully installed printer: " & pp)
>                 Catch Exc As Exception
>                     MsgBox(GetErrorText(3))
>                 End Try
>             Else
>                 MsgBox(GetErrorText(4))
>             End If
>         Else
>             MsgBox(GetErrorText(9))
>         End If
>         t.Abort()
>         Button2.Enabled = True
>     End Sub
>
> I dont care how long the progress bar is run so I abort it once the add
> printer function is done successfully or not.
>
> If I run the Progressbar.show in the same thread by itself it works
> fine, just the way i want it too.
> And of course if i run the progressbar.show in the same thread along
> with the add printer it does not start until the printer function is
> done.  Then continues to display the bar.
>
> If I put the progressbar.show in a separate thread along with the add
> printer function is "blinks" up, does not display anything.  This is
> were i would expect it to show itself continue to display the bar as
> the main thread adds the printer.
>
> I hope i explained this ok, if you need anymore code let me know.
> Thanks for any help in advance.