具有公共变量的线程

时间:2013-04-05 05:38:45

标签: c# multithreading

我正在构建一个操纵MouseWheel事件的NFS操纵杆。在我的主线程上,我调用了第二个线程,它以生成的频率触发键盘键。我的问题是,当您向上或向下移动MouseWheel时,此频率会随时间动态变化,但模拟器线程不会注意到此更改。这是我的代码片段:

int centro = 0;
static bool left;
static bool ismoving;
static int grado;
static int frec;

Thread simulator = new Thread(new ThreadStart(move));
public Form1()
{
    InitializeComponent();
}
static void mleft()
{
    KeyboardSimulator.KeyDown(Keys.G);
    Thread.Sleep(10);
    KeyboardSimulator.KeyUp(Keys.G);
}
static void mright()
{
    KeyboardSimulator.KeyDown(Keys.J);
    Thread.Sleep(10);
    KeyboardSimulator.KeyUp(Keys.J);
}
static void move()
{
    while (ismoving)
    {
        if (left)
        {
            mleft();
            Thread.Sleep(frec);
        }
        else
        {
            mright();
            Thread.Sleep(frec);
        }              
    }

}
void map()
{

    if (grado == 0)
    {
        ismoving = false;
        frec = 0;
    }
    else
    {
        int ut = 1000;//1seg
        left = grado > 0;
        grado = Math.Abs(grado);
        frec = ut / grado / 10;
        ismoving = true;
    }


}
private void Window_Closed_1(object sender, EventArgs e)
{
    simulator.Abort();
}
private void Form1_Load(object sender, EventArgs e)
{
    HookManager.MouseWheel += Window_MouseWheel_1;
    simulator = new Thread(new ThreadStart(move));
}
private void Window_MouseWheel_1(object sender, MouseEventArgs e)
{
    centro += e.Delta;
    grado = centro / 120;
    l1.Text = grado + "";
    map();
}

0 个答案:

没有答案