Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-10-30
3
Medium Priority
?
261 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 2000 total points
ID: 17839955
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
ID: 17839985
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
ID: 17841231
Static constructors get called the first time the class is accessed by your code.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

877 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