3

在我的 C# 项目中,我使用 Timer.One(keyboard_timer) 用于观察用户是否按下 F8,另一个 Timer(clipboard_time) 用于观察剪贴板是否包含文本。在我的项目中,键盘始终启用当用户按下 F8 时,clipboard_timer 被启用。如果用户再次按下 F8,clipboard_timer 被禁用。我的项目做了什么当用户按下 F8 并复制一个单词时,我的项目会在窗口中显示复制的单词的含义。我的程序在后台运行并始终检查用户是否按 F8,如果他这样做了,那么我的程序一直检查剪贴板,如果它包含文本(单词),如果它每次都显示单词的含义。我的代码在这里:

在初始化

   keyboard_timer.Enabled = true;
        keyboard_timer.Tick += new EventHandler(keyboard_timer_Tick);

然后

  public void keyboard_timer_Tick(object sender, EventArgs e)
    {
        // clipboard enable
        if ((a % 2) != 0)
        {
      //  F9 is for Easy mood to eanble

             if ((GetAsyncKeyState(Keys.F9) == -32767) && hot_key == "F9")
            {
                label2.Text = "Easy";
                online_clipboard_active = "";
                Clipboard.Clear();
                clipboard_timer.Enabled = true;
                clipboard_timer.Tick += new EventHandler(clipboard_timer_Tick);
               ++a;           
            }


    ///// // F8 is for online Mood
            else if ((GetAsyncKeyState(Keys.F8) == -32767) && online_hot_key == "F8")
            {
                label2.Text = "Online";
                online_clipboard_active = "on";
                clipboard_timer.Enabled = true;                    
                clipboard_timer.Tick += new EventHandler(clipboard_timer_Tick);

                ++a;

            }

        }// end of enable 


        //clipboard disable
        if ((a % 2) == 0) // 
        {
            // F9 is for Easy mood to disable here
            if ((GetAsyncKeyState(Keys.F9) == -32767) && hot_key == "F9")
            {
                   label2.Text = "Off";
                    clipboard_timer.Enabled = false;
                    ++a;
            }

            // F8 is for online Mood to disable here
             else  if ((GetAsyncKeyState(Keys.F8) == -32767) && online_hot_key == "F8")
            {
                label2.Text = "Off";
                online_clipboard_active = "";
                clipboard_timer.Enabled = false;
               ++a;                   
            }

        }//end of clipboard disable

    }// end of keyboard timer

剪贴板计时器是

 public void clipboard_timer_Tick(object sender, EventArgs e)
    {

        if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
        {
            string x = Clipboard.GetText();
            Clipboard.Clear();

            if ((a%2)==0 && online_clipboard_active == "on")
            {
                //cal online_mood form to translate the string from googletranslator
                online_mood o = new online_mood(x);
                o.Show();
            }

            else if((a%2)==0 && online_clipboard_active == "")
            {
                //cal show_meaning form to show the meaning into a window
                show_meaning s = new show_meaning(x);
                s.Show();
            }        
        }

    }// end of clipboard timer Tick

我希望当剪贴板定时器启用时键盘定时器将被锁定,因为它们都使用变量。当剪贴板计时器运行时,键盘计时器将被锁定,然后当剪贴板计时器完成时它会工作,然后键盘计时器将被重新激活我该如何解决这个问题?有人帮帮我吗??????

4

2 回答 2

0

查看C# 锁

你基本上这样做:

public Object lockObject = new Object(); //You want a separate object so that it's not changed when lock is active


...    
//Code that can be run by any number of thread at the same time
...

lock(lockObject)
{
     ...
     //Code that is accessable by only one thread at a time:
     ...
}

...    
//Code that can be run by any number of thread at the same time
...

希望这对您有所帮助。

于 2013-03-07T14:01:34.187 回答
0

简单的锁定可以用一个静态成员来完成。

private static readonly object Sync = new object();

然后在设置或获取信息之前使用

lock(Sync)

锁定变量。

顺便说一句:我不明白你想要做什么。也许您应该使用表单的 Keypress 事件而不是计时器。

于 2013-03-07T14:04:28.143 回答