Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t访问listbox.InvokeRequired_C#_.net_Wpf - Fatal编程技术网

C# Can';t访问listbox.InvokeRequired

C# Can';t访问listbox.InvokeRequired,c#,.net,wpf,C#,.net,Wpf,嘿 我无法访问listbox.InvokeRequired 代码: 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Data; 使用System.Windows.Documents; 使用System.Windows.Input; 使用System.Windows.Media; 使用System.Wind

我无法访问listbox.InvokeRequired

代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统线程;
名称空间素数
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
私有委托void AddListItem(字符串项);
螺纹t;
布尔中断;
公共主窗口()
{
初始化组件();
}
专用无效BTS_单击(对象发送方,路由目标)
{
如果(t==null)
{
t=新螺纹(此。计算);
t、 Start();
btss.Content=“停止”;
}
其他的
{
t、 中断();
}
}
私有无效计算()
{
int currval=2;
int-devide=2;
而(!中断)
{
对于(int i=2;i

请帮助!

InvokeRequired
是一个winforms的东西。对于WPF,您必须使用
Dispatcher
。请查看所有说明。

InvokeRequired
是一个winforms的东西。对于WPF,您必须使用
Dispatcher
。请查看所有说明

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace PrimeNumbers
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private delegate void AddListItem(string item);
        Thread t;
        bool interrupt;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btss_Click(object sender, RoutedEventArgs e)
        {
            if (t == null)
            {
                t = new Thread(this.calculate);
                t.Start();
                btss.Content = "Stop";
            }
            else
            {
                t.Interrupt();
            }

        }

        private void calculate()
        {
            int currval = 2;
            int devide = 2;
            while (!interrupt)
            {
                for (int i = 2; i < currval/2; i++)
                {
                    if (2 % i != 0)
                    {
                        if (
                        lbPrimes.Items.Add(currval.ToString());
                    }
                }
                currval++;
            }
        }
        private void AddListBoxItem(string item)
        {
            if (this.lbPrimes.InvokeRequired) //Error: Expression to evaluate
            {

            }
        }

    }
}