进行跨线程调用以隐藏窗体可视化 C# [英] making a cross-thread call to hide a form visual c#

查看:104
本文介绍了进行跨线程调用以隐藏窗体可视化 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:解决方案从那时起就到了,现在被接受了.由于这个问题的伪代码很清楚,解决方案只用了几秒钟.试图从其他看似重复"的问题中找出解决方案已经花费了很多时间.从我问这个问题的那一刻起,我在 20 分钟内就得到了解决方案.所以它不是重复的,可能是迄今为止最清晰的.

虽然已经提出了几个类似的问题,但我仍然无法从他们那里整理出一个有效的代码.我的主要应用程序不在表单上,​​而是在一个单独的类中,该类是一个插件并由主机应用程序执行.

While several similar questions have been asked on this, I was still not able to put together a working code from them. My main application is not on a form, but in a separate class that is a plugin and executed by a host application.

所以虽然这似乎是一个重复的问题,但事实并非如此.请继续阅读.

So while it seems a duplicate question it is not. Please read on.

我让我的主班做事.然后我有一个向用户显示信息的表单.当用户离开应用程序(主机应用程序失去焦点)时,我需要隐藏此表单.

I have my main class doing stuff. Then I have a form that displays information to the user. I need to hide this form when the user switches away from the application (host application loses focus).

我使用的 API 非常有限,因此我可以使用的唯一方法是由主机应用程序触发的事件.

I am using very limited APIs so the only methods I have at my disposal events triggered by the host application.

我创建了一个计时器,它每 100 毫秒触发一次,并检查用户是否关注应用程序.如果没有,则会向表单发送命令以隐藏自身.

I created a timer that fires every 100ms and checks whether the user had the application in focus. And if not a command is sent to the form to hide itself.

采用这种方法的原因是宿主应用程序失去焦点只是我需要隐藏表单的众多场景之一,我需要通过完全相同的方法来引导所有这些.

The reason for this approach is because the host application loosing focus is just one of the many scenarios that I need to hide the form and I need to channel all these through the same exact method.

所有在其余类中的工作(Hide() 方法从应用程序的其余部分调用没有问题.但是当计时器调用 Hide() 方法时它不起作用,因为计时器在触发时位于不同的线程上.这样调用就变成了跨线程调用.

All works from within the rest of the classes (the Hide() method is called from the rest of the application no problem. But it does not work when the timer calls the Hide() method, because the timer is on a different thread when it fires. So the call becomes a cross-thread call.

非常具体的问题是,我需要一个确切的示例代码,如何使用 InvokeHide() 方法线程安全的调用>.

The very specific question is that I need an exact sample code how to make this call from the timer event handler to the form's Hide() method thread-safe with Invoke.

谢谢.

这是计时器:

private void Controllel_Opened(object sender, EventArgs e)
{
    myTimer.Elapsed += new System.Timers.ElapsedEventHandler(DisplayTimeEvent);
    myTimer.Interval = 50;
    myTimer.Start();
}

public static System.Timers.Timer myTimer = new System.Timers.Timer();
// This method checks different scenarios when the tool tip should be hidden and calls the hiding method
public static void DisplayTimeEvent(object source, System.Timers.ElapsedEventArgs e)
{
    FormHandler.Hide();
}

然后是FormHandler"类:

Then the "FormHandler" class:

public static class FormHandler
{
    private static Form1 frm1 = new Form1();

    public delegate void Form1HideEventHandler();
    public static event Form1HideEventHandler Form1Hide;

    public static void Hide()
    {
        if (Form1Hide != null)
        {
            Form1Hide();
        }

    }
}

然后是表单的代码:

public partial class Form1 : Form
{   
    public Form1()
    {
        InitializeComponent();
        FormHandler.Form1Hide += FormHandler_Form1Hide;
    }

    private void FormHandler_Form1Hide()
    {
        Hide();
    }
}

如果可能的话,我想得到一个带有确切代码的解决方案.谢谢.

I would like to get a solution with exact code if possible. Thanks.

推荐答案

private void FormHandler_Form1Hide()
{
    if (InvokeRequired)
    {
        this.Invoke(new Action(() => { FormHandler_Form1Hide(); }));
    }
    else
    {
        Hide();
    }
}

这篇关于进行跨线程调用以隐藏窗体可视化 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆