Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to call a thread with arguments ?

Posted on 2012-09-08
8
Medium Priority
?
580 Views
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?
0
Comment
Question by:subhashchy
8 Comments
 
LVL 1

Expert Comment

by:SandS_es
ID: 38379054
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
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 38379076
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
 
LVL 8

Author Comment

by:subhashchy
ID: 38379291
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
Technology Partners: 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!

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38379348
?
What has this XML got to do with passing arguments to a thread (drive letters) ?
0
 
LVL 8

Author Comment

by:subhashchy
ID: 38379387
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38379415
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38379441
>> 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
 
LVL 8

Author Closing Comment

by:subhashchy
ID: 38379442
Thank you. Much clear now
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

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.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 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