Jump to content

Windows.Form in Custom Profile


Recommended Posts

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

Link to comment
Share on other sites

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();
    }
}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...