void Upload_Click(object sender, RoutedEventArgs e) { string message; if (ValidateArduinoSetup(ArduinoPath, Programmers, SelectedProgrammer, ProgrammerPorts, SelectedPort, true, out message)) { string jsResults; using (AutoJSContext context = new AutoJSContext(_browser.Window.JSContext)) using (JSAutoCompartment compartment = new JSAutoCompartment(context, (nsISupports)_browser.Window.DomWindow)) { context.EvaluateScript("(Blockly.Generator.workspaceToCode('Arduino'))", out jsResults); } System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(_browser.ClientRectangle.Width, _browser.ClientRectangle.Height); _browser.DrawToBitmap(bmp, _browser.ClientRectangle); var screenshot = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bmp.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); //The following is like 3 card Monty with threads and contexts, but it does work ;) var loadingWindow = new UploadingWindow() { Owner = this, WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner }; loadingWindow.BlocksImage.Source = screenshot; IsEnabled = false; loadingWindow.Loaded += (s, ev) => { Task.Run(() => { string errorText; bool result = SendToArduino(true, jsResults, out errorText); Dispatcher.InvokeAsync(() => { loadingWindow.Close(); IsEnabled = true; if (result) { MessageBox.Show(this, "Success!", "Upload Complete!", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show(this, "There were problems:\n" + errorText ?? "UNKNOWN ERROR", "Upload Failed :(", MessageBoxButton.OK, MessageBoxImage.Error); } }); }); }; loadingWindow.ShowDialog(); } }
void Upload_Click(object sender, RoutedEventArgs e) { string message; if (ValidateArduinoSetup(ArduinoPath, Programmers, SelectedProgrammer, ProgrammerPorts, SelectedPort, true, out message)) { string jsResults; using (AutoJSContext context = new AutoJSContext(_browser.Window.JSContext)) using (JSAutoCompartment compartment = new JSAutoCompartment(context, (nsISupports)_browser.Window.DomWindow)) { context.EvaluateScript("(Blockly.Generator.workspaceToCode('Arduino'))", out jsResults); } System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(_browser.ClientRectangle.Width, _browser.ClientRectangle.Height); _browser.DrawToBitmap(bmp, _browser.ClientRectangle); var screenshot = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bmp.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); //The following is like 3 card Monty with threads and contexts, but it does work ;) var loadingWindow = new UploadingWindow() { Owner = this, WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner}; loadingWindow.BlocksImage.Source = screenshot; IsEnabled = false; loadingWindow.Loaded += (s, ev) => { Task.Run(() => { string errorText; bool result = SendToArduino(true, jsResults, out errorText); Dispatcher.InvokeAsync(() => { loadingWindow.Close(); IsEnabled = true; if (result) { MessageBox.Show(this, "Success!" ,"Upload Complete!",MessageBoxButton.OK,MessageBoxImage.Information); } else { MessageBox.Show(this, "There were problems:\n" + errorText ?? "UNKNOWN ERROR","Upload Failed :(", MessageBoxButton.OK, MessageBoxImage.Error); } } ); }); }; loadingWindow.ShowDialog(); } }