Eidenai
Eidenai

Reputation: 441

What to await when using open file dialog asynchronously?

So, I'm working on some demo code for a future project and the easiest way for me to do that is to use the open file dialog with a console. The problem is, I constantly get these errors:

ContextSwitchDeadlock was detected Message: The CLR has been unable to transition from COM context 0x134bfd8 to COM context 0x134c090 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

So the obvious answer is that I need the operations to be taking place on a different thread.

So I went from using this:

FileParserOne parser = new FileParserOne();
Thread thread = new Thread(parser.GetFile);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

to this:

Task.Factory.StartNew(parser.GetFile);

Problem now being that the project ends as soon as it starts, fires off that task because it's not awaiting anything. Why? Because I'm not sure what it is I should be awaiting within the get file method which opens the text file and separates the words.

internal async void GetFile()
        {
            Stream stream = null;
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = @"C:\";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((stream = dialog.OpenFile()) != null)
                    {
                        using (stream)
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                String temp = reader.ReadToEnd();
                                m_WordArray = temp.Replace
                                    ("\r", string.Empty).Replace("\n", " ")
                                .Replace("\"", string.Empty).Split(' ').ToArray<string>();
                                Console.Read();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
        }

Upvotes: 0

Views: 3910

Answers (1)

Chase
Chase

Reputation: 944

Your program is exiting immediately because the calling thread won't wait unless you tell it to do so, as shown in this example:

Task myTask = Task.Factory.StartNew(parser.GetFile);
myTask.Wait(); // wait for myTask to complete

Upvotes: 1

Related Questions