vb.net多进程

时间:2013-04-09 05:12:32

标签: vb.net sendkeys

这是我的场景,我有两个sendkeys应用程序(使用计时器),我将其重写为一个,但我有一个问题,例如,如果我启用timer1它会发送大量的“q”直到我停止然后,如果我启用了timer2,它将发送“1”然后“2”然后发送“3”。该过程的输出是

qqqqq123q123q123q123q123q123

并且它不是我想要的,在合并两个sendkeys之前,输出将是我这样的东西

qqqq1qqq2qq3qqqqqq1q2q3qqqq1

两个计时器具有相同的间隔。当我将这两个计时器合并到一个runnnig应用程序时发生的事情就像timer1然后timer2然后再次timer1,他们喜欢交替进程而不是同时执行它。希望可以有人帮帮我。 THX

1 个答案:

答案 0 :(得分:0)

请看一下这个参考:http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

它说:

  

此Windows计时器专为单线程环境而设计   UI线程用于执行处理

所以这是单线程的,因为你的计时器的间隔是相同的。他们将按顺序行事。您应该使用System.Threading.Thread。 请参阅下面的示例。您可以创建一个参数化的线程对象,该对象采用字符串参数作为sendkeys应该在该线程上发送的内容。并开始两个或更多线程。

using System;
using System.Threading;

// Simple threading scenario:  Start a static method running 
// on a second thread. 
public class ThreadExample {
    // The ThreadProc method is called when the thread starts. 
    // It loops ten times, writing to the console and yielding  
    // the rest of its time slice each time, and then ends. 
    public static void ThreadProc() {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("ThreadProc: {0}", i);
            // Yield the rest of the time slice.
            Thread.Sleep(0);
        }
    }

    public static void Main() {
        Console.WriteLine("Main thread: Start a second thread.");
        // The constructor for the Thread class requires a ThreadStart  
        // delegate that represents the method to be executed on the  
        // thread.  C# simplifies the creation of this delegate.
        Thread t = new Thread(new ThreadStart(ThreadProc));

        // Start ThreadProc.  Note that on a uniprocessor, the new  
        // thread does not get any processor time until the main thread  
        // is preempted or yields.  Uncomment the Thread.Sleep that  
        // follows t.Start() to see the difference.
        t.Start();
        //Thread.Sleep(0); 

        for (int i = 0; i < 4; i++) {
            Console.WriteLine("Main thread: Do some work.");
            Thread.Sleep(0);
        }

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
        t.Join();
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
        Console.ReadLine();
    }
}

此示例来自:http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx