用windows窗体打开文件,用下面的代码 if (openFile.ShowDialog() == DialogResult.OK)
        {
            TextBox1.Text = openFile.FileName;
        }但是编译的时候,说线程错误
“Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute ed on it. This exception is only raised if a debugger is attached to the process.”对线程理解不深,哪位大虾帮帮忙,多谢。

解决方案 »

  1.   

    这段程序没什么问题,主函数前加了[STAThread]属性吗?
      

  2.   

    程序不大,我写了一遍using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using jmail;namespace WindowsApplication4
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.OpenFileDialog openFile;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.openFile = new System.Windows.Forms.OpenFileDialog();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(96, 160);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(32, 40);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "textBox1";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 270);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    if (openFile.ShowDialog() == DialogResult.OK)
    {
    textBox1.Text = openFile.FileName;
    }
    }
    }
    }
      

  3.   

    看看你编译的属性   是不是编译成dll,而不是exe
      

  4.   

    我测试了你的程序,可以正常运行。
    建议你重新测试(新建一个项目,只有一个form的)。
    怀疑你一个项目中有两个窗体都有[STAThread]Main(){}入口了。
      

  5.   

    多谢likang2008(灵感),代码很详细不过,我还是不太明白我是在web页上添加OpenFileDialog的,用按钮触发。这样的话,属性[STAThread]应该定义在哪里呢?qwersky(爱睡觉的小虫):编译的属性  编译成dll,而不是exe?你能详细说一下怎么改么?
      

  6.   

    看一下这个
    [AttributeUsage(AttributeTargets.Method)]
    public sealed class STAThreadAttribute : Attribute
      

  7.   

    web ??
    你用OpenFileDialog???
      

  8.   

    Web页面上好象不是用OpenFileDialog的吧??
    用INPUT type="file"这种应该是