C# Can';t访问listbox.InvokeRequired
嘿 我无法访问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
使用系统;
使用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
{
}
}
}
}