Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Action call lost with no exception thrown when calling a control's method from a thread!?

I'm trying to call a control's method from a thread. It's very simple code but for some reason, the call never gets executed if InvokeRequired is true. It hangs forever with no exception thrown. Below is a simplified version and it also hangs once the control is added to a parent form. If it was not added to a form InvokeRequired is false and it works fine. MyControl _ctl; Thread _thread; void Main() { _ctl = new MyControl { Dock = DockStyle.Fill }; Form frm = new Form(); frm.Controls.Add(_ctl); frm.Show(); (_thread = new Thread(Test)).Start(); Thread.Sleep(); _thread.Join(); } void Test() { _ctl.Write("Hello...!"); } class MyControl : TextBox { int _inputStart = -1; public void Write(string value) { Action act = () => { AppendText(value); _inputStart = SelectionStart; }; if (InvokeRequired) Invoke(act, Array.Empty()); else act(); } } I'd like to understand why does it hang? and what is the correct way to implement scenario?

user-image
Question ajoutée par Utilisateur supprimé
Date de publication: 2018/03/23
More Questions Like This