VolatileWrite布尔 [英] VolatileWrite booleans

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

问题描述

问题出在哪里?

 布尔stillSingleClick = FALSE;

//不compime
System.Threading.Thread.VolatileWrite(REF stillSingleClick,真正的);
 

  

最好的重载的方法匹配   System.Threading.Thread.VolatileWrite(REF   对象,对象)有一些无效的   参数

为什么我问。我试着区分我的DoubleClick以下列方式:

 挥发性布尔stillSingleClick = FALSE;

无效SegmentLine_MouseLeftButtonUp(对象发件人,System.Windows.Input.MouseButtonEventArgs E)
{
    SegmentLine线=发件人为SegmentLine;

    如果(e.ClickCount == 1)
    {
        stillSingleClick = TRUE;

        线程线程=新主题(
            新System.Threading.ThreadStart(
                代表()
                {
                    line.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        新的行动(
                            代表()
                            {
                                Thread.sleep代码(System.Windows.Forms.SystemInformation.DoubleClickTime * 2);
                                如果(stillSingleClick)//这里总是如此!
                                {
                                    SelectSegmentLine(线);
                                }
                                stillSingleClick = FALSE;
                            }
                    ));
                }
        ));
        thread.Start();
    }
    否则,如果(e.ClickCount == 2)
    {
        stillSingleClick = FALSE; //这里应该设置假!
        DisplaySegmentLineInfo(线);
    }
}
 

不工作 stillSingleClick 总是在真......

解决方案

有没有重载的 VolatileWrite 布尔领域,基本上是这样。

任何理由不只是使用volatile字段下手?或者你可以使用不同类型的字段,如cdhowie建议。

我个人感到紧张想写无锁code。它是如此容易出错。有没有使用更高级别的积木当前的设计其它方法吗?如果您使用的是.NET 4,新的任务并行库是非常有用的。

Where is the problem?

bool stillSingleClick = false;

// does not compime
System.Threading.Thread.VolatileWrite(ref stillSingleClick, true); 

The best overloaded method match for 'System.Threading.Thread.VolatileWrite(ref object, object)' has some invalid arguments

Why do I ask.. I try to differentiate my doubleClick in the following way:

volatile bool stillSingleClick = false;

void SegmentLine_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    SegmentLine line = sender as SegmentLine;

    if (e.ClickCount == 1)
    {
        stillSingleClick = true;

        Thread thread = new Thread(
            new System.Threading.ThreadStart(
                delegate()
                {
                    line.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                            delegate()
                            {
                                Thread.Sleep(System.Windows.Forms.SystemInformation.DoubleClickTime * 2);
                                if (stillSingleClick) // HERE ALWAYS TRUE!!!
                                {
                                    SelectSegmentLine(line);
                                }
                                stillSingleClick = false;
                            }
                    ));
                }
        ));
        thread.Start();
    }
    else if (e.ClickCount == 2)
    {
        stillSingleClick = false; // HERE SHOULD BE SET IN FALSE !!!
        DisplaySegmentLineInfo(line);
    }
}

Does not work stillSingleClick is always in True....

解决方案

There are no overloads for VolatileWrite for Boolean fields, basically.

Any reason not to just use a volatile field to start with? Or you could use a field of a different type, as cdhowie suggested.

Personally I get nervous trying to write lock-free code. It's so easy to get wrong. Are there any alternatives to your current design using higher-level building blocks? If you're using .NET 4, the new Tasks Parallel Library can be very useful.

这篇关于VolatileWrite布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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