简体   繁体   English

vb.net多进程

[英]vb.net multi process

here is my scenario, i have two sendkeys application (using timer) and i rewrite it to as one, but im having a problem, for example, if i enable the timer1 it will send lots of "q" until i stop it, then if i enabled the timer2 it will send "1" then "2" then "3". 这是我的情况,我有两个sendkeys应用程序(使用计时器),我将其重写为一个,但是我遇到了问题,例如,如果我启用了timer1,它将发送很多“ q”直到我停止它,然后如果我启用了timer2,它将发送“ 1”,然后发送“ 2”,然后发送“ 3”。 the output of that process is 该过程的输出是

qqqqq123q123q123q123q123q123

and it's not what i wanted, before i merge the two sendkeys the output will me something like this 这不是我想要的,在我合并两个sendkey之前,输出将是这样的

qqqq1qqq2qq3qqqqqq1q2q3qqqq1

both timer have same interval. 两个计时器具有相同的间隔。 the thing that happen when i merge those two timer in to one runnnig application is like the timer1 then timer2 then timer1 again, they like alternating process instead of doing it in the same time. 当我将这两个计时器合并到一个runnnig应用程序中时,发生的事情就像是timer1然后timer2然后再次是timer1,他们喜欢交替处理而不是同时进行。 hope someone can help me. 希望可以有人帮帮我。 thx 谢谢

Take a look at this reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx 看一下此参考: http : //msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

It says: 它说:

This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing 该Windows计时器是为使用UI线程执行处理的单线程环境而设计的

So this is single threaded, as the intervals of your timer are the same. 所以这是单线程的,因为计时器的间隔是相同的。 They will behandled sequentially. 他们将被顺序处理。 You should use System.Threading.Thread instead. 您应该改用System.Threading.Thread。 See the example below. 请参见下面的示例。 You could make a paramaterized thread object that takes a string argument as what sendkeys should send on that thread. 您可以创建一个带字符串参数的参数化线程对象,作为该线程上应该发送的sendkeys。 And start two, or more threads. 并启动两个或更多线程。

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();
    }
}

This example came from: http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx 该示例来自: http : //msdn.microsoft.com/zh-cn/library/system.threading.thread.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM