You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已激活许可证时frmLicensing窗体无法隐藏的问题求助

问题:许可证激活后frmLicensing窗体无法隐藏

当系统中的许可证已激活时,程序虽能正常跳转到FrmLogin窗体,但frmLicensing仍会显示,导致两个窗体同时出现。怀疑问题出在代码中标记的部分,相关代码如下:

public partial class frmLicensing : Form
{
    public static frmLicensing instance;
    public frmLicensing()
    {
        InitializeComponent();
        instance = this;
    }

    private void frmLicensing_Load(object sender, EventArgs e)
    {
        using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
        {
            using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("Setting.txt", System.IO.FileMode.Open, isolatedStorageFile))
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(isolatedStorageFileStream))
                {
                    var Activated = Activation.isactive(sr.ReadLine());
                    if (!Activated)
                    {
                        this.Show();
                    }
                    else if(Activated == true)
                    {
                        // 标记的问题代码部分
                        this.Hide();
                        FrmLogin form = new FrmLogin();
                        form.Show(); 
                    }
                }
            }
        }
    }
}

问题原因与修复方案

核心原因

在frmLicensing_Load事件中调用this.Hide()时,窗体的加载显示流程尚未完成,后续的窗体初始化逻辑会覆盖Hide操作,导致窗体依然显示。

修复方法1:异步执行隐藏操作

通过BeginInvoke异步执行隐藏和登录窗体显示逻辑,确保窗体加载流程完成后再执行相关操作:

else
{
    this.BeginInvoke(new Action(() =>
    {
        this.Hide();
        FrmLogin form = new FrmLogin();
        form.Show();
    }));
}

修复方法2:直接关闭窗体(若无需复用)

如果后续不需要再使用frmLicensing窗体,可直接关闭而非隐藏:

else
{
    FrmLogin form = new FrmLogin();
    form.Show();
    this.Close();
}

注意:如果frmLicensing是程序启动窗体,直接关闭会导致整个应用退出,需配合调整Program.cs的启动逻辑。

修复方法3:调整启动逻辑(推荐)

将许可证检查逻辑移至Program.cs,在程序启动时直接判断并加载对应窗体,从根源避免窗体叠加问题:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    
    bool isActivated = CheckLicenseStatus();
    if (isActivated)
    {
        Application.Run(new FrmLogin());
    }
    else
    {
        Application.Run(new frmLicensing());
    }
}

private static bool CheckLicenseStatus()
{
    using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
    {
        using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("Setting.txt", System.IO.FileMode.Open, isolatedStorageFile))
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(isolatedStorageFileStream))
            {
                return Activation.isactive(sr.ReadLine());
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Thunderwolf4012

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 05:25:44