August 1, 201510 yr hi there, like the title says i would like to instantiate a Windows.Form object in a Custom Profile. i tried it in an backgroundworker thread but it still freezes the wrobot and crashes. any idea how i could do this ? best regards
August 2, 201510 yr Hello, Try it: using System.Threading; using System.Windows.Forms; using Custom_Profile; public class CustomProfile : ICustomProfile { private Form _form; private bool _isStarted; public void Pulse() { _isStarted = true; var t = new Thread(OpenWinform); t.SetApartmentState(ApartmentState.STA); t.Start(); } public void Dispose() { _isStarted = false; if (_form != null) { // Use BeginInvoke because it not called from winform thread. _form.BeginInvoke(new MethodInvoker(_form.Close)); _form.BeginInvoke(new MethodInvoker(_form.Dispose)); } } void OpenWinform() { _form = new Form(); _form.Controls.Add(new Label { Text = "My window" }); _form.ShowDialog(); } }
Create an account or sign in to comment