Solved

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

Posted on 2006-10-30
3
253 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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