Avoiding the window (WPF) to freeze while using TPL
问题 I am building a WPF which has a button that execute a sql query in sql server (the query could take a long time to run). I want to use TPL for doing that. This code: var result = Task.Factory.StartNew(() => { command.ExecuteNonQuery(); }); gives this exception: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed. I guess this is due to the fact that the query runs on a different thread and is not aware of the open connection. I have 2 questions: