Amrit Sharma
Amrit Sharma

Reputation: 1916

Update GUI in realtime

I am using SharpSSH to send command from windows form to one of the linux machine. Everything is working as expected except that I am not able to update the output in real time. Output appears once the command execution is complete. How can I update the output as it happens.

Here is my code that runs as background worker.

private void execute_green_DoWork(object sender, DoWorkEventArgs e)
        {
            List<object> argumentList = e.Argument as List<object>;
            string isp_username = (string)argumentList[0];
            string isp_password = (string)argumentList[1];
            string ssid = (string)argumentList[2];

            string stdOut = null;
            string stdError = null;
            string address = "192.168.1.100";
            string start = "Connecting...";
            this.Invoke(new MethodInvoker(delegate { progressWindow.green_update(start); }));
            System.Threading.Thread.Sleep(200);
            string user = "user";
            string pass = "password";
            string status = "Connected.....";
            this.Invoke(new MethodInvoker(delegate { progressWindow.green_update(status); }));
            System.Threading.Thread.Sleep(200);
            this.Invoke(new MethodInvoker(delegate { progressWindow.green_update("Programming....."); }));
            SshExec ssh_green = new SshExec(address, user, pass);
            ssh_green.Connect();
            ssh_green.RunCommand("cfg_green " + isp_username + " " + isp_password + " " + ssid, ref stdOut, ref stdError);
            output_green = stdOut;
            ssh_green.Close();

In above the output_green is rendered to richtextbox. Someone with knowledge in SharpSSH , can you please guide me.

Upvotes: 1

Views: 1714

Answers (1)

hungndv
hungndv

Reputation: 2141

This is using Synchronization context to update Form's lblTimer label:

CS:

public partial class MainForm : Form
{
    private readonly SynchronizationContext _context;

    public MainForm()
    {
        InitializeComponent();
        // the context of MainForm, main UI thread
        // 1 Application has 1 main UI thread
        _context = SynchronizationContext.Current;
    }

    private void BtnRunAnotherThreadClick(object sender, EventArgs e)
    {
        Task.Run(() =>
                 {
                     while (true)
                     {
                         Thread.Sleep(1000);
                         //lblTimer.Text = DateTime.Now.ToLongTimeString(); // no work
                         UpdateTimerInMainThread(); // work
                     }
                 });
    }

    private void UpdateTimerInMainThread()
    {
        //SynchronizationContext.Current, here, is context of running thread (Task)
        _context.Post(SetTimer, DateTime.Now.ToLongTimeString());
    }

    public void SetTimer(object content)
    {
        lblTimer.Text = (string)content;
    }
}

enter image description here

Hope this help.

Upvotes: 1

Related Questions