Click here to Skip to main content
15,921,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While timer loads if the data comes from the user a form will be open:
C#
private void Tutor_Login_Load(object sender, EventArgs e)
        {
            this.Text = Login.User_Name + " (" + Login.ID + ")";
            timer1.Start();
            timer1.Enabled = true;
            timer1.Interval = 5000;

        }

 private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                string Role = Login.Role;
                XmlNode Node;
                string Msg = string.Empty;
                int Suc;
                string tmpSuc = string.Empty;
                int tmpParam;
                string i;
                XmlNode tmpNode;
                string tmpChat = string.Empty;
                string chtdtm = "yyyy-MM-d";
                string date;
                objService.STUDENTS_ONLINE(Role, out Node, out Msg, out Suc);
                DataTable dt = new DataTable();
                dt.Columns.Add("Students_Name", typeof(string));
                dt.Columns.Add("ID", typeof(int));
                dt.Columns.Add("Status", typeof(string));
                foreach (XmlNode node in Node)
                {
                    DataRow dr = dt.NewRow();
                    dr["Students_Name"] = node["Name"].InnerText;
                    Student_Name = Convert.ToString(dr["Students_Name"]);
                    dr["ID"] = node["ID"].InnerText;
                    Student_ID = Convert.ToInt32(dr["ID"]);
                    objService.CHAT_RECEIVE_MESSAGE(Student_ID, Tutor_ID, out tmpNode, out Chatting, out date, out tmpParam, out tmpSuc);
                    DateTime today = DateTime.Today;
                    string tmpdate;
                    tmpdate = today.ToString(chtdtm);

                    if (Chatting != string.Empty)
                    {
                        if (tmpdate == date)
                        {
                            var TC = new Tutor_Chat();
                            var openedForm = Application.OpenForms[TC.tmpRName];

                            if (openedForm != null)
                            {
                                if (Student_ID != tmpSid)
                                {
                                    TC.Show();
                                }
                                else
                                {
                                    openedForm.WindowState = FormWindowState.Normal;
                                    openedForm.BringToFront();
                                    openedForm.Activate();
                                }
                            }
                            else
                            {
                                TC.Show();
                            }
                        }
                    }
                    tmpSid = Student_ID;
                    i = node["Status"].InnerText;
                    if (i == "true")
                    {
                        dr["Status"] = "online";
                    }
                    else
                    {
                        dr["Status"] = "offline";
                    }
                    dt.Rows.Add(dr);
                }
                dataGridView1.DataSource = dt;
            }

            catch (Exception ex)
            {
                throw ex;
            }

        }


My Issue:
If again my timer gets loaded the form open for the user is again gets opened and the process keeps on continuing.

Solution Need:
If a form open for an user while the timer loads initially, again after the timer interval if the timer gets loaded it should not open the form for the same user which is already opened!
Posted
Updated 3-Jun-13 1:10am
v2
Comments
CHill60 3-Jun-13 7:16am    
How are you loading form Tutor_Login? Is it in Main() or from a button click? And you seem to be trying to do a lot of work every 5 seconds ... are you *sure* you want this on a timed event?
riodejenris14 3-Jun-13 8:34am    
Hello Chill! ya i'm loading the form tutor_login in main_form_load function as it is a chat app i need to load the timer every five seconds to get response from the sender to receiver....

1 solution

Hi muhammed riyaaz,

Sorry for not directly answering your question. But in my opinion you are doing it wrong.

* Don't abuse a timer running on the GUI thread (like System.Windows.Forms.Timer)for such an background task. Use proper threading (e.g. with BackgrondWorker, aync/TPL, whatever) for sending/receiving in the background, or at least a timer not running on the GUI thread. Otherwise you will run into performance problems by blocking the GUI thread periodically.

* Create kind of "MessageReceived" or "MessageSent" events and then decide to show/activate the corresponding Form while handling them. So your background sender/receiver should implement (and raise) the events. Your Forms/Controls/Components/whatever should then handle these events (e.g. by bringing the correct (Chat-)Form to front.

I just can guess whats wrong with your code now - a quick look leads me to the opinion that maybe you are creating more than one timer. Are you creating multiple instances of that Form (and therefore run through multiple load-events)? - and btw. to start the timer you just need to set Enabled or call Start (not both)

So whatever you do with my opinion, using a System.Windows.Forms timer is for sure not what you want!

Good luck and kind regards
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900