线程中的BlockInput不起作用

时间:2013-05-11 15:20:52

标签: c#

[System.Runtime.InteropServices.DllImportAttribute("User32.dll", EntryPoint = "BlockInput")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool BlockInput([System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fBlockIt);

如果我在主线程中运行块输入,那么它会毫无问题地执行。 如果我把它放在一个线程然后执行它,然后它返回true但不阻止输入。 更有趣的是,如果我从线程内部调试代码(而不是从开头)也是有效的。

知道为什么吗?每个线程都不应该拥有主线程的特权。顺便说一下,我在行政特权下运行它。

2 个答案:

答案 0 :(得分:0)

尝试将清单文件添加到项目中,然后使用管理员权限运行您的应用程序。在清单文件中复制此行。

        <operation>
            <search><![CDATA[
            $data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
            ]]></search>
            <add position="after"><![CDATA[
            $data['you_save'] = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')) - $this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
            $data['you_save_or'] = round((($product_info['price'] - $product_info['special']) / $product_info['price'] * 100));
            $data['text_you_discount'] = round($product_info['price'] - ($product_info['price']* 0.15));
            ]]></add>
        </operation>
        <operation>
            <search><![CDATA[
            foreach ($discounts as $discount) {
            ]]></search>
            <add position="after"><![CDATA[
            $you_save = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')) - $this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
            if ($product_info['price'] == 0) { $you_save_or = 0; } 
            else if { $you_save_or = round((($product_info['price'] - $discount['price']) / $product_info['price'] * 100)); } else
            {  $text_you_discount = round($product_info['price'] - ($product_info['price']* 0.15));}
            ]]></add>
        </operation>

然后尝试再次运行,您的BlockInput必须正确运行。

答案 1 :(得分:-1)

我认为这应该有效:

BeginInvoke((MethodInvoker)delegate{ BlockInput(true); });