Solved

can i make a worker thread in a static constructor inside a static class?

Posted on 2006-10-30
3
250 Views
Last Modified: 2010-04-16
can i make a worker thread in a static constructor inside a static class?


    static class OffsetLocations
    {
        private static DataSet _dsLocalOffsets;
        private static DataSet _dsExternalOffsets;

        private static object myLock = new object();
       

        static OffsetLocations()
        {
            _dsLocalOffsets = new DataSet();
            _dsExternalOffsets = new DataSet();


            //make worker thread and start it HERE???

        }
0
Comment
Question by:joshuadavidlee
3 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
Comment Utility
Yes, you can make a worker thread and start it in the static constructor of a class.  Keep in mind, that the static constructor will not be invoked (and so, the worker thread will not be started) until a request has been made to invoke another static method, or create an instance of your class.

For example, in the following program, the worker thread will never be started:

    class Program
    {
        static void Main()
        {
            Console.ReadLine();
        }
    }

    static class OffsetLocations
    {
        static OffsetLocations()
        {
            Thread workerThread = new Thread(new ThreadStart(DoWork));
            workerThread.Start();
        }

        private static void DoWork()
        {
            while (true)
            {
                Console.WriteLine("Doing Work...");
                Thread.Sleep(1000);
            }
        }

        public static void Activate()
        {
        }
    }

In this scenario, the worker thread can be started by either invoking a static method,

    static void Main()
    {
        OffsetLocations.Activate();

        Console.ReadLine();
    }

or creating an instance of OffsetLocations:

    static void Main()
    {
        OffsetLocations instance = new OffsetLocations();

        Console.ReadLine();
    }
0
 

Author Comment

by:joshuadavidlee
Comment Utility
ok i will go ahead and try this.  
I did not know that you have to activate the static class first , i thought the constructor was activated instantly when your program was run

0
 
LVL 14

Expert Comment

by:existenz2
Comment Utility
Static constructors get called the first time the class is accessed by your code.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now