954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help in threading problem

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.

MxDev
Junior Poster
141 posts since Sep 2007
Reputation Points: 8
Solved Threads: 3
 

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);
    }
  }
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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

MxDev
Junior Poster
141 posts since Sep 2007
Reputation Points: 8
Solved Threads: 3
 

You're welcome

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

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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?

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You