后台工作程序未正常工作。

3

我有一个表单调用另一个表单中的方法,但是另一个表单中的方法无法正常工作。
Form2 调用 main 方法:

private void button1_Click(object sender, EventArgs e)
{
    main ma = new main();
    ma.AddType(txtName.Text,txtURL.Text,12);
    this.Close();
}

主函数:(向 XML 中添加一行并从 XML 重新加载数据表格)

public void AddType(string name, string url, int interval)
{

    string path = Application.StartupPath + @"\sites.xml";
    //create new instance of XmlDocument
    XmlDocument doc = new XmlDocument();
    //load from file
    doc.Load(path);
    //create node and add value
    XmlNode node = doc.CreateNode(XmlNodeType.Element, "site", null);
    node.InnerXml = "<Name>"+name+"</Name><URL>"+url+"</URL><Status></Status><Response-Time></Response-Time><Last-Checked></Last-Checked>";
    //add to elements collection
    doc.DocumentElement.AppendChild(node);
    //save back
    doc.Save(path);
    bwLoadXML.RunWorkerAsync();
}
由于某种原因,未能在数据网格中显示新的xml。 编辑,这是backgroundWorker:
/////////////////////////////////
        ////Populate Grid from XML
        /////////////////////////////////
        private void bwLoadXML_DoWork(object sender, DoWorkEventArgs e)
        {
            gridPopulate();
        }
        private void gridPopulate()
        {

            DataSet data = new DataSet(); string p = System.IO.Path.Combine(Application.StartupPath, "sites.xml");
            data.ReadXml(p);
            if (this.dataGrid.InvokeRequired)
            {
                this.dataGrid.Invoke(new MethodInvoker(delegate
                {
                    this.dataGrid.DataSource = data;
                    this.dataGrid.DataMember = "site";
                }));
            }
            else
            {
                this.dataGrid.DataSource = data;
                this.dataGrid.DataMember = "site";
            }
            int i = 0;
            foreach (DataGridViewColumn column in this.dataGrid.Columns)
            {
                if (i != 0)
                {
                    if (column.Name == "Name" || column.Name == "Status" || column.Name == "URL" || column.Name == "Response-Time" || column.Name == "Last-Checked")
                    {
                        //column.AutoSizeMode
                        column.Visible = true;
                        //column.Width = (int)(dataGrid.Width * .2) + (column.Name.Length / 2)-9;
                        /*if (column.Name == "URL")
                        {
                            ColumnHeader ch = new ColumnHeader();
                            //ch.
                        }*/
                    }
                    else
                    {
                        column.Visible = false;
                        //dataGrid.Columns[i+1].CellType = new DataGridViewButtonColumn();
                        //dataGrid.Columns[i+1].HeaderCell.
                    }
                }
                i++;
            }
            if (this.dataGrid.InvokeRequired)
            {
                this.dataGrid.Invoke(new MethodInvoker(delegate
                {
                    // If column 3 is the checkbox column, we sit it's resize mode to none:
                    dataGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                    // Then we set the width:
                    dataGrid.Columns[0].Width = 25;
                    dataGrid.Columns[0].DefaultCellStyle.Padding = System.Windows.Forms.Padding.Empty;
                    // If column 3 is the checkbox column, we sit it's resize mode to none:
                    dataGrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    // Finally we set the rest of the grid to fill or what ever resizing you need:
                    dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                }));
            }
            else
            {
                // If column 3 is the checkbox column, we sit it's resize mode to none:
                dataGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                // Then we set the width:
                dataGrid.Columns[0].Width = 25;
                dataGrid.Columns[0].DefaultCellStyle.Padding = System.Windows.Forms.Padding.Empty;
                // If column 3 is the checkbox column, we sit it's resize mode to none:
                dataGrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                // Finally we set the rest of the grid to fill or what ever resizing you need:
                dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
        }

1
请不要在你的问题标题前加上 "C# |"。我们在 [so] 上使用标签来实现这个目的。 - John Saunders
1
BackgroundWorker 的代码在哪里?另外,哪个窗体包含 DataGrid?我们需要更多信息。 - Michael Minton
@MichaelMinton 好的,我已经添加了BackgroundWorker代码,主窗体上有DataGridView。 - funerr
1个回答

2

你的问题源于按钮单击方法创建了一个新的、独立的主窗体,并且没有与你期望的那个进行交互。由于在按钮单击完成后,你的新窗体不再被引用,因此后台工作程序可能永远没有机会启动。你需要设置并保留对主窗体的引用才能使用它。

public class Form2 : ... {
  main ma;

  public Form2(main ma) {
    this.ma = ma;
  }

  private void Button1_Click(object sender, EventArgs e) {
    this.ma.AddType(txtName.Text, txtUrl.Text, 12);
    this.Close();
  }
}

从主表单中创建第二个表单并显示它时,您需要传递它所期望的表单:

void DoingSomething() {
  Form2 form = new Form2(this); // <-- this is where you pass in main
  form.ShowDialog();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接