Link to home
Start Free TrialLog in
Avatar of subhashchy
subhashchyFlag for India

asked on

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?
Avatar of SandS_es
SandS_es

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
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of subhashchy

ASKER

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.
?
What has this XML got to do with passing arguments to a thread (drive letters) ?
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.
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  = )
>> 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.
Thank you. Much clear now