Solved

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

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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