Home All Groups Group Topic Archive Search About

Threading Delegate Question

Author
8 Dec 2006 7:12 PM
BrianDH
Hi
I have a textbox that is created on my "main form thread".  However I need
to be able to change its background color, flashing, using a 2nd thread.

I have this working BUT sometimes I get an error "object reference not set
to an instance of an object".

I am new to multi threading so I am sure I must be missing something.

Example: (((
Public Sub Thread2_Run()
        Try
            While (Not stopApplication)
                Dim Z As Integer = 0
                While ((bFlashing) And (Not stopApplication))
                       If (intflag = 0) Then
                            intflag = 1
                            SetColor(System.Drawing.Color.WhiteSmoke)
                        Else
                            intflag = 0
                            Select Case Z
                                Case Is < 120
                                    SetColor(System.Drawing.Color.Yellow)
                                Case Is > 120
                                    SetColor(System.Drawing.Color.Red)
                            End Select
                        End If
                    Thread.Sleep(1000)
                    Z = Z + 1
                  End If
                End While
                If (Not stopApplication) Then
                    Thread.Sleep(3000)
                End If
            End While
        Catch ex As Exception
            WriteErrortoLog(ex)
        End Try
    End Sub
    Delegate Sub SetBGColorCallback(ByVal [Colors] As System.Drawing.Color)
    Private Sub SetColor(ByVal [Colors] As System.Drawing.Color)
        Try
            If Me.bteUbconfirmed.InvokeRequired Then
                Dim d As New SetBGColorCallback(AddressOf SetColor)
                Me.Invoke(d, New Object() {[Colors]})
            Else
                bteUbconfirmed.BackColor = [Colors]
            End If
        Catch ex As Exception
            WriteErrortoLog(ex)
        End Try
    End Sub
)))

Thanks for any help
Brian

Author
9 Dec 2006 5:39 AM
Cor Ligthert [MVP]
Brian,

> I am new to multi threading so I am sure I must be missing something.
>
That you don't have to tell, you should only use multithreading if you need
it. In my idea can there be not even the slightest argument to change a
colour in an other UI using multithreading.

Cor
Author
9 Dec 2006 6:50 PM
BrianDH
OK, then how would you make an item blink?

Show quoteHide quote
"Cor Ligthert [MVP]" wrote:

> Brian,
>
> > I am new to multi threading so I am sure I must be missing something.
> >
> That you don't have to tell, you should only use multithreading if you need
> it. In my idea can there be not even the slightest argument to change a
> colour in an other UI using multithreading.
>
> Cor
>
>
>
Author
9 Dec 2006 7:11 PM
Cor Ligthert [MVP]
Just use a forms.form.timer.

It is very simple to easy to show you the code.

Cor


Show quoteHide quote
"BrianDH" <Bria***@discussions.microsoft.com> schreef in bericht
news:B7F8D610-C944-4465-BB93-814832E90917@microsoft.com...
> OK, then how would you make an item blink?
>
> "Cor Ligthert [MVP]" wrote:
>
Author
10 Dec 2006 5:33 PM
Branco Medeiros
BrianDH wrote:
> I have a textbox that is created on my "main form thread".  However I need
> to be able to change its background color, flashing, using a 2nd thread.
>
> I have this working BUT sometimes I get an error "object reference not set
> to an instance of an object".
<snip>

What line causes the error? In what circunstances the error pops up?

Regards,

Branco.
Author
11 Dec 2006 2:49 PM
BrianDH
Branco

Thank You for trying to help me.  I am NOT using a timer because I was told
"not to use a timer".  

I get an error from the "Try" around my "SetColor Sub".  
"object reference not set to an instance of an object".

It does Not happen all the time, just now and then. 

I was trying to get input from the group on my sub.  Am I doing something
wrong
within the sub? 

Example: (((
Public Sub Thread2_Run()
        Try
            While (Not stopApplication)
                Dim Z As Integer = 0
                While ((bFlashing) And (Not stopApplication))
                       If (intflag = 0) Then
                            intflag = 1
                            SetColor(System.Drawing.Color.WhiteSmoke)
                        Else
                            intflag = 0
                            Select Case Z
                                Case Is < 120
                                    SetColor(System.Drawing.Color.Yellow)
                                Case Is > 120
                                    SetColor(System.Drawing.Color.Red)
                            End Select
                        End If
                    Thread.Sleep(1000)
                    Z = Z + 1
                  End If
                End While
                If (Not stopApplication) Then
                    Thread.Sleep(3000)
                End If
            End While
        Catch ex As Exception
            WriteErrortoLog(ex)
        End Try
    End Sub
    Delegate Sub SetBGColorCallback(ByVal [Colors] As System.Drawing.Color)
    Private Sub SetColor(ByVal [Colors] As System.Drawing.Color)
        Try
            If Me.bteUbconfirmed.InvokeRequired Then
                Dim d As New SetBGColorCallback(AddressOf SetColor)
                Me.Invoke(d, New Object() {[Colors]})
            Else
                bteUbconfirmed.BackColor = [Colors]
            End If
        Catch ex As Exception
            WriteErrortoLog(ex)
        End Try
    End Sub
)))


There could be external reasons for the error.  However the error does state
that th

Brian
Show quoteHide quote
"Branco Medeiros" wrote:

>
> BrianDH wrote:
> > I have a textbox that is created on my "main form thread".  However I need
> > to be able to change its background color, flashing, using a 2nd thread.
> >
> > I have this working BUT sometimes I get an error "object reference not set
> > to an instance of an object".
> <snip>
>
> What line causes the error? In what circunstances the error pops up?
>
> Regards,
>
> Branco.
>
>
Author
11 Dec 2006 6:13 PM
Branco Medeiros
BrianDH wrote (inline):
<snip>
> I am NOT using a timer because I was told
> "not to use a timer".

Whoa! By whom?

> I get an error from the "Try" around my "SetColor Sub".
> "object reference not set to an instance of an object".

The problem is: this doesn't point to the exact line where the error
occurred, and thus there's no way to know which object was not set: is
it the text box? the delegate? etc...

<snip>
>                        If (intflag = 0) Then
<snip>

where does this flag come from? Is it global? Can it be that you have
Option Explicit Off? And what about Option Strict?

<snip>
>                         End If
>                     Thread.Sleep(1000)
>                     Z = Z + 1
>                   End If
<snip>

Where does this last "End If " belongs? If I just copy/paste the code,
this particular End If is marked as an error...

<snip>
> There could be external reasons for the error.  However the error does state
> that th
<snip>

=))
What external reasons? Are there other threads running?

Regards,

Branco.
Author
11 Dec 2006 8:05 PM
Lucian Wischik
BrianDH <Bria***@discussions.microsoft.com> wrote:
>Thank You for trying to help me.  I am NOT using a timer because I was told
>"not to use a timer".  

Whoever told you that was badly wrong. Timers are the by far the best
solution for flashing a textbox. Threads are definitely a bad
solution.

The work you're doing here is solely UI rather than computational. So
a thread would have to synchronize on the UI thread anyway, which
would actually make it LESS efficient than timers.

(My professional work involves automatic analysis of multithreaded
programs for concurrency bugs, and I do love threads, so it pains me
to advise you against them. But you generally have to treat them with
respect, use them only where the substantial additional complexity
they bring is justified, and use them in ways as simple as possible so
as to mitigate that complexity.)

--
Lucian