I am creating an application to read table from database and dump them into file.The user ask to dump one file and right after as a second task then a third and so one. I am looking to display on the GUI when the task finnish or if they fail.I am having hard time to communicate between my task and the GUI. Here is the code of my gui :
namespace ClientCsvDumper
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
async void go_Click(object sender, RoutedEventArgs e)
{
String database_name;
String schema_name;
String output_file_name;
database_name = this.database_name.SelectedItem.ToString();
output_file_name = this.output_file_name.Text;
SqlConnection conn = new SqlConnection("Data Source="+this.server_name.Text+";Initial Catalog="+"MASTER"+";Integrated Security=True");
if (this.query_button.IsChecked.Value) {
String query_text;
query_text = this.query_textbox.Text;
thread_number++;
await Task.Run(() => {
UpdateWindows("task " + thread_number.ToString() + " started");
AsyncDumpQueryToFile(conn, database_name, query_text, output_file_name);
});
}
else {
schema_name = this.schema_name.SelectedItem.ToString();
String table_name;
table_name = this.table_name.SelectedItem.ToString();
thread_number++;
await Task.Run(() => {UpdateWindows("task " + thread_number.ToString() + " started");
AsyncDumpTableToFile(conn, database_name, schema_name, table_name, output_file_name);
});
}
}
async Task AsyncDumpTableToFile( SqlConnection connection, string database_name, string schema_name, string tableName, string destinationFile) {
// gui.UpdateWindows("Work Started");
TableDumper ts = new TableDumper();
ts.DumpTableToFile(connection, database_name, schema_name, tableName, destinationFile);
// gui.UpdateWindows("Done");
}
async Task AsyncDumpQueryToFile( SqlConnection connection, string database_name, string query, string destinationFile)
{
//gui.UpdateWindows("Work Started");
TableDumper ts = new TableDumper();
ts.DumpQueryToFile( connection, database_name, query, destinationFile);
// gui.UpdateWindows("Done");
}
void UpdateWindows(String text) {
Dispatcher.Invoke(() => { status.Text += text; });
}
First i tried (using code from the web ) to pass the gui 0bject to the fonction AsyncDumpTableToFile (so i can modify thing on the gui but it was crashing saying the main Thread used and own it.
How could i print on the main GUI informatoin about the runnin task ( Started,running,failed ( exception message), SUccess ) ?
Also any good reading for me to understand a bit more this kind of stuff ?
All the best
Vincent