Add files to queue: before or after thread.Start()


i have windows service process xml files in queue. files in queue added filesystemwatcher event when files created.

the code is:

namespace xmlftp { public class xml_processor : servicebase { public string s_folder { get; set; } public xml_processor(string folder) { s_folder = folder; } thread worker; filesystemwatcher watcher; directoryinfo my_folder; public static autoresetevent resetevent { get; set; } bool running; public bool start() { my_folder = new directoryinfo(s_folder); bool success = true; running = true; worker = new thread(new threadstart(serviceloop)); worker.start(); // add files queue filesystemwatcher event return (success); } public bool stop() { try { running = false; watcher.enableraisingevents = false; worker.join(servicesettings.threadjointimeout); } catch (exception ex) { return (false); } return (true); } public void serviceloop() { string filename; while (running) { thread.sleep(2000); if (processingqueue.count > 0) { // process file , write info db. } } }

        void watcher_created(object sender, filesystemeventargs e)
        {

case watcherchangetypes.created:// add files queue

}

it works perfectly, want add other files queue. these files not triggered filesystemwatcher. there code it.

detectxml(my_folder); // add other files queue

there files processed service serviceloop method. question should place code detectxml(my_folder)?

before worker.start() is

my_folder = new directoryinfo(s_folder);  detectxml(my_folder);        bool success = true;  running = true;  worker = new thread(new threadstart(serviceloop));  worker.start();

or after worker.start(), likely

my_folder = new directoryinfo(s_folder);  bool success = true;  running = true;  worker = new thread(new threadstart(serviceloop));  worker.start();  detectxml(my_folder);
or doesn't matter @ all. thank you.



hi,

a linkedlist<t> isn't thread safe - see thread safety in class documentation - , , applies collections in system.collections.generic namespace.

here can/will result in problems. example initial post:

    while (running)     {         thread.sleep(2000);         if (processingqueue.count > 0)         {             // process file , write info db.         }     }

as access processingqueue.count isn't protected lock, count can change, if different thread alters "queue". result process file part may fail. that's case if implement count property as:

public static int count { 	get { lock (syncroot) return _files.count; } }

as lock released early.

with blockingcollection<t> can replace loop using getconsumingenumerable. foreach block until new element available, using sleep or checking count isn't necessary. manage shutdown see cancellation in managed threads

if using .net 2.0/3.5 there (less powerful) approaches - example producer/consumer queue.

regards, elmar



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

How to Share webservice object to all user