473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

thread problem

I have a thread call as follows:
Dim th As New paydue

Dim bgthread As Thread = New Thread(AddressO f th.threadroutin e)

bgthread.Start( )

paydue is the class of the form. threadroutine looks like this:

Public Sub threadroutine()

Dim i As Integer

i = 8

Button1.Perform Click()

i = 9

End Sub

i = 8 and i = 9 execute, but button1.perform click() never executes. Any
idea what I'm doing wrong?

Thanks,

Bernie Yaeger


Nov 20 '05
11 1214
Hi Armin,

Thanks again - I will be testing the button1.invoke method you displayed
after your brief but very helpful explanation of messaging and threads.

Bernie

"Armin Zingler" <az*******@free net.de> wrote in message
news:ek******** ******@TK2MSFTN GP11.phx.gbl...
"Bernie Yaeger" <be*****@cherwe llinc.com> schrieb

Here's the real problem: I am trying to call a method/sub that has
arguments. I begin to understand that a thread is its own world, has
its own heap, doesn't even recognize global variables from another
thread, etc.
A thread is just a sequence of executed statements. Apart from the stack
containig local variables, data is not thread specific.
Is there a way to use invoke to overcome this? If I can
use invoke to launch a performclick - which has arguments - then I
should be able to use it to do what I want. Can you give me a simple
example of using invoke?


You probably know that windows applications are message driven. The OS

sends the messages, like mouse movements and pressed keys, to a window by putting the messages in a message queue. Any thread that created a window is a UI
thread and has a message queue. A thread gets the messages only for those
windows that have been created in the same thread. The thread must process
the messages and send them to the window they belong to. This message loop
is contained in the method Application.Run , also in Form.ShowDialog . Usually an application has only one UI thread. Application.Run is usually called in Sub Main.

Now, a basic rule is that only the thread creating a window can access it.
The Invoke method (or BeginInvoke) can be compared to sending a message to
the window in the other thread. Using the Framework you can do a little bit more: You can specify a procedure that is to be called in the other thread. You can also pass arguments to the procedure.

If there's a reference to the button available in your second thread, you
can call the button's Invoke method and specify the method to be called:

Button1.Invoke( New MethodInvoker(A ddressOf Button1.Perform Click))

--
Armin

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

Nov 20 '05 #11
Thanks Herfried,

Both you and Armin have given me a good start on threads.

Bernie

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:br******** *****@ID-208219.news.uni-berlin.de...
* "Bernie Yaeger" <be*****@cherwe llinc.com> scripsit:
Here's the real problem: I am trying to call a method/sub that has
arguments. I begin to understand that a thread is its own world, has its own heap, doesn't even recognize global variables from another thread, etc. Is there a way to use invoke to overcome this? If I can use invoke to
launch a performclick - which has arguments - then I should be able to use it to do what I want. Can you give me a simple example of using invoke?


Quick and Dirty:

\\\
Imports System.Threadin g

.
.
.

Private m_str As String

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArg s _
) Handles MyBase.Load
Dim t As Thread = New Thread(AddressO f Me.AddThread)
t.Start()
End Sub

Private Sub AddThread()
If Me.ListBox1.Inv okeRequired Then
Dim i As Integer
SyncLock ListBox1.GetTyp e()
For i = 1 To 1000
m_str = "Item " & i.ToString()
Me.ListBox1.Inv oke(CType(Addre ssOf Me.Add,
MethodInvoker))
Next i
End SyncLock
End If
End Sub

Private Sub Add()
Me.ListBox1.Ite ms.Add(m_str)
End Sub
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #12

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

Similar topics

6
2328
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something more sophisticated), and what situations might perturb it. Even a hint as to what would be considered normal scheduling might help. The root of our problem is that we observed a normally well-behaved web application suddenly limit itself to a...
7
2706
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates that. Problem is that when program is started many threads are created (see output section), when only two should be running at any time. Can you please help me to identify me where the problem is? Best regards
6
23745
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100); } The problem is that I don't know how to terminate the thread when my class
7
5901
by: Sin Jeong-hun | last post by:
Hi. I'm writing a Client/Multi-threaded Server program on Windows Vista. It worked fine on Windows Vista, but when the server ran on Windows XP, I/O operation has been aborted because of either a thread exit or an application request exception randomly occurred at the OnReceive method (asynchronous tcp stream reading). I searched all over the internet and found a post posted few years ago. He had the same problem as me, and he said it
34
2819
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
0
9673
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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...
0
10221
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6785
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
5440
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...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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.