Home
Options

Is to possible to force panels to be shown and updated whilst Non-Async script is running?

As an example, the following script will not display the summary panel until the script has finished, but I would like it to display the summary panel before the five second delay.

var panel = new System.Windows.Controls.DockPanel();
panel.Dump("Summary");
var tb = new System.Windows.Controls.TextBox();
panel.Children.Add(tb);

tb.Text += "Started\r\n";

"Waiting for 5 seconds".Dump();
System.Threading.Thread.Sleep(5000);

tb.Text += "Finished\r\n";

Is this possible?

Comments

  • Options

    Windows controls cannot be rendered without a thread running a message loop. You need to either sleep asynchronously (await Task.Delay) or use LINQPad's HTML controls instead of Windows controls.

  • Options

    Am I correct in thinking that LINQPad's HTML controls can only be used in the results panel and there it no way to do something similar to the above script or to have something that acts like a second results panel?

  • Options
    edited July 2022

    I think you would have to change up the way you make changes to the control. Since it is WPF, the UI is running separate from the query so you'll need to issue commands asynchronously to update it. (I may have botched the explanation tho)

    This works and I would also recommend getting used to using the dispatcher to make changes to UI elements.

    async Task Main()
    {
        var panel = new System.Windows.Controls.DockPanel();
        panel.Dump("Summary");
        //await PanelManager.DisplayWpfElementAsync(panel, "Summary");
    
        var tb = new System.Windows.Controls.TextBox();
        panel.Children.Add(tb);
        //await panel.Dispatcher.InvokeAsync(() => panel.Children.Add(tb));
    
        tb.Text += "Started\r\n";
        //await panel.Dispatcher.InvokeAsync(() => tb.Text += "Started\r\n");
    
        "Waiting for 5 seconds".Dump();
        await Task.Delay(5000);
    
        tb.Text += "Finished\r\n";
        //await panel.Dispatcher.InvokeAsync(() => tb.Text += "Finished\r\n");
    }
    

    (I had just realized the goal was to keep it synchronous, oops)

Sign In or Register to comment.