473,699 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
'button is to add/create the selected printer to windows.

Dim t As Thread
t = New Thread(AddressO f Progressbar.Sho w)
t.Start()

Button2.Enabled = False
Dim WshNetwork As Object = CreateObject("W Script.Network" )
Dim PrinterQueueNam eSelection As String =
GetcboComboBoxS electedItem() 'GetComboBox1Se lectedItem()
Dim pp As String = GetPrinterPath( )
If Not GetifPrinterExs ists(pp) Then
If PrinterQueueNam eSelection <"" Then
Try
WshNetwork.AddW indowsPrinterCo nnection(pp)
MsgBox("Success fully installed printer: " & pp)
Catch Exc As Exception
MsgBox(GetError Text(3))
End Try
Else
MsgBox(GetError Text(4))
End If
Else
MsgBox(GetError Text(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.sho w in the same thread by itself it works
fine, just the way i want it too.
And of course if i run the progressbar.sho w 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.sho w 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 1457
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
'button is to add/create the selected printer to windows.

Dim t As Thread
t = New Thread(AddressO f Progressbar.Sho w)
t.Start()

Button2.Enabled = False
Dim WshNetwork As Object = CreateObject("W Script.Network" )
Dim PrinterQueueNam eSelection As String =
GetcboComboBoxS electedItem() 'GetComboBox1Se lectedItem()
Dim pp As String = GetPrinterPath( )
If Not GetifPrinterExs ists(pp) Then
If PrinterQueueNam eSelection <"" Then
Try
WshNetwork.AddW indowsPrinterCo nnection(pp)
MsgBox("Success fully installed printer: " & pp)
Catch Exc As Exception
MsgBox(GetError Text(3))
End Try
Else
MsgBox(GetError Text(4))
End If
Else
MsgBox(GetError Text(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.sho w in the same thread by itself it works
fine, just the way i want it too.
And of course if i run the progressbar.sho w 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.sho w 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
'button is to add/create the selected printer to windows.

Dim t As Thread
t = New Thread(AddressO f Progressbar.Sho w)
t.Start()

Button2.Enabled = False
Dim WshNetwork As Object = CreateObject("W Script.Network" )
Dim PrinterQueueNam eSelection As String =
GetcboComboBoxS electedItem() 'GetComboBox1Se lectedItem()
Dim pp As String = GetPrinterPath( )
If Not GetifPrinterExs ists(pp) Then
If PrinterQueueNam eSelection <"" Then
Try
WshNetwork.AddW indowsPrinterCo nnection(pp)
MsgBox("Success fully installed printer: " & pp)
Catch Exc As Exception
MsgBox(GetError Text(3))
End Try
Else
MsgBox(GetError Text(4))
End If
Else
MsgBox(GetError Text(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.sho w in the same thread by itself it works
fine, just the way i want it too.
And of course if i run the progressbar.sho w 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.sho w 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
3523
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 = serial.Serial()
20
3483
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
2382
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 things. The method I am writing queries a large dataset. As part of my feedback to the user, I am updating the status bar when the connection is made and the dataset is actually retrieved. The dataset retrieval method I have placed on a separate...
73
4603
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 endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix code leading to an endless loop can look like and if it is eventually not possible to write such...
6
5121
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 performed on the same thread thread that instantiated the class. One way to do this is to pass an ISynchronizeInvoke into the class and use it to synchronize the callback. In the constructor of the class, could I take note of the current thread...
3
5571
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 the mutex in order to activate the thread once the data is generated. I have to do it this way, i can only call the thread if the data are generated. ******************************************************** step 1: initialize the mutex
176
8358
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 their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
9
2979
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
7796
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 possible this function evolves from thread-unsafe to thread-safe in recent years? How could i find out? I am using the C library coming with GNU linux distribution. thanks a lot.
0
9172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8908
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.