ThreadPool中,遇到了“xxxx”的重载均与委托“System.Threading.WaitCallback”不匹配.

说明:将方法排入队列以便执行,WaitCallback,表示要执行的方法。如果将方法成功排入队列,则为 true;否则为 false

示例:addtest方法需要比较长的时间来响应,因此在button1_Click被点击以后,程序会失去响应,使用ThreadPool.QueueUserWorkItem 后,页面会继续响应其他时间,等addtest执行结束后,响应结果。

 

<Window x:Class="WpfAppThread.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Height="23" HorizontalAlignment="Left" Margin="9,23,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">ButtonOne</Button>
        <Button Height="23" Margin="102,23,101,0" Name="button2" VerticalAlignment="Top" Click="button2_Click">ButtonTwo</Button>
    </Grid>
</Window>

 

 

 

private void button1_Click(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(addtest),"Testaa"); 
            //addtest();
        }

        private void addtest(object aa)
        {
            long result=0;
            for (int i = 0; i < 1000000000; i++)
            {
                result += i;
            }
            MessageBox.Show(result.ToString()+aa.ToString());
            
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("two");
        }

在使用ThreadPool中,遇到了“xxxx”的重载均与委托“System.Threading.WaitCallback”不匹配.

在网上找一下原因

1、请参见 WaitCallback 委托的声明:
public delegate void WaitCallback(object state);
由此可见,需要一个无返回值的,并且有一个object型参数的方法才可以匹配。
就是这个原因导致的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新时代丘鸣山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值