1:C# OpenFileDialog.ShowDialog 打不开,程序无响应

环境:win7 .Net framework2.0
现象; c#写的一个程序,在xp下点击文件打开按钮没有任何问题,但在我的win7下,点击则不弹出打开文件对话框,程序忙且无响应
网上搜索其他人也出现过这样的问题,且有些计算机行有些不行。
给出解决办法为设置openFileDialog打开的缺省目录。
可是设置了后仍然不行,在win7下,点击打开无法弹出打开文件对话框且程序忙,无响应
继续搜索,发现一个帖子也是说这个问题,大致是主线程必须是单线程单元,否则可能会导致此问题,应在主线程上加 [STAThread] 属性
http://social.msdn.microsoft.com/Forums/zh-CN/vcgeneral/thread/105702a4-475d-4209-9730-c02cef7895e2
更改main函数的声明如下,问题解决。
[STAThread]
public static void Main(string[] args)

网上找到的关于[STAThread]的一些说明:
[STAThread]
STAThread:Single     Thread     Apartment Thread.(单一线程单元线程)
[]是用来表示Attributes;

[STAThread]
是一种线程模型,用在程序的入口方法上(在C#和VB.NET里是Main()方法),来指定当前线程的ApartmentState 是STA。用在其他方法上不产生影响。在aspx页面上可以使用AspCompat = "true" 来达到同样的效果。这个属性只在  Com  Interop  有用,如果全部是  managed  code  则无用。简单的说法:[STAThread]指示应用程序的默认线程模型是单线程单元 (STA)。启动线程模型可设置为单线程单元或多线程单元。如果未对其进行设置,则该线程不被初始化。也就是说如果你用的.NET Framework,并且没有使用COM Interop,一般不需要这个Attribute。其它的还有MTA(多线程套间)、Free  Thread(自由线程)。

[STAThread] attribute指示应用程序的 COM 线程模型是单线程单元。
而于此对应的多线程单元则是 [MTAThread] (多线程单元线程)

COM 线程模型只适用于使用 COM interop 的应用程序。如果将此属性应用到不使用 COM interop 的应用程序,将没有任何效果。

COM 线程模型可设置为单线程单元或多线程单元。如果应用程序线程实际调用了 COM 组件,则仅为 COM interop 初始化该线程。如果没有使用 COM interop,则不初始化该线程。

我又在网络上找了两篇文章或许更能说明这个问题。我没有翻译,文章的大意是,由于很多COM在.NET环境下如果使用多线程的话,会导致引用的COM不能正常运行,而如果不声明程序为STAThread的话,.NET就会自动使用多线程来提高效率,这样就会导致不可预知的后果。

Q. When I create a c# project from scratch in VS.NET, the generated code always have a [STAThread] attribute above the main routine. What does the STAThread attribute really do? Can I change it to MTAThread instead? I have searched website and books, no one seems to explain this well.

Asked by anon. Answered by the Wonk on February 17, 2003

A.

The STAThreadAttribute marks a thread to use the Single-Threaded COM Apartment if COM is needed. By default, .NET won't initialize COM at all. It's only when COM is needed, like when a COM object or COM Control is created or when drag 'n' drop is needed, that COM is initialized. When that happens, .NET calls the underlying CoInitializeEx function, which takes a flag indicating whether to join the thread to a multi-threaded or single-threaded apartment.

A multi-threaded apartment (MTA) in COM is more efficient, since any of a number of RPC threads from a pool can be used to handle a request. However, an object on the MTA thread needs to protect itself from multiple threads accessing it at the same time, so that efficiency comes at a cost.

The single-thread apartment (STA) in COM is inherently single-threaded and therefore no additional thread synchronization is needed. The STA is implemented using the thread's Windows message queue, which is how requests to objects on an STA are serialized. Because of how the STA thread is implemented, calls to objects on that thread are serialized with Windows message handling on that thread, making sure that everything, both the COM objects and the underlying windowing objects, e.g. HWNDs, are all synchronized. This is necessary for UI-oriented COM objects, like controls and drag 'n' drop, which must also be synchronized together with the UI.

When COM is needed .NET will call CoInitializeEx, picking the MTA by default because it's more efficient. However, to get the synchronization needed for controls, windows and drag 'n' drop, you need to mark a thread's entry point with the STAThreadAttribute to let .NET know to initialize the UI thread on the STA. All of the VS.NET project templates put that attribute in to make sure you don't forget:

[STAThread]
static void Main() {...}

Be very careful to leave that STAThreadAttribute just where it is, or things can go all wacky and you won't know why.

Original Source Link: http://www.sellsbrothers.com/askthewonk/Secure/WhatdoestheSTAThreadattri.htm

2:ShowDialog() 错误的解决

首先,一个类里,有个linkLabel1

private OpenFileDialog openFileDialog1;
private DialogResult result;

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

openFileDialog1 = new OpenFileDialog();
            string patch = Application.StartupPath + "\\LOG\\";
            openFileDialog1.InitialDirectory = patch;
            openFileDialog1.Filter = "xls files (*.xls)|*.xls";

result = openFileDialog1.ShowDialog();

if (result == DialogResult.OK)
            {
                if (openFileDialog1.FileName != "")
                {
                    Process.Start(openFileDialog1.FileName);
                }
               
            }

}

就会报 
在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常。

在测试小程序里没有问题,当移到大程序里就这样的问题了。可能是线程多的原因。解决办法就是添加线程,代码如下

private Thread invokeThread;

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = patch;
            openFileDialog1.Filter = "xls files (*.xls)|*.xls";

invokeThread = new Thread(new ThreadStart(InvokeMethod));
            invokeThread.SetApartmentState(ApartmentState.STA);
            invokeThread.Start();
            invokeThread.Join();

if (result == DialogResult.OK)
            {
                if (openFileDialog1.FileName != "")
                {
                    Process.Start(openFileDialog1.FileName);
                }

}
        }

private void InvokeMethod()
        {
            result = openFileDialog1.ShowDialog();
        }

问题得到解决

转载1: http://hi.baidu.com/yycblog/blog/item/cb57cba46f56388dd1435891.html
转载2: http://www.cnblogs.com/verna/archive/2011/02/15/1955276.html

C# OpenFileDialog.ShowDialog 打不开,程序无响应(错误的解决)相关推荐

  1. 鸿蒙系统测试失败,ANR-WatchDog-ohos: 一个简单的监测程序,可检测到鸿蒙系统的 ANR(Application Not Response-应用程序无响应)错误并引发有意义的异常...

    ANR-WatchDog-ohos 一个简单的监测程序,可检测到鸿蒙系统的 ANR(Application Not Response-应用程序无响应)错误并引发有意义的异常 项目名称:ANR-Watc ...

  2. 计算机任务管理器无法响应,简单几步解win10任务管理器打不开提示无响应的方法...

    在使用win10系统过程中,每当我们电脑中的某个程序出现卡死的现象时,用户常常会通过打开任务管理器选择相应的程序结束进程来解决,但是有些用户在打开任务管理器的时候也会遇到打不开提示无响应的情况,那么面 ...

  3. 您没有权限来打开应用程序_苹果建议:除非应用程序无响应,否则不要滑动强制退出...

    紫金财经2月26日消息 今日,苹果公司发布的一条建议,成为了微博热搜的话题.苹果建议除非应用程序无响应,否则不要滑动强制退出. 苹果公司表示,滑动关闭iPhone的应用程序可能会缩短电池寿命,并使设备 ...

  4. 易语言程序假死优化_易语言假死无响应采用处理事件解决办法

    易语言假死无响应采用处理事件解决办法 处理事件() 一个比较简单的理解是:让程序反应过来 这个函数一般是用在延时前面或后面,如果不用的话程序很容易形成假死,造成程序无响应 如下图,虽然这个程序还在运行 ...

  5. chm打开秒退_Mac_Mac电脑程序无响应怎么办?Mac程序无响应解决方法,虽然Mac电脑一向以运行稳定、 - phpStudy...

    Mac电脑程序无响应怎么办?Mac程序无响应解决方法 虽然Mac电脑一向以运行稳定.流畅而著称,但Mac电脑运行时间长了,难免也会遇到程序卡死无响应.一直"转菊花"的情况,可能是由 ...

  6. Android Training - 避免程序无响应ANR

    可能你写的代码在性能测试上表现良好,但是你的应用仍然有时候会反应迟缓(sluggish),停顿(hang)或者长时间卡死(frezze),或者应用处理输入的数据花费时间过长.对于你的应用来说最槽糕的事 ...

  7. 简单有效的解决打开Xcode一直loading并显示程序无响应问题

    简单有效的解决打开Xcode一直loading并显示程序无响应问题 项目场景: 问题描述: 原因分析: 解决方案: 项目场景: 打开一个旧的项目文件的 main.storyboard 时候Xcode崩 ...

  8. 计算机应用程序无响应,Win7系统运行Word文档提示“应用程序没有响应”怎么办...

    Win7系统已经成为人们首选的主流操作系统,它以系统稳定著称,但是Win7系统在软件兼容性方面往往不及WinXP,比如大家在运行Word程序时就会发现经常编辑到一半时卡了,提示"应用程序没有 ...

  9. ANASYS Fluent保存的文件打不开或打开出现错误,解决方法

    ANSYS Fluent保存的文件打不开或打开出现错误,解决方法 问题描述: 当我们使用ANSYS Fluent将创建的文件运行保持后,有时打开出现错误,我们会很疑惑,怎么该软件和其他软件保存方式和打 ...

最新文章

  1. 透视鹏程.盘古:首个2000亿参数中文大模型是怎样炼成的?
  2. 让我们一起Go(十三)
  3. ubuntu下gcc的安装与使用
  4. inode与ln命令
  5. 03-instancing 工程分析详解
  6. 【渝粤教育】国家开放大学2018年春季 7392-22FMatlab语言及其应用 参考试题
  7. 库克:5G iPhone目前还不是我们考虑的问题
  8. 关于数论【莫比乌斯反演】
  9. Bailian4128 单词序列【BFS】
  10. JAVA魔法堂:折腾Mybatis操作SQLite的SQLException:NYI异常
  11. Q110:PBRT-V3十大基类对应的继承关系
  12. linux学习笔记:vim编辑器基本操作(附vim 键盘图)
  13. linux中vim怎么编辑文件内容,Linux 使用vim命令编辑文件内容
  14. 人在职场,身不由己?
  15. 别再说不知道元空间和永久代的区别了
  16. 10分钟学会go module
  17. 【答学员问】面试问题-毕业时候为什么没有选择开发
  18. 使用浏览器插件,下载网页中的图片
  19. win10系统开机停在请稍候解决教程【系统天地】
  20. Java管理扩展JMX入门学习

热门文章

  1. cocoscreator如何使用对象池-对象池使用详解
  2. 成都东部新区与百度签约战略合作,共同打造新型智慧城市标杆
  3. js时间戳转换日期格式 yyyy-MM-dd hh:mm:ss
  4. 【Day16】移动端布局
  5. NBIOT_BC95_AT命令集
  6. Unity加载C语言32dll,Unity3d 载入 C++/CLI (托管方式c++) 的动态库(DLL)
  7. CRUSH:可控、可扩展的复本数据非中心化的定位算法
  8. 爬虫与反爬虫系统的设计思路与策略
  9. 互联网相关概念——RFC
  10. 虚拟DOM和真实DOM的区别