Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am writing the service to read data values from simulator. When i try to debug my service I am receiving following exception "ActiveX Control cannot be instantiated because the current thread is not in a single threaded apartment".Any help in this regard is very much appreciated.
My service code is
C#
<pre>public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            this.read();
        }
        public void read()
        {
            String sFilepath = @"C:\Users\admin\Desktop\TestService Projects\DATA\Read.txt";
            System.IO.StreamWriter oStreamWriter = new System.IO.StreamWriter(sFilepath, true);
            Connection con = new Connection();
            string[] result= con.read(4, Connection.baudrate.B19200, Connection.Parity.E, Connection.Databit.EIGHT, Connection.Stopbit.ONE, Connection.RegisterType.HoldingRegister, "1", 10);
            for(int i=0;i<result.Length;i++)
            {
                oStreamWriter.WriteLine(result[i]);
            }
            oStreamWriter.Close();
         }

        protected override void OnStop()
        {
        }



What I have tried:

I have tried debugging and reinstalling setup file completely.
Posted
Updated 7-Apr-17 5:03am
v3
Comments
Suvabrata Roy 7-Apr-17 3:11am    
Please share the service code.
Member 11790928 7-Apr-17 3:20am    
@suvabrata Roy I am invoking a dll file in my service which inturn invokes the windows form to set the parameters
Suvabrata Roy 7-Apr-17 3:24am    
Please post the full code, I could not found any active X dependency in your current code block
Bernhard Hiller 7-Apr-17 3:12am    
"ActiveX Control" - really? A service must not contain any UI elements. OK, there might be ActiveX controls not containing UI elements, but I guess ...
Member 11790928 7-Apr-17 3:20am    
I am invoking a dll file in my service which inturn invokes the windows form to set the parameters

1 solution

have you tried tagging your method as:
[ComRegisterFunction]
public void read()
        {
            String sFilepath = @"C:\Users\admin\Desktop\TestService Projects\DATA\Read.txt";
            System.IO.StreamWriter oStreamWriter = new System.IO.StreamWriter(sFilepath, true);
            Connection con = new Connection();
            string[] result= con.read(4, Connection.baudrate.B19200, Connection.Parity.E, Connection.Databit.EIGHT, Connection.Stopbit.ONE, Connection.RegisterType.HoldingRegister, "1", 10);
            for(int i=0;i<result.Length;i++)
            {
                oStreamWriter.WriteLine(result[i]);
            }
            oStreamWriter.Close();
         }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900