调用锁线程中的问题 [英] Invoke problem in the Lock thread

查看:103
本文介绍了调用锁线程中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么在此代码中调用锁不执行正确
在及时执行执行调用时,可以执行当前线程(启动)的阻塞和调用列表.

I do not understand why in this code Invoke in lock Do not Execute correcty
while In time ٍExecute Invoke ,Current thread(Start)Blocked and invocation list can excecuted .

public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       delegate string t();
       t gh;

       delegate void s();
       s kd;

       void startmethod()
       {
           lock (this)
           {
               kd = delegate()
               {
                   label1.Text = "Seccssefully";
               };

              // Invoke(kd);    //if use this place program Hang!!!Why??
           }

           Invoke(kd);        //if use this place excute correctly
       }

       string ui()
       {
           label1.Text = "Successfully";
           return "Mehdi";
       }

       void di()
       {
           label1.Text = "Successfully";
       }
       IAsyncResult y;

       void Secondthreadprocces()
       {
          MessageBox.Show("Second thread Procces ,excecute successfully");
       }


       private void button1_Click(object sender, EventArgs e)
       {
           Thread.CurrentThread.Priority = ThreadPriority.Lowest;
           ThreadStart j = new ThreadStart(startmethod);
           j += new ThreadStart(Secondthreadprocces);
           Thread o = new Thread(j);
           o.Priority = ThreadPriority.Highest;

           o.Start();
           Thread.Sleep(3);
           lock (this)
           {
               MessageBox.Show("End Of Process.");
           }
       }
   }

推荐答案

不是很可能解决您的问题,但是我在您的最后几个问题中注意到您使用了lock(this).这是不可取的.请参阅此注释部分. > [ ^ ] MSDN页面,详细解释了原因.
Not that it is likely to resolve your problem, but I have noticed in your last couple of questions that you use lock(this). This is not advisable. See the Remarks section on this[^] MSDN page for a full explanation of the reasons.


我在对问题#current − 2的回答中提供的解释也涵盖了这一点:2:

在结合锁的线程中使用Invoke [ ^ ].您接受了此答案!

现在,好像您阅读了我对死锁场景的解释,并且有意地,根据我的确切示例,所有细节都像您想要陷入此死锁一样重现了它.解释是相对复杂的,需要一些技巧才能复制它,而您做到了.恭喜!

现在,如果是这样,让我再向您解释一件事.僵局是坏事,不是好事!

我只是在开玩笑.请执行以下操作:从您的代码中删除此锁.请勿将Invoke移至锁之外,请完全移开锁;你不需要它.为什么没有在上面提到的答案中进行解释.
是的,当您在不同情况下使用锁时,请考虑亨利关于不使用lock(this)的建议,请参阅我的评论以解释原因.
-SA
This is also covered by my explanation I provided in my Answer to your question #current − 2:

Use of Invoke in thread combined with lock[^]. You accepted this Answer!

Now, it looks like if you read my explanation of the deadlock scenario, and intentionally, in all detail according to my exact example reproduced it as if you wanted to get in this deadlock. The explanation is relatively complex, it would take some skills to reproduce it, and you did it. Congratulations!

Now, if that is the case, let me explain you one more thing. The deadlock is bad thing, not good!

I''m just kidding. Do the following: remove this lock from your code. Do not move Invoke outside the lock, remove the lock completely; you don''t need it. The explanation why not is done in my Answer I referenced above.
And yes, when you use the lock in different situation, please take into account Henry''s advice about not using lock(this), see my comment explaining why.

—SA


This[^] might help you.

You are supposed to use BeginInvoke in that case.


这篇关于调用锁线程中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆