how to call a thread with arguments ?

Posted on 2012-09-08
Last Modified: 2012-09-08
Hey Guys,

I m creating a windows service which will monitor the drives for free space.

Here is my work flow :

1. on start check if configuration.xml exist in the installation folder,
2. read the list of volumes from xml (It will be like C:, D:, E: , once each line).
3. After reading the xml, start one thread for each drive.

I have my code which works fine if i use the drive letter hard coded, I m able to read the xml and reader.value outputs the drive letters just fine.

I am stuck on how to call the thread with drive letter as an argument and run for all drives at same time.

Can you suggest a way?
Question by:subhashchy
    LVL 1

    Expert Comment

    Hi subhashchy,

    One possible way to solve this issue is to use static members of a "Configuration" class.
    For example, you could create a class called TConfig (or CConfig or whatever you like) that is in charge of reading (and writing, if necessary) the configuration files. The new threads, when started, must ask this class (using a static method) for the unit that the new thread has to monitor.
    This would mean that the thread main code would be something like:

    void theCodeToBeRunInAThread()
        string theUnitToBeMonitored=TConfig.GetNextUnit();
        codeToMonitorAUnit( theUnitToBeMonitored);

    Hope this helps.
    LVL 43

    Accepted Solution

    This is from the help files - about passing a paramter to a thread:

    using System;
    using System.Threading;
    public class Work
        public static void Main()
            // To start a thread using a shared thread procedure, use
            // the class name and method name when you create the 
            // ParameterizedThreadStart delegate.
            Thread newThread = new Thread(
                new ParameterizedThreadStart(Work.DoWork));
            // Use the overload of the Start method that has a
            // parameter of type Object. You can create an object that
            // contains several pieces of data, or you can pass any 
            // reference type or value type. The following code passes
            // the integer value 42.
            // To start a thread using an instance method for the thread 
            // procedure, use the instance variable and method name when 
            // you create the ParameterizedThreadStart delegate.
            Work w = new Work();
            newThread = new Thread(
                new ParameterizedThreadStart(w.DoMoreWork));
            // Pass an object containing data for the thread.
            newThread.Start("The answer.");
        public static void DoWork(object data)
            Console.WriteLine("Static thread procedure. Data='{0}'",
        public void DoMoreWork(object data)
            Console.WriteLine("Instance thread procedure. Data='{0}'",
    /* This code example produces the following output (the order 
       of the lines might vary):
    Static thread procedure. Data='42'
    Instance thread procedure. Data='The answer'

    Open in new window

    LVL 8

    Author Comment

    Thanks for the solution,The solution is right, but i am not able to use in my windows service :(
    would you please help me with the following content
    this is my xml file
    <?xml version="1.0" encoding="utf-8" ?>

    My service should refer to this above specification same as confguartion file(so that if in future ,if  i want to add any volume(i.e F, G, H),my service should monitor it with defined value)

    Apart from the above mentioned the service needs to spawn "n" number of threads after every 10secs and a particular thread(s) needs to be killed/terminated if the processing of same goes beyond 7sces. "n" number equals the number of volumes being monitored.
    LVL 43

    Expert Comment

    What has this XML got to do with passing arguments to a thread (drive letters) ?
    LVL 8

    Author Comment

    yes Andy,

     i will have to read the volume tag from xml  and call a thread with drive letter as an argument , so there will be one thread for each drive. The thread will then monitor the drive for free space and stuff.
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    In my opinion, your question is, "how to call a thread with arguments," and that is what AndyAinscow has shown you. Your new question regarding spawning a certain number of threads and killing them if they run too long should really be a new question  = )
    LVL 43

    Expert Comment

    >> i will have to read the volume tag from xml  and call a thread with drive letter as an argument , so there will be one thread for each drive. The thread will then monitor the drive for free space and stuff.

    So, I've shown you how to do the part you asked a question about.
    LVL 8

    Author Closing Comment

    Thank you. Much clear now

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    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…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 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

    21 Experts available now in Live!

    Get 1:1 Help Now