Note that there are some explanatory texts on larger screens.

plurals
  1. POShowing Dialog After Long Process
    primarykey
    data
    text
    <p>In the following code, I have a long running process called <code>GetExcelData</code>. When it's complete, I want to show a dialog to save it's contents into a TXT file.</p> <p>The problem is, when debugging, I get the following error:</p> <blockquote> <p>Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.</p> </blockquote> <p>This is my code. The error occurs on the line that reads <code>saveFileDialog1.ShowDialog();</code></p> <pre><code>FileInfo existingFile = new FileInfo("C:\\MyExcelFile.xlsx"); ConsoleApplication2.Program.ExcelData data = ConsoleApplication2.Program.GetExcelData(existingFile, _worker); var json = new JavaScriptSerializer().Serialize(data); SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName != "") { File.WriteAllText(saveFileDialog1.FileName, json); } </code></pre> <p>I have tried adding the <code>[STAThread]</code> attribute to the method I am calling this from but it didn't seem to work.</p> <p><strong>Please let me provide more code for additional clarity as to what I am trying to do:</strong></p> <p>The following exists in a WPF project which references my Console project:</p> <pre><code>private BackgroundWorker _backgroundWorker = new BackgroundWorker(); public MainWindow() { InitializeComponent(); // Set up the BackgroundWorker. this._backgroundWorker.WorkerReportsProgress = true; this._backgroundWorker.WorkerSupportsCancellation = true; this._backgroundWorker.DoWork += new DoWorkEventHandler(bw_DoWork); this._backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); } private void Button_Click(object sender, RoutedEventArgs e) { if (this._backgroundWorker.IsBusy == false) { this._backgroundWorker.RunWorkerAsync(); } e.Handled = true; } void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { // Set the Value porperty when porgress changed. this.progressBar1.Value = (double)e.ProgressPercentage; } void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker _worker = sender as BackgroundWorker; if (_worker != null) { FileInfo existingFile = new FileInfo("C:\\MyExcelFile.xlsx"); ConsoleApplication2.Program.ExcelData data = ConsoleApplication2.Program.GetExcelData(existingFile, _worker); var json = new JavaScriptSerializer().Serialize(data); SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName != "") { File.WriteAllText(saveFileDialog1.FileName, json); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload