VolatileWrite布尔值

ser*_*hio 1 .net multithreading

问题出在哪儿?

bool stillSingleClick = false;

// does not compime
System.Threading.Thread.VolatileWrite(ref stillSingleClick, true); 
Run Code Online (Sandbox Code Playgroud)

'System.Threading.Thread.VolatileWrite(ref object,object)'的最佳重载方法匹配有一些无效的参数

为什么我要问..我试着通过以下方式区分我的doubleClick:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

不工作stillSingleClick永远是真的....

cdh*_*wie 5

VolatileWritebool变量进行操作没有过载.我和Interlocked班上有同样的问题.我的解决方法是使用一个int代替,使用0表示false和1表示true.这是一个讨厌的黑客,但它的工作原理.