调用OpenFileDialog.ShowDialog当Windows窗体GUI挂起() [英] Windows Forms GUI hangs when calling OpenFileDialog.ShowDialog()

查看:306
本文介绍了调用OpenFileDialog.ShowDialog当Windows窗体GUI挂起()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目一个三层架构项目交谈,在后端WCF服务。当后端能够从服务获取数据时,它通知使用发布 - 订阅业务层,这在返回通知的GUI层

my project a three tier architecture project talking to a WCF service in the backend. When the backend is able to fetch data from the service, it notifies the business layer using publish-subscribe, which in return notifies the GUI layer.

我添加一个OpenFileDialog使用视觉工作室设计师我的UI设计。按钮事件处理函数调用ShowDialog的消息。但是,一旦我按一下按钮,整个UI挂起。

I have added an OpenFileDialog to my UI design using Visual Studios designer. A button event handler calls the ShowDialog message. However, once I click the button, the whole UI hangs.

已经用Google搜索了一下周围,我发现,使用委托是preferred的方式来处理这样的任务。然而,也无委托的问题仍然存在。

Having googled around a bit, I found out that using delegates is the preferred way to handle tasks like this. However, with nor without delegate the problem persists.

目前我的code是这样的:

Currently my code looks like this:

private void bOpen_Click(object sender, EventArgs e)
{
    Func<Image> del = delegate
    {
        OpenFileDialog d = new OpenFileDialog();
        if (d.ShowDialog() == DialogResult.OK)
        {
            return Image.FromFile(d.FileName);
        }

        return null;
    };

    Invoke(del);
}

我是从Java世界来了,所以我不是很熟悉C#UI编程的复杂性。

I'm coming from the Java world, so I'm not really familiar with the intricacies of C# UI programming.

任何事情,我在这里失踪?

Anything I'm missing here?

推荐答案

我似乎已经解决了这个问题,添加[STAThread]属性的主要方法。有人告诉我这样做,当我运行该程序在调试器 - 这是我没有做过,因为我经常从Windows运行从Visual Studio的服务和客户端

I seem to have solved the problem adding the [STAThread] Attribute to the main method. I was told to do so once I ran the program in a debugger - which I hadn't done before because I ran the service from Visual Studio and the client regularly from Windows.

[STAThread]
public static void Main(string[] args)
{
    GUI gui = new GUI();
    gui.ShowDialog();
}

有人能解释一下到底什么是虽然回事

Can anybody explain what exactly is going on though

这篇关于调用OpenFileDialog.ShowDialog当Windows窗体GUI挂起()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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