定时器加载时保存数据 [英] Saving Data while Timer Loads

查看:70
本文介绍了定时器加载时保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果数据来自用户,计时器会加载,表格将会打开:

While timer loads if the data comes from the user a form will be open:

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!

推荐答案

您好muhammed riyaaz,



很抱歉没有直接回答您的问题。但在我看来,你做错了。



*不要滥用在GUI线程上运行的计时器(如System.Windows.Forms.Timer)后台任务。使用适当的线程(例如使用BackgrondWorker,aync / TPL等)在后台发送/接收,或至少在GUI线程上运行的计时器。否则,您将通过定期阻止GUI线程来遇到性能问题。



*创建一种MessageReceived或MessageSent事件,然后决定显示/激活处理它们时的相应表格。所以你的后台发送者/接收者应该实现(并提升)事件。您的表格/控件/组件/随后应该处理这些事件(例如,将正确的(聊天)表格放在前面。



我只能猜到什么是错的你的代码现在 - 快速浏览一下,我认为你可能正在创建多个计时器。你是在创建该表单的多个实例(因此运行多个加载事件)吗? - 顺便说一下启动计时器你只需要设置Enabled或者调用Start(不是两个)



所以无论你怎么做我的意见,使用System.Windows.Forms计时器肯定不是你的意思想要!



祝你好运和亲切的问候
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


这篇关于定时器加载时保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆