hardtoking
hardtoking
采纳率31.4%
2017-03-07 12:57 阅读 2.6k

C#线程问题,执行循环,定时器停止了

50

我在软件中定义了一个定时器,循环执行一个方法
然后我在其他方法中又定义了一个循环,当循环执行时,定时器就停止执行了

有什么方法可以让定时器与循环同步执行

求大神帮忙解答

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • baipiang baipiang 2017-03-07 13:12

    建议你把代码贴出来,这样有助于分析到底是程序问题还是逻辑问题

    点赞 评论 复制链接分享
  • hardtoking hardtoking 2017-03-07 14:12

    public void StartRunTime(string strs)
    {
    AutomationElement deskTop = AutomationElement.RootElement; //桌面进程
    var firstSC = deskTop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "WindowsForms10.Window.8.app.0.141b42a_r34_ad1"));
    var secondSC = firstSC.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "WindowsForms10.MDICLIENT.app.0.141b42a_r34_ad1"));
    var thitdSC = secondSC.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "WindowsForms10.Window.8.app.0.141b42a_r34_ad1"));
    //MessageBox.Show(thitdSC.Current.Name);
    var fourthSC = thitdSC.FindAll(TreeScope.Children, Condition.TrueCondition);
    var fatherSC = fourthSC[7];
    var fatherAll = fatherSC.FindAll(TreeScope.Children, Condition.TrueCondition);
    var TCK = fatherAll[1];
    /* TCK.GetType()*/
    ;
    //MessageBox.Show(TCK.Current.NativeWindowHandle.ToString());

                int CRKJB = TCK.Current.NativeWindowHandle;
    
                //Thread.Sleep()
                //keywordinput[1]  句柄
    
                SetForegroundWindow((IntPtr)CRKJB);    //设置活跃窗口
                SendMessage((IntPtr)CRKJB, BM_CLICK, 0, 0);     //鼠标点击
                SendKeys.SendWait("^{a}");    //清空里面原有的数据
                SendKeys.SendWait(strs);   //发送数据
    
            var firstsSC = deskTop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "WindowsForms10.Window.8.app.0.141b42a_r34_ad1"));
            var secondsSC = firstSC.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "WindowsForms10.MDICLIENT.app.0.141b42a_r34_ad1"));
            var thitdsSC = secondSC.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "WindowsForms10.Window.8.app.0.141b42a_r34_ad1"));
            /ar fourthSC = thitdSC.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "WindowsForms10.Window.8.app.0.141b42a_r34_ad1"));
            /ar fifthSC = fourthSC.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "WindowsForms10.Window.b.app.0.141b42a_r34_ad1")); 
            //MessageBox.Show(thitdsSC.Current.Name);
            var thitdChildre = thitdsSC.FindAll(TreeScope.Children, Condition.TrueCondition);
            var FatherForm = thitdChildre[0];
            var fatherForm2 = FatherForm.FindAll(TreeScope.Children, Condition.TrueCondition);
            var YJTC = fatherForm2[3];                   //获取一键题词按钮
            var nextSX = thitdChildre[1];                //获取下一步 进入筛选 按钮
            InvokePattern btnYJTC = (InvokePattern)YJTC.GetCurrentPattern(InvokePattern.Pattern);   //点击一键题词按钮
            btnYJTC.Invoke();
    
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            //IntPtr FW = FindWindow(null, "提示");
            //IntPtr hwnd = FindWindowEx(FW, IntPtr.Zero, null, "&OK");
            //SendMessage(hwnd, BM_CLICK, 0, 0);
    
    
            IntPtr mainwind = FindWindow(null, "提示");
            IntPtr buttok = FindWindowEx(mainwind, IntPtr.Zero, null, "&OK");
            if (buttok!=IntPtr.Zero)
            {
                SendMessage(buttok, WM_LBUTTONDOWN, IntPtr.Zero, null);
                SendMessage(buttok, WM_LBUTTONUP, IntPtr.Zero, null);
    
                NextStep();
                SelectLenth();
            }
    
        }
    
        //执行
        private void button7_Click(object sender, EventArgs e)
        {
    
            for (int i = 0; i < strList.Count; i++)
             {
                        StartRunTime(strList[i]);
    
              }
    
        }
    
    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2017-03-07 15:21

    那你直接循环+延迟就可以了,为什么要定时器

    点赞 评论 复制链接分享
  • yangbo50304 yangbo50304 2017-03-08 01:16

    看代码视乎是你在timer中模拟发送了一个buttonclick事件,如果这样的话,你的button不处理完click,timer肯定是卡主的,
    因为你用了SendMessage,SendMessage就是将线程卡主,直到处理完才往下走,你换成PostMessage试试呢?

    点赞 评论 复制链接分享

相关推荐