473,785 Members | 2,794 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 #1
11 1212
"Bernie Yaeger" <be*****@cherwe llinc.com> schrieb
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.
I recommend not to call performclick because the button has not been
clicked. If it has been clicked, the procedure is called automatically.
Any idea what I'm doing wrong?


We must not access controls from a thread that did not create the control.
We have to call the Button's Invoke (or BeginInvoke) method. See also:

http://msdn.microsoft.com/library/en...rmscontrol.asp
http://msdn.microsoft.com/library/en...romThreads.asp
http://msdn.microsoft.com/library/en...msControls.asp

--
Armin

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

Nov 20 '05 #2
Cor
Hi Bernie,

This is a nice one.

I think that it is because that your thread is running in a non showed new
Form1 instance.
(As far as I know a thread cannot be a form)

It is another click event than you do expect.

I think want the click event from the showed form and not from the not
showed form.

If you make this changes it does maybe what you did expect.

I am not that sure, but it has to be something like this.

Cor
Dim bgthread As Thread = New Thread(AddressO f threadroutine)
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?

Nov 20 '05 #3
Cor

"> I think want the click event from the showed form and not from the not
showed form.

I think you want ............... .....
Nov 20 '05 #4
Hi Cor,

Thanks for your advice. I'm just beginning to work with threads, so I need
some guidance.

Thanks again,

Bernie
"Cor" <no*@non.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi Bernie,

This is a nice one.

I think that it is because that your thread is running in a non showed new
Form1 instance.
(As far as I know a thread cannot be a form)

It is another click event than you do expect.

I think want the click event from the showed form and not from the not
showed form.

If you make this changes it does maybe what you did expect.

I am not that sure, but it has to be something like this.

Cor
Dim bgthread As Thread = New Thread(AddressO f threadroutine)
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?


Nov 20 '05 #5
Hi Armin,

Thanks for your advice. I'm just beginning to work with threads, so I need
some guidance.

I will review the msdn articles you point to.

Thanks again,

Bernie
"Armin Zingler" <az*******@free net.de> wrote in message
news:eB******** ******@TK2MSFTN GP09.phx.gbl...
"Bernie Yaeger" <be*****@cherwe llinc.com> schrieb
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.
I recommend not to call performclick because the button has not been
clicked. If it has been clicked, the procedure is called automatically.
Any idea what I'm doing wrong?


We must not access controls from a thread that did not create the control.
We have to call the Button's Invoke (or BeginInvoke) method. See also:

http://msdn.microsoft.com/library/en...rmscontrol.asp http://msdn.microsoft.com/library/en...romThreads.asp http://msdn.microsoft.com/library/en...msControls.asp
--
Armin

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

Nov 20 '05 #6
* "Bernie Yaeger" <be*****@cherwe llinc.com> scripsit:
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?


Instance members of Windows Forms controls are not mutlithreading-safe.
You will have to use the control's 'Invoke' method to call the method.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #7
Hi Herfried,

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?

Tx as always,

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:
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?


Instance members of Windows Forms controls are not mutlithreading-safe.
You will have to use the control's 'Invoke' method to call the method.

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

Nov 20 '05 #8
* "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 #9
"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 #10

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
2705
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
2818
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
9645
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
10329
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
10092
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
9950
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
8974
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
7500
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
6740
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();...
1
4053
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
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.