C# 后台工作程序完成后,DataGridView窗体显示为空

C# 后台工作程序完成后,DataGridView窗体显示为空,c#,winforms,datagridview,backgroundworker,C#,Winforms,Datagridview,Backgroundworker,我需要向你寻求帮助,因为我已经为此挣扎太久了。已经看过很多教程,但还没弄明白 在该项目的第一阶段,我开发了一个控制台程序,可以向web服务器发出请求,处理数据并更新MS Access数据库。现在我的问题出现了,在完成所有这些处理之后,我需要在winform中显示结果,以便客户端查看,而且在打开应用程序时,该过程应该一次又一次地进行 到目前为止,我所做的是创建一个winform,它作为后台工作程序运行控制台程序,结果应该在程序继续运行时显示和更新。为简单起见,我将所有繁重的处理替换为一个填充哈希表

我需要向你寻求帮助,因为我已经为此挣扎太久了。已经看过很多教程,但还没弄明白

在该项目的第一阶段,我开发了一个控制台程序,可以向web服务器发出请求,处理数据并更新MS Access数据库。现在我的问题出现了,在完成所有这些处理之后,我需要在winform中显示结果,以便客户端查看,而且在打开应用程序时,该过程应该一次又一次地进行

到目前为止,我所做的是创建一个winform,它作为后台工作程序运行控制台程序,结果应该在程序继续运行时显示和更新。为简单起见,我将所有繁重的处理替换为一个填充哈希表列表的循环,该列表将返回到winform以显示:

    namespace TheNameSpace
{
    class aldeloUpdater
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new GuiForm());
        }

        public List<Hashtable> oldMain()
        {
            List<Hashtable> products = new List<Hashtable>();

            try
            {
                Hashtable product1 = new Hashtable();
                Hashtable product2 = new Hashtable();
                Hashtable product3 = new Hashtable();

                product1.Add("productName", "Empanada de Pollo");
                product1.Add("userName", "Fabio Roman");
                product1.Add("dateAndTime", "2016-08-11 15:50:52");
                product1.Add("domiciliosOrderId", "1932211-20160811155052");
                products.Add(product1);

                product2.Add("productName", "Empanada de Carne");
                ...
                products.Add(product2);

                product3.Add("productName", "Empanada Mixta");
                ...
                products.Add(product3);

                Console.WriteLine("A message for debugging.");
                Console.ReadLine();

                return products;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception details: " + e.ToString());
                Console.ReadLine();
                return products;
            }
        }

        // More Methods and classes
}
名称空间
{
类aldeloUpdater
{
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新的GuiForm());
}
公共列表oldMain()
{
列表产品=新列表();
尝试
{
Hashtable product1=新的Hashtable();
Hashtable product2=新的Hashtable();
Hashtable product3=新的Hashtable();
产品1.添加(“产品名称”、“Empanada de Pollo”);
产品1.添加(“用户名”、“法比奥罗马”);
产品1.添加(“日期和时间”,“2016-08-11 15:50:52”);
产品1.添加(“户籍医嘱ID”,“1932211-2016081155052”);
产品。添加(产品1);
产品2.添加(“产品名称”、“卡恩百货”);
...
产品。添加(产品2);
产品3.添加(“产品名称”、“Empanada Mixta”);
...
产品。添加(产品3);
WriteLine(“用于调试的消息”);
Console.ReadLine();
退货产品;
}
捕获(例外e)
{
WriteLine(“异常详细信息:+e.ToString());
Console.ReadLine();
退货产品;
}
}
//更多方法和类
}
至于winform,我有这样一个:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace TheNameSpace
{
    public partial class GuiForm : Form
    {
        public List<Hashtable> dataGridViewProducts;

        public GuiForm()
        {
            InitializeComponent();
            InitializeBackgroundWorker();
            backgroundWorker.RunWorkerAsync(); // Initialize the whole process.
        }

        // Set up the BackgroundWorker object by 
        // attaching event handlers. 
        private void InitializeBackgroundWorker()
        {
            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            backgroundWorker.RunWorkerCompleted +=
                new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerComplet);
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void GuiForm_Load(object sender, EventArgs e)
        {
            int index = 0;
            if (dataGridViewProducts != null && !dataGridViewProducts.Any())
            {
                foreach (Hashtable product in dataGridViewProducts)
                {
                    dataGridView1.ReadOnly = false;
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[index].Cells[0].Value = product["productName"];
                    dataGridView1.Rows[index].Cells[1].Value = product["userName"];
                    dataGridView1.Rows[index].Cells[2].Value = product["dateAndTime"];
                    dataGridView1.Rows[index].Cells[3].Value = product["domiciliosOrderId"];
                    index++;
                }
                return;
            } 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Logic for a delete-button
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            aldeloUpdater aldeloThread = new aldeloUpdater();
            this.dataGridViewProducts = aldeloThread.oldMain();

            //this.GuiForm_Load(); // ¿Do I need to make this call? ¿How to?
            this.Show();
        }

        private void backgroundWorker_RunWorkerComplet(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                MessageBox.Show(e.Result.ToString());
            }
        }
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用System.Windows.Forms;
名称空间
{
公共部分类GuiForm:Form
{
公开列出dataGridViewProducts;
公共表格()
{
初始化组件();
初始化BackgroundWorker();
backgroundWorker.RunWorkerAsync();//初始化整个过程。
}
//通过设置BackgroundWorker对象
//附加事件处理程序。
private void InitializeBackgroundWorker()
{
backgroundWorker.DoWork+=新的DoworkerVenthandler(backgroundWorker_DoWork);
backgroundWorker.RunWorker已完成+=
新的RunWorkerCompletedEventHandler(backgroundWorker\u RunWorkerComplet);
}
私有void dataGridView1\u CellContentClick(对象发送者,DataGridViewCellEventArgs e)
{
}
私有void表单加载(对象发送方、事件参数e)
{
int指数=0;
if(dataGridViewProducts!=null&&!dataGridViewProducts.Any())
{
foreach(dataGridViewProducts中的哈希表产品)
{
dataGridView1.ReadOnly=false;
dataGridView1.Rows.Add();
dataGridView1.Rows[index]。单元格[0]。值=产品[“productName”];
dataGridView1.Rows[index]。单元格[1]。值=产品[“用户名”];
dataGridView1.Rows[index]。单元格[2]。值=产品[“dateAndTime”];
dataGridView1.Rows[index].Cells[3].Value=product[“domiciliosOrderId”];
索引++;
}
返回;
} 
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//删除按钮的逻辑
}
私有void backgroundWorker_DoWork(对象发送方,DoWorkEventArgs e)
{
aldeloUpdater aldeloThread=新的aldeloUpdater();
this.dataGridViewProducts=aldeloThread.oldMain();
//this.GuiForm_Load();/?我需要打这个电话吗?如何?
this.Show();
}
私有void backgroundWorker\u RunWorkerComplet(对象发送方,RunWorkerCompletedEventArgs e)
{
如果(例如错误!=null)
{
MessageBox.Show(例如Error.Message);
}
其他的
{
Show(例如Result.ToString());
}
}
}
}
我一直在期待这个结果:

但我得到的是:

我知道我做错了什么,但我不知道这是什么,也不知道如何做好,我是PHP后端开发人员,这是我的第一个C#程序。请帮助。

如果没有一个好的解决方案,就不可能确定什么是最好的解决方案。但是根据您上面发布的代码,我可以提供一些建议:

  • 似乎不需要将
    oldMain()
    作为实例方法。您可以将其设置为
    public static List oldMain()
    ,然后将其称为
    aldeloUpdater.oldMain()
    。更好的是,为该方法创建一个单独的类,而不是将它放在主
    程序中
  • 你写了一条评论:“我需要打这个电话吗?怎么打?”。我会回答为“是的,你需要打这个电话。”因为方法h
    private void backgroundWorker_RunWorkerComplet(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show(e.Error.Message);
        }
        else
        {
            MessageBox.Show(e.Result.ToString());
            GuiForm_Load(sender, e);
        }
    }