hi guys,
I'm writting a simple text editor using visual studio 2008. When I'm trying to

make a new instance of the application using the following code which is existed

in Main() method

[STAThread]
private static void newInstance() {

//the following code also exists in Main() method

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());             
        }

the application creates a new instance, but the problem is when try to open,

save, and any other action the application generate a "ThreadStateException"

which says

"Current thread must be set to single thread apartment (STA) mode before

OLE calls can be made. Ensure that your Main function has

STAThreadAttribute marked on it. This exception is only raised if a debugger is

attached to the process."

How to solve this bug, or where I can find an answer???

Thanks in advance.

Recommended Answers

All 4 Replies

Why don't you call Process.Start() and create a new instance? I don't know why you would _want_ to implement what you are describing but it is a neat idea ;)

It probably has something to do with that visual studio hosting process. It always mucks my projects up so I shut it off.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace daniweb
{
  public partial class frmProcess : Form
  {
    public frmProcess()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Process.Start(Application.ExecutablePath);
    }
  }
}

Thanks sknake, I appreciate your help, it worked fine!!!!

You're welcome

Please mark this thread as solved if you have found an answer to your question and good luck!

Just an FYI and you still need to mark this question as SOLVED.

FYI: Not sure why a text editor application would launch a new instance of itself. Have you considered using an MDI Form?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.