简体   繁体   中英

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". 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

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. hope someone can help me. thx

Take a look at this reference: 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

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. See the example below. You could make a paramaterized thread object that takes a string argument as what sendkeys should send on that thread. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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