thread problem

  • Thread starter Thread starter Bernie Yaeger
  • Start date Start date
B

Bernie Yaeger

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

Dim bgthread As Thread = New Thread(AddressOf th.threadroutine)

bgthread.Start()

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

Public Sub threadroutine()

Dim i As Integer

i = 8

Button1.PerformClick()

i = 9

End Sub

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

Thanks,

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

Dim bgthread As Thread = New Thread(AddressOf th.threadroutine)

bgthread.Start()

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

Public Sub threadroutine()

Dim i As Integer

i = 8

Button1.PerformClick()

i = 9

End Sub

i = 8 and i = 9 execute, but button1.performclick() 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/e...evelopingmultithreadedwindowsformscontrol.asp
http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskManipulatingControlsFromThreads.asp
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconFreeThreadingWithFormsControls.asp
 
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
 
Hi Cor,

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

Thanks again,

Bernie
 
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
 
* "Bernie Yaeger said:
I have a thread call as follows:
Dim th As New paydue

Dim bgthread As Thread = New Thread(AddressOf th.threadroutine)

bgthread.Start()

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

Public Sub threadroutine()

Dim i As Integer

i = 8

Button1.PerformClick()

i = 9

End Sub

i = 8 and i = 9 execute, but button1.performclick() 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.
 
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
 
Bernie Yaeger said:
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(AddressOf Button1.PerformClick))
 
* "Bernie Yaeger said:
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.Threading

..
..
..

Private m_str As String

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

Private Sub AddThread()
If Me.ListBox1.InvokeRequired Then
Dim i As Integer
SyncLock ListBox1.GetType()
For i = 1 To 1000
m_str = "Item " & i.ToString()
Me.ListBox1.Invoke(CType(AddressOf Me.Add,
MethodInvoker))
Next i
End SyncLock
End If
End Sub

Private Sub Add()
Me.ListBox1.Items.Add(m_str)
End Sub
///
 
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
 
Back
Top