how to call a thread with arguments ?

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?
LVL 8
subhashchyAsked:
Who is Participating?
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
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.
        //
        newThread.Start(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}'",
            data);
    }

    public void DoMoreWork(object data)
    {
        Console.WriteLine("Instance thread procedure. Data='{0}'",
            data);
    }
}

/* 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

0
 
SandS_esCommented:
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);
    return;
}

Hope this helps.
Best,
David
0
 
subhashchyAuthor Commented:
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" ?>
<configuration>
 <volumes>C,D,E</volumes>
 <threshold>25</<threshold>
 <initialmonitoringnotification>5</initialnotification>
 <lastpatnotification>180</lastnotification>
</configuration>

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.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
AndyAinscowFreelance programmer / ConsultantCommented:
?
What has this XML got to do with passing arguments to a thread (drive letters) ?
0
 
subhashchyAuthor Commented:
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.
0
 
käµfm³d 👽Commented:
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  = )
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
>> 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.
0
 
subhashchyAuthor Commented:
Thank you. Much clear now
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.