Solved

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

Posted on 2006-10-30
3
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 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