|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Threading Delegate QuestionI 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 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 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 > > > 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: > BrianDH wrote:
> I have a textbox that is created on my "main form thread". However I need <snip>> 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". What line causes the error? In what circunstances the error pops up? Regards, Branco. 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. > > BrianDH wrote (inline):
<snip> > I am NOT using a timer because I was told Whoa! By whom?> "not to use a timer". > I get an error from the "Try" around my "SetColor Sub". The problem is: this doesn't point to the exact line where the error> "object reference not set to an instance of an object". 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 <snip>> Thread.Sleep(1000) > Z = Z + 1 > End If 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 <snip>> that th =)) What external reasons? Are there other threads running? Regards, Branco. BrianDH <Bria***@discussions.microsoft.com> wrote:
>Thank You for trying to help me. I am NOT using a timer because I was told Whoever told you that was badly wrong. Timers are the by far the best>"not to use a timer". 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
Interlocked.Add Not Thread Safe with Longs on a 32bit system
Data Relations with DataTable Class Data access layer for SQL Server & MS Access vbs to vb.net Error: The transport failed to connect to the server Is it possible to display images on desktop (not as an icon)? How to Insert field and value into another grid vbQuestion error how 2 approximate 0.1 to be 1 Reading int64 from file & converting to DateTime |
|||||||||||||||||||||||