Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the <a href="http://dotnetperls.com/backgroundworker-introduction" rel="nofollow noreferrer">BackgroundWorker</a>, which is easy to use.</p> <p>Also, I'm not sure it's completely safe (I could be wrong, though) to show the SaveFileDialog in the new thread. My recommendation would be a flow as such:</p> <ol> <li>Show SaveFileDialog on main thread.</li> <li>Pass file name to a method, which is then called asynchronously.</li> </ol> <p>Here's an example implementation, without the use of <code>BackgroundWorker</code>:</p> <pre><code>private void button1_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.InitialDirectory = "C:\\"; sfd.Filter = "All files (*.*)|*.*"; sfd.FilterIndex = 1; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { // Invoke the SaveFile method on a new thread. Action&lt;string&gt; invoker = new Action&lt;string&gt;(SaveFile); invoker.BeginInvoke(sfd.FileName, OnSaveFileCompleted, invoker); } } protected void SaveFile(string fileName) { // save file here (occurs on non-UI thread) } protected void OnSaveFileCompleted(IAsyncResult result) { Action&lt;string&gt; invoker = (Action&lt;string&gt;) result.AsyncState; invoker.EndInvoke(result); // perform other actions after the file has been saved (also occurs on non-UI thread) } </code></pre> <p>Note that all actions performed on non-UI threads, must only affect non-UI elements. If you want to modify UI elements, you should marshal the call back to the UI thread, by using <code>Control.Invoke</code> (e.g. <code>this.Invoke</code>). See <a href="https://stackoverflow.com/questions/1194918/why-am-i-getting-a-cross-thread-operation-not-valid-exception-in-the-callback-m/1194997#1194997">this post</a> for further details.</p>
 

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