943,728 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 501
  • C# RSS
Sep 26th, 2009
0

help in threading problem

Expand Post »
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

C# Syntax (Toggle Plain Text)
  1. [STAThread]
  2. private static void newInstance() {
  3.  
  4. //the following code also exists in Main() method
  5.  
  6. Application.EnableVisualStyles();
  7. Application.SetCompatibleTextRenderingDefault(false);
  8. Application.Run(new Form1());
  9. }

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.
Similar Threads
Reputation Points: 8
Solved Threads: 3
Junior Poster
MxDev is offline Offline
137 posts
since Sep 2007
Sep 26th, 2009
0

Re: help in threading problem

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.

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10.  
  11. namespace daniweb
  12. {
  13. public partial class frmProcess : Form
  14. {
  15. public frmProcess()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void button1_Click(object sender, EventArgs e)
  21. {
  22. Process.Start(Application.ExecutablePath);
  23. }
  24. }
  25. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 27th, 2009
0

Re: help in threading problem

Thanks sknake, I appreciate your help, it worked fine!!!!
Reputation Points: 8
Solved Threads: 3
Junior Poster
MxDev is offline Offline
137 posts
since Sep 2007
Sep 27th, 2009
0

Re: help in threading problem

You're welcome

Please mark this thread as solved if you have found an answer to your question and good luck!
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 27th, 2009
0

Re: help in threading problem

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?
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Array Issues
Next Thread in C# Forum Timeline: C#Express & Access DB not flowing to Form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC