C# 在新线程上运行Linq查询

C# 在新线程上运行Linq查询,c#,multithreading,linq,C#,Multithreading,Linq,在遵循MSDN指南创建新线程以更新UI控件之后,我遇到了一个奇怪的错误。我在load方法中运行了相同的查询,它运行得很好,现在在新线程中运行,我得到了正确数量的结果,但不是得到字段的名称,而是在组合框中写入了16次数据集。有人能帮我吗 private void Form1_Load(object sender, EventArgs e) { recipeListComboBox.Items.Clear(); Thread QueryThread = new

在遵循MSDN指南创建新线程以更新UI控件之后,我遇到了一个奇怪的错误。我在load方法中运行了相同的查询,它运行得很好,现在在新线程中运行,我得到了正确数量的结果,但不是得到字段的名称,而是在组合框中写入了16次数据集。有人能帮我吗

private void Form1_Load(object sender, EventArgs e)
    {
        recipeListComboBox.Items.Clear();
        Thread QueryThread = new Thread(new ThreadStart(updateRecipeList));
        QueryThread.Start();
    }

 private void updateRecipeList()
    {

        IEnumerable<string> list = recipeList.getList();

        foreach (string a in list)
            UpdateRecipeComboBox(a);
    }

 private void UpdateRecipeComboBox(string text)
    {
        if (this.recipeListComboBox.InvokeRequired)
        {
            UpdateRecipeComboBoxCallBack d = new UpdateRecipeComboBoxCallBack(UpdateRecipeComboBox);
            Invoke(d, new object[] { text });
        }

        else
        {
            this.recipeListComboBox.Items.Add(Text);
        }
    }

    delegate void UpdateRecipeComboBoxCallBack(string text);
private void Form1\u加载(对象发送方,事件参数e)
{
recipeListComboBox.Items.Clear();
线程QueryThread=新线程(newthreadstart(updateRecipeList));
QueryThread.Start();
}
私有void updateRecipeList()
{
IEnumerable list=recipeList.getList();
foreach(列表中的字符串a)
更新密码箱(a);
}
私有void UpdateRecipeComboBox(字符串文本)
{
if(this.recipeListComboBox.InvokeRequired)
{
UpdateRecipeComboxCallback d=新的UpdateRecipeComboxCallback(UpdateRecipeComboxCallback);
调用(d,新对象[]{text});
}
其他的
{
this.recipeListComboBox.Items.Add(文本);
}
}
委托void updateRecipeComboxCallback(字符串文本);
在我把它放到新的线程上之前,它看起来是这样的:

private void Form1_Load(object sender, EventArgs e)
{
     recipeListComboBox.Items.Clear();
     IEnumerable<string> list = recipeList.getList();

     foreach (string a in list)
          recipeComboBox.Items.Add(a);
private void Form1\u加载(对象发送方,事件参数e)
{
recipeListComboBox.Items.Clear();
IEnumerable list=recipeList.getList();
foreach(列表中的字符串a)
recipeComboBox.Items.Add(a);
这将在数据库中重新运行16个不同的Recipe列表,现在我只打印了16次数据集

谢谢你的帮助

Craig

如果你在一个文件上再次打印“数据集”,我猜你在某处使用了一个
数据集
对象来代替字符串参数,它会自动调用
对象。ToString()
,返回类的名称

不确定这是否是您的问题,但您也有套管不匹配的问题:

private void UpdateRecipeComboBox(string text)
{
    if (this.recipeListComboBox.InvokeRequired)
    {
        UpdateRecipeComboBoxCallBack d = new UpdateRecipeComboBoxCallBack(UpdateRecipeComboBox);
        Invoke(d, new object[] { text });
    }

    else
    {
        this.recipeListComboBox.Items.Add(Text);  // <--- should be text???
    }
}
private void UpdateRecipeComboBox(字符串文本)
{
if(this.recipeListComboBox.InvokeRequired)
{
UpdateRecipeComboxCallback d=新的UpdateRecipeComboxCallback(UpdateRecipeComboxCallback);
调用(d,新对象[]{text});
}
其他的
{

this.recipeListComboBox.Items.Add(Text);//此处的文本大小写是否与代码中的相同:
this.recipeListComboBox.Items.Add(Text)
?请回答这个问题。你给我弄到了,谢谢!!我想我的眼睛可能累了,对不起。是的,我正在使用一个dataset对象,我开始想我可能需要在我调用的类中运行新线程,但MSDN教程以这种方式运行它,这就是我尝试它的原因。我是线程新手,一直在玩w我开始相信,随着数据库的增长,我需要在不同的线程上运行查询以保持UI的活力。