473,586 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple thead question .... what am I doing wrong?

Hi,

I have a little test project. I spin up a thread, lets it sleep for a
second and then the thread makes a button visible ... or at least should
make it visible, which it doesn't do.

---------------------------------------------------------------------------

Here's my thread class:

Imports System.Threadin g

Public Class MyThreadClass

Private _thread As Thread
Public Delegate Sub ThreadDone(ByVa l IsOnline As Boolean)
Public OnThreadDone As ThreadDone
Public Sub Run()
_thread = New Thread(AddressO f Start)
_thread.IsBackg round = True
_thread.Start()
End Sub
Public Sub SpinDown()
If _thread Is Nothing Then Exit Sub
_thread.Abort()
_thread = Nothing
End Sub
Public Sub Abort()
If _thread Is Nothing Then Exit Sub
_thread.Abort()
_thread = Nothing
End Sub
Private Sub Start()
Try
_thread.Sleep(1 000)
OnThreadDone.In voke(True)
Catch
OnThreadDone.In voke(False)
Finally
End Try
End Sub

End Class

---------------------------------------------------------------------------

And here's my form code:

Private _thread As MyThreadClass

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As _
System.EventArg s) Handles MyBase.Load
_thread = New MyThreadClass
_thread.OnThrea dDone = AddressOf OnThreadDone
_thread.Run()
End Sub
Public Sub OnThreadDone(By Val status As Boolean)
Me.Button1.Visi ble = True
' The button is still hidden :o(((
End Sub

---------------------------------------------------------------------------

What am I doing wrong??

Why doesn't the button show up?

Thanks in advance!!

M O J O

Nov 20 '05 #1
3 1133
Hi,

When you call a delegate synchronously, the Invoke method calls the target
method directly on the current thread.
For more details, take a look at the link below.
Asynchronous Delegates
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpovrasynchrono usdelegates.asp

You may the code below,
Private _thread As MyThreadClass
Public Sub OnThreadDone(By Val status As Boolean)
Me.Button1.Visi ble = True
' The button will be visible now :)))
End Sub
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
_thread = New MyThreadClass
_thread.fm = Me
_thread.OnThrea dDone = AddressOf OnThreadDone
_thread.Run()
End Sub

Public Class MyThreadClass
Public fm As Form1
''''Pass in the Form1's reference
Private _thread As Thread
Public Delegate Sub ThreadDone(ByVa l IsOnline As Boolean)
Public OnThreadDone As ThreadDone
Public Sub Run()
_thread = New Thread(AddressO f Start)
_thread.IsBackg round = True
_thread.Start()
End Sub
Public Sub SpinDown()
If _thread Is Nothing Then Exit Sub
_thread.Abort()
_thread = Nothing
End Sub
Public Sub Abort()
If _thread Is Nothing Then Exit Sub
_thread.Abort()
_thread = Nothing
End Sub
Private Sub Start()
Try
_thread.Sleep(1 000)
'Call the form1's invoke, so that the delegate method OnThreadDone will
call on
'Form1's thread
fm.Invoke(OnThr eadDone, New Object() {True})
Catch
fm.Invoke(OnThr eadDone, New Object() {False})
Finally
End Try
End Sub
End Class

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #2
"M O J O" <mojo@_no_spam_ delete_this_new websolutions.dk > schrieb
Hi,

I have a little test project. I spin up a thread, lets it sleep for a
second and then the thread makes a button visible ... or at least
should
make it visible, which it doesn't do.

-------------------------------------------------------------------------- -
Here's my thread class:

Imports System.Threadin g

Public Class MyThreadClass

Private _thread As Thread
Public Delegate Sub ThreadDone(ByVa l IsOnline As Boolean)
Public OnThreadDone As ThreadDone
Public Sub Run()
_thread = New Thread(AddressO f Start)
_thread.IsBackg round = True
_thread.Start()
End Sub
Public Sub SpinDown()
If _thread Is Nothing Then Exit Sub
_thread.Abort()
_thread = Nothing
End Sub
Public Sub Abort()
If _thread Is Nothing Then Exit Sub
_thread.Abort()
_thread = Nothing
End Sub
Private Sub Start()
Try
_thread.Sleep(1 000)
OnThreadDone.In voke(True)
Catch
OnThreadDone.In voke(False)
Finally
End Try
End Sub

End Class

-------------------------------------------------------------------------- -
And here's my form code:

Private _thread As MyThreadClass

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
_
System.EventArg s) Handles MyBase.Load
_thread = New MyThreadClass
_thread.OnThrea dDone = AddressOf OnThreadDone
_thread.Run()
End Sub
Public Sub OnThreadDone(By Val status As Boolean)
Me.Button1.Visi ble = True
' The button is still hidden :o(((
End Sub

-------------------------------------------------------------------------- -
What am I doing wrong??

Why doesn't the button show up?

Only the thread creating a window/button can access it. In the Form, in
OnThreadDone, Uue Me.Invoke or Me.Button1.Invo ke to marshal the call to the
thread creating the form/button.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
You've fallen into a classic threading trap--the thread you're using to make
the button visible is not the UI thread.

Here's one way to fix the problem. Add this code to the form.
Private Delegate Sub showButtonDeleg ate()

Public Sub OnThreadDone(By Val status As Boolean)
Dim sb As New showButtonDeleg ate(AddressOf Me.MakeButton1V isible)
If Me.InvokeRequir ed Then
Me.Invoke(sb)
Else
Me.Button1.Visi ble = True
End If
' button is now visible
End Sub

Private Sub MakeButton1Visi ble()
Me.Button1.Visi ble = True
End Sub

Obviously, you could make the code a little more generic than shown here.
The Invoke method is the key to getting UI actions back on the right thread.
You could just as easily check the InvokeRequired property of the button
object itself and use Invoke or not, as required.
"M O J O" <mojo@_no_spam_ delete_this_new websolutions.dk > wrote in message
news:eO******** ******@TK2MSFTN GP11.phx.gbl...
Hi,

I have a little test project. I spin up a thread, lets it sleep for a
second and then the thread makes a button visible ... or at least should
make it visible, which it doesn't do.

-------------------------------------------------------------------------- -
Here's my thread class:

Imports System.Threadin g

Public Class MyThreadClass

Private _thread As Thread
Public Delegate Sub ThreadDone(ByVa l IsOnline As Boolean)
Public OnThreadDone As ThreadDone
Public Sub Run()
_thread = New Thread(AddressO f Start)
_thread.IsBackg round = True
_thread.Start()
End Sub
Public Sub SpinDown()
If _thread Is Nothing Then Exit Sub
_thread.Abort()
_thread = Nothing
End Sub
Public Sub Abort()
If _thread Is Nothing Then Exit Sub
_thread.Abort()
_thread = Nothing
End Sub
Private Sub Start()
Try
_thread.Sleep(1 000)
OnThreadDone.In voke(True)
Catch
OnThreadDone.In voke(False)
Finally
End Try
End Sub

End Class

-------------------------------------------------------------------------- -
And here's my form code:

Private _thread As MyThreadClass

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As _
System.EventArg s) Handles MyBase.Load
_thread = New MyThreadClass
_thread.OnThrea dDone = AddressOf OnThreadDone
_thread.Run()
End Sub
Public Sub OnThreadDone(By Val status As Boolean)
Me.Button1.Visi ble = True
' The button is still hidden :o(((
End Sub

-------------------------------------------------------------------------- -
What am I doing wrong??

Why doesn't the button show up?

Thanks in advance!!

M O J O

Nov 20 '05 #4

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

Similar topics

0
1677
by: Dr. Laurence Leff | last post by:
I am having trouble with tables under docbook. I am using xmlto under Redhat Linux. When I process a simple table as below, I get an error message about the Unit of Measure. If I change the colwidth from "1*" to something specific like "1in" it works. Yet the docbook manual indicates the "1*" stands for one column width. I tried...
1
2394
by: Freddy Morel | last post by:
My objective is to make a two columns table from an XML-data file. I do not want to use XSL, so sorting and else is not important for the moment. I have this working in a table: |-------------------------------| | table heading OK | |-------------------------------| | R1-C1 | R1-C2 | R1-C3 | R1-C4 | | R2-C1 | R2-C2 | R2-C3 |...
11
6040
by: Woolly Mittens | last post by:
I tried validating my gallery page using your validator. http://validator.w3.org/check?uri=http%3A%2F%2Fwww.woollymittens.nl%2Fcontent%2Fgallery%2Findex.asp To my surprise it informed me that <tfoot> wasn't valid XHTML 1.1 strict, while it is in the document definition:...
3
1716
by: suzy | last post by:
Hello, I am trying to write a generic tool that accesses a SQL server and reads/updates/deletes/creates records. I want to reference this tool from my asp.net pages to talk to my db. by the way, i want the results of any read, update and create to be returned in xml. when reading data, i populate a dataset with data and then use the...
51
8251
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
33
2244
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void modify_pointer(char *); int main(int argc, char *argv) {
0
2289
by: redaudi | last post by:
I'm hoping someone might be able to help me with the following issue. Basically I'm creating a htmltable on the fly and then simply populating it with rows and cells accordingly. My test site has a CSS sheet which applies styling etc. My problem is that I can't find a method of adding a THEAD tag into the table. For example I start with...
0
1325
by: makifsar | last post by:
I am using a comserver to communicate remote equipments. This server gives response via WndProc. First I call READ function to start communication and remote equipment gives response this via WndProc. Mw request is, I want to sleep READ thead and after taking right message from WndProc, wake up this thead return this value to main thead. I am...
2
1423
by: Michael7 | last post by:
Hi everyone, I'm new to CSS of course, and have been trying to learn it. However, when I try to pull off something as simple as positioning of text . . . nothing works in my index page. So in frustration I made a test page. Just a but of words to test only the code I'm looking for. Still nothing! I apologize if this question is a...
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6614
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...
1
5710
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...
0
5390
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...
0
3837
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1180
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...

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.