简体   繁体   English

在C#中锁定线程

[英]Locking thread in C#

In my C# project I m using the Timer.One(keyboard_timer) is for to watch if a user press F8 or not and another Timer(clipboard_time) is to watch if Clipboard contains a text or not.In my project The keyboard is always enable and the clipboard_timer is enabled when a user press F8.If the user again press F8 the clipboard_timer is disabled.what my project does that When user press F8 and he copies a word then my project show the meaning of the copied word in a window.my program runs on the background and always check if a user press F8 if he does then all-time my program check the clipboard, if it contains a text(word) if it does the show the meaning of the word everytime. 在我的C#项目中,我使用Timer.One(keyboard_timer)来观察用户是否按F8而另一个Timer(clipboard_time)是否要观看剪贴板是否包含文本。在我的项目中键盘始终启用当用户按下F8时,启用clipboard_timer。如果用户再次按F8,则clipboard_timer被禁用。我的项目执行的操作当用户按F8并复制一个单词时,我的项目会在窗口中显示复制的单词的含义。我的程序在后台运行,并始终检查用户是否按F8然后我的程序检查剪贴板,如果它包含文本(单词),如果它显示每次单词的含义。 My code is here: 我的代码在这里:

On the initialize 在初始化

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

then 然后

  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

Clipboard timer is 剪贴板计时器是

 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

I want that when clipboard timer enable on that time keyboard timer will be lock because both of them uses a variable. 我希望当剪贴板计时器启用时,键盘计时器将被锁定,因为它们都使用变量。 When clipboard timer runs then keyboard timer will be lock and then when clipboard timer finished it works then the keyboard timer will be reactivated How can I solve this? 当剪贴板计时器运行时,键盘计时器将被锁定,然后当剪贴板计时器完成它工作,然后键盘计时器将被重新激活我如何解决这个问题? Anyone give me any help????? 有人给我任何帮助?????

Look into C# locks 查看C#锁

you basicly do this: 你基本上这样做:

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

Hope this helps you out a bit. 希望这会帮助你一点点。

Simple locking can be accomplish with a static member. 使用静态成员可以实现简单锁定。

private static readonly object Sync = new object();

and then before setting or getting the info use 然后在设置或获取信息之前使用

lock(Sync)

to lock the variable. 锁定变量。

BTW: I don't get what you are trying to do. 顺便说一句:我不知道你想要做什么。 Maybe you should use the Keypress event of the form instead of the timers. 也许您应该使用表单的Keypress事件而不是计时器。

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

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