繁体   English   中英

没有Thread.Sleep(),多个Invoke / BeginInvoke调用无法完成

[英]Multiple Invoke / BeginInvoke calls not finishing without Thread.Sleep()

在下面的代码中,在后台工作线程中运行:

while (StaticVideoList.DecodedFramesList.Count > 0)
{
    if (StaticVideoList.DecodedFramesList.ContainsKey(a))
    {
        byte[] output;

        if(StaticVideoList.DecodedFramesList.TryRemove(a, out output))
        {

            MemoryStream ms = new MemoryStream(output);

            Image img = Image.FromStream(ms);

            a++;

            this.Invoke((Action)delegate
            {
                textEdit7.Text = "Num Frame: " + a.ToString() + " / " + (1000m / ((decimal)sw.ElapsedMilliseconds / (decimal)a)).ToString();
                pictureEdit1.Image = img;
            });

            // Works if I do this.. 10 is the minium. 
            //System.Threading.Thread.Sleep(10);
        }
    }

}

我的问题是,除非在循环中添加Thread.Sleep(10)否则不会进行所有Invoke调用。 整个循环被调用,但是仅执行20%的调用(它在随后停止时获得20%,而不是间歇性的结果)

如果我逐步调试,它也可以正常工作。

认为问题可能出在后台工作线程完成时,它取消了由此产生的任何调用。 但是,将Thread.Sleep(10000)挂起后台线程的时间更长并没有做任何事情。

调用调用的速度有限制吗?

另外BeginInvoke具有相同的“问题”

向我发出的邀请似乎是一条红鲱鱼。 循环似乎“停止”但在您休眠线程时起作用的事实向我暗示您有竞争条件。

我的建议是将循环恢复到最低限度并重建它,直到再次遇到问题为止-这应该有助于您确定问题出在哪里。

暂无
暂无

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

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