交叉线程错误

时间:2015-01-02 16:00:03

标签: c# listbox

所以我有点困惑。我正在我的主程序的Form1中实例化一个名为Form2的对象。在Form1中,我一直在阅读和更改列表框控件的数据,没有交叉线程问题。但是,现在我在Form2中,每次尝试运行该程序时,这个问题似乎都会弹出。

 namespace WindowsFormsApplication1
{
    //from program.cs file
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1()); //form 2 is instantiated in Form1
    }
}
//In my Form2.Designer.CS file
partial class Form2
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.    </param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {

        // 
        // directories
        // 
        this.directories.FormattingEnabled = true;
        this.directories.Location = new System.Drawing.Point(323, 80);
        this.directories.Name = "directories";
        this.directories.Size = new System.Drawing.Size(241, 186);
        this.directories.TabIndex = 0;                     
        // 
        // backgroundWorker1
        // 
        this.backgroundWorker1.WorkerReportsProgress = true;
        this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
        this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
        this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
        // 
        // Form2
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(578, 578);
        this.Controls.Add(this.directories);
        this.Name = "Form2";
        this.Text = "Form2";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion



    public System.Windows.Forms.ListBox directories;
    private System.ComponentModel.BackgroundWorker backgroundWorker1;
}
//separated into my Form2.CS file
partial class Form2
{
    private void PopulateListBox(ListBox lsb, string directory) //populates a given list block.  takes the name of a list block and the directory to be seen.
    {
        List<string> fileList = new List<string>();
        DirectoryInfo dinfo = new DirectoryInfo(directory);
        FileInfo[] Files = dinfo.GetFiles("*.xml"); //only display xmls.  we don't care about other file types at this point.
        foreach (FileInfo file in Files)
        {
            string[] aFolder = file.Name.Split('.');
            fileList.Add(aFolder[0]); //adds items to the list block
        }
        fileList.Sort();
        foreach (string fileName in fileList)
        {
            lsb.Items.Add(fileName); //adds items to the list block
        }

    }
     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

        //heres where I have the error thrown
        DirectoryInfo dinfo = new DirectoryInfo(baseDirectory + directories.SelectedItem.ToString());
        FileInfo[] Files = dinfo.GetFiles("*.xml");
        XmlDocument doc = new XmlDocument();
        int currentInterval = 0;
        //More occurs after this point but I can't list the info.
    }

}
}

令人沮丧的是,我仍然可以将数据加载到列表框中没有问题。我遇到的唯一问题是当我试图访问我存储在列表框中的数据时。我将listbox.SelectionMode设置为多选。也尝试了单一选择。任何帮助将不胜感激。我不是试图以任何方式改变列表框的内容。只需访问我已存储的数据。

0 个答案:

没有答案