timer or thread?

bpayne111

Well-known member
Joined
Feb 28, 2003
Messages
326
Location
BFE
i have a user control which displays some text and fades the colors randomly.
Would using a timer to fade the text create issues in a multithreadd environment?
If so would using Thread.Sleep be a better option to make this work correctly in any application of mine?

thanks
brandon
 
Threads, in my point of view and experience, are more relyable but... and theres allways a but in everything...

Its not SAFE to deal with object between threads.
What Im trying to say its that you may experience some issues if you control an object that its running on one thread from another thread.
To do this, MS tells us to use delegates... "delegating" this way the task of dealing with the object to a method on the same thread of the object...

Alex :D
 
i appreciate yoru comment but it seems you sort of halfway answered my question
my original question " Would using a timer to fade the text create issues in a multithreadd environment?"
In order to better specify what is happened ill explain my project more in depth.
I have created a control which is simply my initials painted on the control that fade to random colors constantly. Im using this control as my own personal tag for my programs.
It works wonderful as of now but my concern is.... if i throw this control on say the client of a server app, will it not work correctly using a timer.
Your response makes me ask another question though... you say threads arent safe but wouldnt using a lock block in my class when the class decides to update be ok? in reality no other control will ever change my control because its just a bit of text that changes color. so even locking it wouldnt seem to have an effect on any other threads to me.
id also like to check and see if i interpreted your post right
in other words you say ms says that delegating basically means dont use threads thats how it sounds to me.
i think im going to create my project both ways and see how thigns work out. id stil llike to hear your input on things again though.

thanks
brandon
 
Hi again ...

Ill begin from the bottom...

- You missunderstood... MS tells that you have to use delegates to step out of a thread and work on the object running on another thread.

A cute a simple example where you can see a thread messing all up your UI is:
Create a form with a DataGrid and fill this datagrid with a table that have some records... make it 1000 records!
Now put the Fill process of the dataset running on a seperated thread (so it just dont frizes the app).

Just with this youll see columns, scrollbars missdrown and possibly not drown at all...



By your project compexity the clock just works fine...
What you can do its just dont put the code that changes the colors inside the Tick of the clock. Delegate it to a method on the main Thread...

Alex :D
 
Actually if I understand your problem correctly it seems the easiest way to do it would be to make an about box with your control on it, then set the about box to open it its own thread. Any controls on it then would operate separately from the main program.

Just a suggestion. Alot easier than having to assign delegates and worry about possible bad interactions.
 
The problem here its a little diferent...
Its the timer or a thread to use and how to use it inside the control... not a form or something like you said...

Alex :D
 
If the instance of the control is created within the second thread then there shouldnt be a problem.

1. make your control complete with timer in the control.

2. make a form2 class for about box and add code to form2 class to instantiate your custom control with timer

3. In the main application make a function that creates an instance of form2 in a separate thread.... form2 and any contained controls will be in your 2nd thread.

Your timer should be able to work then in that second thread since it is actually created as you make the second thread making it a part of that process.

I understand what you are talking about.... but it seems like unnessasary hassle.
 
why would using tick to make calculations be different than having tick call a method?

i know little about delegates because im a c# rookie :(
actually i just realized i wrote this one in vb but i can convert it to c# very quickly if i need delegates
i am posting the code of this class so that it is obvious what this thing does.
the code is rather rough because well im lazy i guess.



Code:
Public Class BPLogo
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        This call is required by the Windows Form Designer.
        InitializeComponent()
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.DoubleBuffer, True)
        currentRGB = randomRGB.Next(0, 3)


        Add any initialization after the InitializeComponent() call

    End Sub

    UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    NOTE: The following procedure is required by the Windows Form Designer
    It can be modified using the Windows Form Designer.  
    Do not modify it using the code editor.
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        
        Timer1
        
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 50
        
        BPLogo
        
        Me.Name = "BPLogo"
        Me.Size = New System.Drawing.Size(48, 48)

    End Sub

#End Region
 


#Region "Declarations"
    this control will be the Letters BP in the corner of the screen, they will change
     colors at a givin rate, adjust to different sizes
    in the future i plan to use this buttton as my exit/minimize button on a custom form
    Private mColor As Color = Color.AliceBlue
    Private mSize As Short = 20

    Dim myFont As New Font("Sylfaen", mSize, FontStyle.Bold)
    Dim myBrush As New SolidBrush(mColor)

    Dim randomRGB As New Random(3)
    Dim randomIncrementTo As New Random(111)
    Dim increment As Short = 1
    Dim value As Short

    Dim rgb As Short() = {0, 0, 0}
    Dim currentRGB As Byte
    Dim valueReached As Boolean = True

#End Region

#Region "Properties"
    Public Property LogoSize() As Short
        Get
            Return mSize
        End Get
        Set(ByVal Value As Short)
            mSize = Value
            myFont = New Font("Sylfaen", mSize, FontStyle.Bold)
            Dim mycolr As Color

        End Set
    End Property

    Public Property StartColor() As Color
        Get
            Return mColor
        End Get
        Set(ByVal Value As Color)
            mColor = Value
        End Set
    End Property
#End Region

#Region "Methods"
    Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
        Me.Height = Me.Width
        mSize = (Me.Width / 2)
        myFont = New Font("Sylfaen", mSize, FontStyle.Bold)
        Me.Refresh()
    End Sub
#End Region

#Region "Events"
    Private Sub BPLogo_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        e.Graphics.DrawString("BP", myFont, myBrush, 0, 0)

    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        myBrush.Color = Drawing.Color.FromArgb(rgb(0), rgb(1), rgb(2))
        Me.Refresh()

        If valueReached Then
            currentRGB = randomRGB.Next(0, 3)

            Do While valueReached = True

                value = randomIncrementTo.Next(0, 255)
                If rgb(currentRGB) < value Then
                    increment = 1
                    valueReached = False
                ElseIf rgb(currentRGB) > value Then
                    increment = -1
                    valueReached = False
                Else
                    valueReached = True
                End If
            Loop
        End If

        rgb(currentRGB) += increment
        If rgb(currentRGB) = value Then valueReached = True

    End Sub
#End Region

End Class

so this paints 2 letters and changes the letters colors randomly constantly.
This control will be added to any program i create as my personal trademark.
One of the first applications im going to add it to is a Client/Server app.

i never plan on editing anything about this control during runtime. its basically completly self contained.
my concern is. If im loading a database or something of that nature. and my control is visible. Will it continue to change colors using a timer or will it stop untill the db loads?
that is what makes me think i should use a thread instead of a timer to change the colors.

to sum it all up. I want this control to change colors no matter what is happening in any app that is using it and im worried the timer wont accomplish that.

thanks for your help guys i hope i can figure this one out before it destroys some app of mine while im debugging.

merry christmas sorry i made this so long
brandon
 
I think were complicating this issue a bit but, as youve putted it, the thread its the only way...

Think about it this way... if your app frizes loading a database, the tick wont raise until the the app "unfrizes"...
So, based on this idea, youll allways have to do this on a seperated thread...

Pesonally, I dont think this is a such important thing that must woth all this work. But if you think it worths go ahead.

Just dont forget this... if the main thread its stuck doing some task all object in that thread are also stuck...


Alex :D
 
thanks
i guess i did answer my own question in a way.
and yes i really want this to change colors cuz the best art ive created is the logo next to my name in this forum (sad eh?)
so taking a cool font and changing the color of its text at random will kind of make me feel better about not being artistic.
well i am artistic but i have to play CHESS to show it ;)
thanks for even considering reading all the crap i posted here
i should have realized the answer myself
MERRY CHRISTMAS
brandon
 
Ok...
Like I told you... I think we kind of complicated this abit but it must be the christmas "air"! :p

Anyways... if theres anything I can help you can count on me...

Mery Christmas and a great new year...

Alex :D
 
Back
Top