Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Timer isn't ticking!
Message
From
08/11/2008 07:56:26
Mark Hall
Independent Developer & Voip Specialist
Keston, Kent, United Kingdom
 
 
To
07/11/2008 13:36:02
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows XP
Database:
MS SQL Server
Miscellaneous
Thread ID:
01360368
Message ID:
01360662
Views:
5
>>I did put a debug.writeln("tick fired") but it was never displayed in the debug output window, so the tick code isn't executed.
>>
>>I know that the code that enables the timer is running, as it also changes the text of a label, and changes the background color of the custom control.
>>
>>OK, here's some of the code. It's my first ever C# application, so may be a little disorganised.
>>
>>First, the event handler that gets called whenever the state of a phone line changes.
>>clsLine manages the interface to the actual phone system, and raises events when calls come and go.
>>The code must allow for a variable number of physical phone lines, so at application startup, the number of lines
>>is detected and a corresponding number of clsLine and CtlPhoneLine1 (on-screen representation) objects
>>are created.
>>When the state of a phone line changes, this code then loops through the controls on the form to find the
>>correct matching on-screen representation of the appropriate phone line (by matching Index property).
>>Do let me know if there is a better way of doing this.
>>This code is a method on the main form
>>
>>      private void OnLineStatChg(object sender, EventArgs e)
>>        {
>>            //This event handler fires whenever the status of a clsLine (phone line) changes
>>            //Find the corresponding screen line representation, then call the refresh code, below.
>>  
>>            //I added the following line for debugging and it does display the text
>>            if (this.InvokeRequired == true) Debug.WriteLine("Invoke Required");
>>
>>            clsLine theline = (clsLine) sender;
>>            foreach (CtlPhoneLine1 obj in this.Controls)
>>            {
>>                if (obj is CtlPhoneLine1)
>>                {
>>                    if (obj.Index == theline.Index)
>>                    {
>>                        this.RefreshLine(theline, obj);
>>                        goto FoundIt;
>>                    }
>>                }
>>            }
>>            FoundIt:
>>            return;
>>        }
>>
>>
>>Once the correct CtlPhoneLine1 is found, another form method is called to make the actual changes.
>>
>>       private void RefreshLine( clsLine LineObj, CtlPhoneLine1 LineCtl)
>>        {
>>            //This code sets the properties of the appropriate ctlPhoneLine control
>>            //When a call enters or leaves the queue.
>>            LineCtl.CallerId = LineObj.Callerid;
>>            LineCtl.CallerIdName = LineObj.Callername;
>>            switch (LineObj.State)
>>            {
>>                case LineState.LineClear:
>>                    LineCtl.Status = "Clear";
>>                    break;
>>                case LineState.LineRinging:
>>                    LineCtl.Status = "Ringing";
>>                    break;
>>                case LineState.lineHungup:
>>                    LineCtl.Status = "Hungup";
>>                    break;
>>            }
>>        }
>>
>>
>>CtlPhoneLine1 is my custom control.
>>In the Set method of the Status property of CtlPhoneLine1, the code that adjusts the appearance of various parts of the control runs.
>>
>>        public string Status
>>        {
>>            get
>>            {
>>                return this._status;
>>            }
>>            set
>>            {
>>                this._status = value;
>>                //Call a method to re-set control appearance based on state
>>                this.SetAppearance(this._status);
>>            }
>>        }
>>
>>
>>And finally, the CtlPhoneLine1 SetAppearance method makes the changes.
>>
>>       private void SetAppearance(string state)
>>        {
>>            switch (state)
>>            {
>>                case "Ringing":
>>                    tmrFlash.Start();
>>                    lblStatus.Text = state;
>>                    this.BackColor = Color.Red;
>>                    break;
>>                case "Hungup":
>>                    tmrFlash.Stop();
>>                    lblStatus.Text = "Available";
>>                    this.BackColor = Color.Yellow;
>>                    break;
>>                case "Clear":
>>                    tmrFlash.Stop();
>>                    lblStatus.Text = "Available";
>>                    this.BackColor = Color.Yellow;
>>                    break;
>>            }
>>        }
>>
>>
>>On my form, I was able to reference an Enum that I create for LineState, but notice that I had to then change that to a string when passing through to the CtlPhoneLine1 control. This is because I developed the control as a seperate project (added as part of the whole solution), but I can't seem to access classes and types created as part of the main solution from within CtlPhoneLine1. I have tried adding a using.. of r the main project namespace but it didn't work - any suggestions here too would be useful.
>>
>>Thanks,
>
>I just sent you a private email.
>Tim

Hi Tim,

Thanks for the info. Your suggestion of using InvokeRequired and Invoke did the trick. Once I found the MSDN article on the subject and plumbed it all in, everything is fine.

I changed my handler for new/dropped phone calls to the following:
        private delegate void RefreshLineCallback(clsLine LineObj, CtlPhoneLine1 LineCtl);

        private void OnLineStatChg(object sender, EventArgs e)
        {
            //This event handler fires whenever the status of a clsLine (phone line) changes
            //Find the corresponding screen line representation, then call the refresh code, below.

            clsLine theline = (clsLine)sender;
            foreach (CtlPhoneLine1 obj in this.Controls)
            {
                if (obj is CtlPhoneLine1)
                {
                    if (obj.Index == theline.Index)
                    {
                        if (this.InvokeRequired == true)
                        {
                            this.Invoke(new RefreshLineCallback(RefreshLine), theline, obj);
                        }
                        else
                        {
                            this.RefreshLine(theline, obj);
                        }
                        goto FoundIt;
                    }
                }

            }
        FoundIt:
            return;
        }
A good lesson learned here - one which I think every .NET Winforms developer will come across at some time or other.

Thanks again.
Regards
Mark

Microsoft VFP MCP
Menulib - OO Menus for VFP www.hidb.com/menulib
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform