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

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

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

求大神帮忙解答

4个回答

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

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]);

          }

    }

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

hardtoking
hardtoking 回复caozhy: 我用循环加延迟试过,不过程序总会很卡
3 年多之前 回复

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问