Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to code threading

0 views
Skip to first unread message

Tetsuya Oguma

unread,
Aug 29, 2002, 6:33:04 PM8/29/02
to
Hi all,

I am trying to create a Form which fade in and out as time
goes by with Threading. My code is very simple.

I have a Form called Test.vb (and Test.vb[Design] for the
actual Form GUI) and Module.vb on my Project.

It builds OK and when running it I get an error message
saying "An unhandled exception of
type 'System.NullReferenceException' occurred in
Project4.exe. Additional information: Error binding to
target method." with "myTread" highlighted in yellow in
Dim statement. And the Form doesn't get up.

How can I fix this?

Test.vb contains;
---
Public Class TestForm
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
'here I omit the code
#End Region

Dim myThread As New Threading.Thread(AddressOf
MyForm.FadeIn)

Private Sub TestForm_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Me.Opacity = 1

cmbOK.Text = "Fade in and out this Form"
End Sub

Private Sub cmbOK_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmbOK.Click
myThread.Start()
End Sub

Private Sub FadeIn()
With MyForm
While (.Opacity < 1)
.Opacity -= 0.02
System.Threading.Thread.Sleep(100)
'myThread.Sleep(175)
.cmbOK.Text = "Thread: " & .Opacity
End While
End With
End Sub
End Class
---

Module1.vb contains;
---
Module Module1
Public MyForm As New TestForm()

Sub Start()
MyForm.Opacity = 1
MyForm.Show()
End Sub
End Module
---

Thanks for your help.
---
Tetsuya Oguma, Sydney, Australia


Armin Zingler

unread,
Aug 29, 2002, 7:32:19 PM8/29/02
to
"Tetsuya Oguma" <z123...@unsw.edu.au> schrieb

> It builds OK and when running it I get an error message
> saying "An unhandled exception of
> type 'System.NullReferenceException' occurred in
> Project4.exe. Additional information: Error binding to
> target method." with "myTread" highlighted in yellow in
> Dim statement. And the Form doesn't get up.
> [...]

> Module1.vb contains;
> ---
> Module Module1
> Public MyForm As New TestForm()
>
> Sub Start()
> MyForm.Opacity = 1
> MyForm.Show()
> End Sub
> End Module

I'm not a specialist for threading but
- You didn't write a Sub Main and set it as the startup object for the
project.
- If your TestForm is the startup object, you're creating two form instances
- Your Start procedure that should be Sub main doesn't contain
Application.Run
- There's a circular problem: If your TestForm is the startup object, an
internal Sub Main is created in your form. The first thing that is done in
this sub main is the initialization of the public variable MyForm in the
module. This means, a form instance is created because it's declared "As New
TestForm". One action in the constructor of the form is creating the thread
because it's declared As New. Now, when the thread is created, MyForm.FadeIn
is accessd. But: MyForm is just beeing created, i.e. it hasn't been created
completely. That's why you get a nullrefrenceexception.

I don't have a complete solution, but in general, you should drop "MyForm"
in your TestForm class. That means:

Dim myThread As New Threading.Thread(AddressOf FadeIn)
and
With Me 'instead of With MyForm

Second, rename sub start to sub main and set it as the startup object.

Third, add
Application.run(MyForm)
in Sub Main

Fourth, it should probably be
While .Opacity > 0


HTH

Armin


Dan Kelley

unread,
Aug 30, 2002, 8:08:50 AM8/30/02
to
If the code is failing at :

Dim myThread As New Threading.Thread(AddressOf
myForm.FadeIn) then try:

Dim myThread As New Threading.Thread(New ThreadStart
(AddressOf MyForm.FadeIn))

Dan

>.
>

0 new messages