OpenFileDialog causes "The application called an interface that was marshalled for a different thread"

1

I have the following code in my C# WinForms application:

private void AddFile_Click(object sender, EventArgs e)
{
    OpenFileDialog openFile = new OpenFileDialog();
    openFile.RestoreDirectory = false;
    openFile.Filter = "PCAP files (*.pcap)|*.pcap";
    openFile.RestoreDirectory = true;
    openFile.Multiselect = true;
    openFile.FilterIndex = lastUsedFileType;

    if (openFile.ShowDialog() == DialogResult.OK)
    {
        lastUsedFileType = openFile.FilterIndex;
        foreach (string file in openFile.FileNames)
        {
            inputListBox.Items.Add(file);
            inputListBox.HorizontalScrollbar = true;
        }
    }
}

When it calls the function ShowDialog, i get the following exception:

First-chance exception at 0x00007fff77f48b9c in DataVerifierGUI.exe: 0x8001010E: The application called an interface that was marshalled for a different thread.

If there is a handler for this exception, the program may be safely continued.

The call stack doesn't exactly help either as it seems to be coming from the combase.dll.

Does anyone know what I might be doing wrong here?

EDIT The full Form1.cs code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataVerifierLib;

namespace DataVerifierGUI
{
    public partial class Form1 : Form
    {
        PCAPParserWrapper pcapParserWrapper = new PCAPParserWrapper();
        public static List<string> pcapSelectionList;
        public static int lastUsedFileType;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String someString = inputListBox.Items[0].ToString();
            for (int i = 1; i < inputListBox.Items.Count; i++ )
            {
                someString = someString + ",";
                someString = someString + inputListBox.Items[i];
                //pcapSelectionList.Add(item);
            }
            pcapParserWrapper.ParseWrapper(someString);
        }

        private void AddFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.RestoreDirectory = false;
            openFile.Filter = "PCAP files (*.pcap)|*.pcap";
            openFile.RestoreDirectory = true;
            openFile.Multiselect = true;
            openFile.FilterIndex = lastUsedFileType;

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                lastUsedFileType = openFile.FilterIndex;
                foreach (string file in openFile.FileNames)
                {
                    inputListBox.Items.Add(file);
                    inputListBox.HorizontalScrollbar = true;
                }
            }
        }

        private void RemoveFile_Click(object sender, EventArgs e)
        {
            if (inputListBox.Items.Count > 0)
            {
                if (inputListBox.SelectedItems.Count > 0)
                {
                    inputListBox.Items.RemoveAt(inputListBox.SelectedIndex);
                }
                else
                {
                    MessageBox.Show("Select a file to remove");
                }
            }
            else
            {
                MessageBox.Show("There are no items to remove");
            }
        }

        private void ClearFiles_Click(object sender, EventArgs e)
        {
            if (inputListBox.Items.Count > 0)
            {
                inputListBox.Items.Clear();
            }
            else
                MessageBox.Show("There are no items to clear");
        }
    }
}
c#
winforms
asked on Stack Overflow Aug 4, 2015 by Suzan Aydın • edited Aug 4, 2015 by Suzan Aydın

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0