Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6171157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:09:55+00:00 2026-05-23T23:09:55+00:00

This is my first attempt at creating a Windows service that calls multiple threads.

  • 0

This is my first attempt at creating a Windows service that calls multiple threads. What i did was create a console app, then added a windows service and installer to it. My code is below.

partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        System.Timers.Timer timer1 = new System.Timers.Timer();
        timer1.Interval = 10000;
        timer1.Start();
        timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
    }

    public static void timer1_Elapsed(object sender, EventArgs e)
    {
        Program.ThreadController();   
    }

    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop your service.
    }
}

class Program
{
    static void Main(string[] args)
    {
        ThreadController();
    }

    public static void ThreadController()
    {
        for (int i = 0; i < 3; i++)
        {
            new Thread(() => { processEventLogTesting(); });
        }
    }

    public static void processEventLogTesting()
    {
        EventLog.WriteEntry("ProcessThread1", "Process1 Works");
    }
}

I’m not sure why i’m getting this error, any hints would be awesome.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:09:56+00:00Added an answer on May 23, 2026 at 11:09 pm

    First I’d add in some capability to do debugging. It’s actually easy (when you know how!!!).

    Add this to your class:

    [DllImport("kernel32.dll")]
    public static extern Int32 AllocConsole();
    

    Then add this to your main function (and you CAN do this too when running as a service if you enable the service to access the user interface by the way).

    for (int loop = 0; loop < args.Count; loop++)
    {
      if (args[loop] == "debug")
        AllocConsole();
    }
    

    This gives you a console window and you can use Console.WriteLine to send output to it now. Yummy.

    Now what you want to achieve, is for the service control manager to call your OnStart method, and then that method must RETURN the OK signal to let the SCM know that your app started up ok. Otherwise, SCM thinks something is wrong and kills your app. So OnStart is supposed to start your threads. So, make a Run() method that gets run by your thread (and by Main), and make OnStart return as fast as you can.

    I notice I don’t see any of this in your code example:

    System.ServiceProcess.ServiceBase[] ServicesToRun;
    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinService1() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);

    I suspect that your app isn’t reporting to the SCM that it has successfully launched the “service” that is inside your executable.

    You want your app to fire up, hand off the “service” to the SCM and let it know you are ok, and then return, while your service is now running with its own thread.

    See this article for a great walk through:
    http://msdn.microsoft.com/en-us/library/aa984464(v=vs.71).aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first attempt at creating a service and incorporating it in my
This is my first attempt to create a GUI in MATLAB. I haven't been
Because this is my first attempt at an extension method that seems quite useful
I'm trying to create a simple threading procedure (granted this is my first attempt
I'm creating a Windows service that makes use of a FileSystemWatcher to monitor a
I'm using Visual Web Developer 2010 Express. This is my first attempt at creating
This is my first attempt at creating a package, so I must be missing
This is my first ever attempt at creating an svn repository and I'm confused.
This is my first attempt on semaphores and threads. I constructed this code from
This is my first attempt to use boost::threads and I have a silly question.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.