473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple thread implementation not working.

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.

Oct 27 '06 #1
3 1441
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?
ct*******@gmail.com wrote:
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.
Oct 27 '06 #2
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

ct*******@gmail.com wrote:
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.
Oct 27 '06 #3
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:
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?
Nov 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
20
by: Sacha | last post by:
Consider I have a working thread like this, while (1) { if ( g_pObject ) g_pObject->DoOneStep(); } with an class hierarchie like this
3
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
9
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
44
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.