Hi,
I have to do multithreading. In my case each thread should run in a specified timer interval given in a database. I tried a sample by storing the data in an xml file and reading them with linq to xml. But it seems all the thread takes the last specified time interval in the xml file.
Let me try to explain the scenario a bit.
I have two records in xml file and for each I have set the timer interval value as 10 second and 20 second. Now when I run the thread, first time both executes imediatly and thereafter it takes timer interval as 20 for both the threads.
Please see below my codes:
my xml file:
<StoresList>
<Store>
<StoreListName>FIRST STORE</StoreListName>
<StoreListDescription>Testggggggg</StoreListDescription>
<ElapsTime>10000</ElapsTime>
</Store>
<Store>
<StoreListName>SECOND STORE</StoreListName>
<StoreListDescription>Test2</StoreListDescription>
<ElapsTime>20000</ElapsTime>
</Store>
A class:
public class StoreList
{
public string ListName { get; set; }
public string ListDescription { get; set; }
public string ElapsTime { get; set; }
}
Main method:
static void Main(string[] args)
{
timer = new System.Timers.Timer(5000);
timer.AutoReset = true;
timer.Elapsed += new ElapsedEventHandler(DownloadFiles);
timer.Start();
Console.ReadLine();
}
private static void DownloadFiles(object sender, ElapsedEventArgs e)
{
XDocument readStorelist = LoadStoreList();
IEnumerable<StoreList> storeList =
from stList in readStorelist.Descendants("Store")
select new StoreList
{
ListName = stList.Element("StoreListName").Value,
ListDescription = stList.Element("StoreListDescription").Value,
ElapsTime = stList.Element("ElapsTime").Value
};
foreach (StoreList strList in storeList)
{
Thread th = new Thread(DoWrok);
th.Start(strList);
}
}
private static void DoWrok(object stList)
{
StoreList storeList = (StoreList)stList;
timer.Interval = Convert.ToDouble(storeList.ElapsTime);
Console.WriteLine();
Console.WriteLine(DateTime.Now.ToString() + " STORE NAME: " + storeList.ListName);
Console.WriteLine("Store list description: " + storeList.ListDescription);
}
Can someone tell me how can I call each thread with different timer interval.
Thanks, this forum has been great help for me.
Regards,
Suraj