0

I am making an application where I have a tabControl and some controls I want to create. So I have a function that basically makes controls and a textbox. The code looks like this:

TextBox message_box = new TextBox();
message_box.Size = new Size(790, 38);
message_box.Location = new Point(9, 7);
message_box.Font = new Font("Microsoft Sans Serif", 20);
message_box.KeyDown += Message_box_KeyDown;
message_box.Enter += Message_box_Enter;
bottom_panel.Controls.Add(message_box);

I also added some other controls, but this specific control is the one I am having problems with. Because when I add these controls to the tabpage, and then I add the tabpage to the tabcontrol something strange happenes.

Every single control in the added tabPage is working fine, and without lag. But when I type in the textBox I get massive lag and it freezes. This happens also if I add textbox to the designer created page in the tabControl aswell.

This is the event that is being triggered:

private async void Message_box_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        string name = "";
        this.Invoke(new MethodInvoker(delegate { name = tabControl1.SelectedTab.Name; }));
        TextBox box = new TextBox();
        this.Invoke(new MethodInvoker(delegate {  box = (TextBox)sender; }));
        if (box.Text.Length > 0)
        {
            uiControls.ListItem melding = await Task.Factory.StartNew(() => sendMelding(name, box.Text));
            box.Clear();
        }
    }
}

I don't know what I am doing wrong, and how to make the textBox to stop lagging. I have tryed to check if it needs invokation, I have tryed invokation. I have also used this code, owerwriting the CreateParams in the form:

protected override CreateParams CreateParams
{
    get
    {
        // add the drop shadow flag for automatically drawing
        // a drop shadow around the form
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;
        cp.ClassStyle |= CS_DROPSHADOW;
        return cp;
    }
}

To be clear, the KeyDown function has no hit on the performance, the code within the KeyDown function is NOT fired everytime i write, its only fired when i hit enter. however usually if i write about 2-3 chars in the textbox the lagg starts to appear and the program freezes, if i remove the events it also occours.

blueio22
  • 48
  • 7
  • What functionality `sendMelding` is doing? it is necessary invoke `sendMelding` method each time your textbox has data? – Mauricio Arias Olave Dec 12 '16 at 14:42
  • Does the input lag only when you hit the Enter key, or on every key press? – krillgar Dec 12 '16 at 14:48
  • Do you have the lag even if you **don't** invoke the KeyDown event? – Vikhram Dec 12 '16 at 14:55
  • 1
    Maybe it is a problem with the `StartNew` method. See [this](http://blog.stephencleary.com/2013/08/startnew-is-dangerous.html) – OrdinaryOrange Dec 12 '16 at 14:57
  • Yes i have lag regardless of the keydown event – blueio22 Dec 12 '16 at 19:34
  • Its on every keypress, the KeyDown event is only fired when i press enter – blueio22 Dec 12 '16 at 19:41
  • To debug, comment out all of the code in the KeyDown event handler. If the problem persists then the KeyDown event handler isn't the culprit. I can't say what might be causing the problem, though, because you haven't shown any other code that might be the culprit. – Jim Mischel Dec 12 '16 at 20:45
  • @blueio22 If the KeyDown Event isn't the problem could you probably post the code of your enter event? Further i would like to know if you are using a simple Textbox or did you write a descendent or sth. – Sebi Dec 13 '16 at 08:01

1 Answers1

1

I suspect the delay occurs because you're waiting for the task to complete before you continue. That is, you have:

if (box.Text.Length > 0)
{
    uiControls.ListItem melding = await Task.Factory.StartNew(() => sendMelding(name, box.Text));
    box.Clear();
}

The call to box.Clear won't be executed until the Task completes.

It's difficult to say how you'd go about fixing this, because I don't know what you're trying to accomplish here.

Also, you need to review your use of Invoke in that method. You have some errors. For example, you access box.Text.Length without Invoking, which means that if the event handler is called on a non-UI thread, you're going to get an exception.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • The code in the KeyDown event is only fired when i press enter. But the lag and freezing happens regardless with or without the event – blueio22 Dec 12 '16 at 19:36
  • I am also aware of the exception that is being raised due to cross thread invocation. But the lag and the freezing of the textbox happens regardless of the event, and its algorithm. – blueio22 Dec 12 '16 at 19:42
  • So if you comment out all of the code that executes when Enter is pressed, does the lag still exist? If so, then perhaps the `KeyDown` event isn't the culprit and you should be looking somewhere else for the problem. – Jim Mischel Dec 12 '16 at 20:41