程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Visual Studio 2015,C# STAThread 不工作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Visual studio 2015,C# STAThread 不工作?

开发过程中遇到Visual studio 2015,C# STAThread 不工作的问题如何解决?下面主要结合日常开发的经验,给出你关于Visual studio 2015,C# STAThread 不工作的解决方法建议,希望对你解决Visual studio 2015,C# STAThread 不工作有所启发或帮助;

STAThread 在 VS 2015 中被忽略了? 我有我的主要方法:

[STAThread]
static voID Main(String[] args)
{...}

而且我有一个使用 System.windows.Forms.OpenfileDialog 的函数并给了我以下异常:

{"在进行 olE 调用之前,必须将当前线程设置为单线程单元 (STA) 模式。确保您的 Main 函数上标记了 STAThreadAttribute。仅当调试器附加到过程。"}

代码:

 try
            {
                System.windows.Forms.OpenfileDialog openfileDialog1 = new System.windows.Forms.OpenfileDialog
                {
                    InitialDirectory = @"C:\",title = "browse Text files",checkfileExists = true,checkPathExists = true,DefaultExt = "txt",Filter = "txt files (*.txt)|*.txt",FilterIndex = 2,RestoreDirectory = true,Readonlychecked = true,ShowReadonly = true
                };

                if (openfileDialog1.ShowDialog() == System.windows.Forms.DialogResult.oK)
                {

                }
            }catch(Exception E)
            {

            }

解决方法

这解决了我的问题。

您可以使用以下 2 个类在 MTA 中使用 OpenFileDialog:

https://acharyarajasekhar.wordpress.com/2015/04/10/c-winforms-openfiledialog-with-mtathread/

大佬总结

以上是大佬教程为你收集整理的Visual Studio 2015,C# STAThread 不工作全部内容,希望文章能够帮你解决Visual Studio 2015,C# STAThread 不工作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。