Home > Enterprise >  Can't change the TextBox text
Can't change the TextBox text

Time:10-17

I would like to change the text of a TextBox in my Backgroundworker but it does not work and I cannot find the mistake in my code.

BackgroundWorker:

class Check_Server
{
    WebSocket webSocket = new WebSocket("ws://localhost:8548");
    private Form1 Form1;

    public void Check_WebSocket(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        while (!worker.CancellationPending)
        {
            Console.WriteLine("checking connection");
            if (webSocket.IsAlive == true)
            {
                Console.WriteLine("connected");
            }
            else
            {
                Console.WriteLine("not connected");
                
                Form1 form = new Form1();
                form.SetText("XYZ");
                MakeNewConnection();
            }
        }
    }

    void MakeNewConnection()
    {
        webSocket.Connect();      
        Thread.Sleep(10000);
    } 
}

Form:

public void SetText(string text)
{
    if (InvokeRequired)
    {
        this.Invoke((MethodInvoker)delegate () { SetText(text); });
        return;
    }
    
    textBox1.Text  = text;
}

CodePudding user response:

You don't display your Form. Add form.Show() or form.ShowDialog(), depending on whether or not you want to wait until the user closes the form.

Also, I found this extension method quite useful to make thread-safe changes to a control. Use it like this: textBox1.InvokeSave(() => textBox1.Text = text);.

public static partial class ControlExtensions
{
    public static void InvokeSave(this Control control, Action action)
    {
        Delegate del = action;

        try
        {
            if (control.InvokeRequired)
            {
                control.Invoke(del);
            }
            else action();
        }
        catch (ObjectDisposedException)
        {
            // guess do nothing is fine then
        }
    }
}

CodePudding user response:

Websockets in winforms is made a lot easier with a lib from Mariusz Kotas called, perhaps unsurprisingly, Websocket.Client ; if you open VS' nuget package manager and search it, you'll find it (currently circa version 4.3)

Setup could look like:

private WebsocketClient _cws;

private async void ConnectButton_Click(..){

    _cws = new WebsocketClient(new Uri("your uri here"));

    _cws.MessageReceived.Subscribe(msg => MessageReceived(msg.Text));

    _cws.DisconnectionHappened.Subscribe(info => DoSomething("Disconnection happened: "   info));
}

Sending a message could look like:

private async void ConnectButton_Click(..){
    _cws.Send(messageTextBox.Text);
}

Receiving a message is subscribed above:

private void MessageReceived(string msg)

    //local function for logging
    void addAndScrollAsync()
    {
        _messagesListBox.Items.Add( msg);

        //this scrolls to show new messages while the listbox is at the bottom, but doesn't if it's been scrolled up
        int visibleItems = _messagesListBox.ClientSize.Height / _messagesListBox.ItemHeight;
        if (_messagesListBox.Items.Count > visibleItems && _messagesListBox.TopIndex > _messagesListBox.Items.Count - visibleItems - 2)
            _messagesListBox.TopIndex = _messagesListBox.Items.Count - visibleItems   1;

    }

    _messagesListBox.InvokeEx(() => addAndScrollAsync());
        

}

I have some helper extensions to do any invoking:

public static class ControlExtensions
{
    public static TResult InvokeEx<TControl, TResult>(this TControl control,
                                               Func<TControl, TResult> func)
      where TControl : Control
    {
        return control.InvokeRequired
                ? (TResult)control.Invoke(func, control)
                : func(control);
    }

    public static void InvokeEx<TControl>(this TControl control,
                                          Action<TControl> func)
      where TControl : Control
    {
        control.InvokeEx(c => { func(c); return c; });
    }

    public static void InvokeEx<TControl>(this TControl control, Action action)
      where TControl : Control
    {
        control.InvokeEx(c => action());
    }
}
  • Related