473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

long processing

Hi all

I am developing a small windows Service application which mointer a files in a specific folder and add their names to a queue by using a watch file class to add files to the queue.
This part is done.
Then in a windows Service I should move these files to another location (folder,DVD,...).

How can I do this task ?

I am using now


Expand|Select|Wrap|Line Numbers
  1. protected override void OnStart(string[] args)
  2. {
  3.  
  4.     do
  5.  
  6.     {
  7.         if (queue.Count > 0)
  8.  
  9.         {
  10.             itemFile nextFile = (itemFile ) queue.Dequeue();
  11.             System.Threading.Thread t = new
  12.             System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ProcessRequest));
  13.             t.Start(nextFile as object );
  14.         }
  15.     }
  16. } while (true);
  17. ..
and I feel it is not the optimal way.
Can any one suggest best than the above code

thanks

Khayralla
Oct 30 '08 #1
21 1388
Curtis Rutland
3,256 Expert 2GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Oct 30 '08 #2
Frinavale
9,735 Expert Mod 8TB
Have you tried using the File.Move Method to move the files?
Oct 30 '08 #3
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR

thanks, it was the first post for me
Oct 30 '08 #4
Have you tried using the File.Move Method to move the files?
My question is about
Expand|Select|Wrap|Line Numbers
  1. do
  2. {
  3. ...
  4. }while (true);
  5.  
maybe it will not execute others events which must be done by this service.
I mean I have to add Sleep(5000) or no need for it.
Oct 30 '08 #5
Frinavale
9,735 Expert Mod 8TB
It sounds like you want to implement a Windows Service. Did you create a Windows Service project when you first created your project?

You need to initialize your file watcher in your OnStart method of your Windows Service application.

You will also have to implement a method that handles the FileWatcher.Changed event.

The Changed event is fired whenever there is a change in the folder that it's watching....it is at this time that you want to move the file (or do whatever you need to do).


You don't have to loop forever...just handle the event.

<edit> Please note that you can not use F5 to run Windows Service applications....see How To Start Services </edit>

-Frinny
Oct 30 '08 #6
It sounds like you want to implement a Windows Service. Did you create a Windows Service project when you first created your project?

You need to initialize your file watcher in your OnStart method of your Windows Service application.

You will also have to implement a method that handles the FileWatcher.Changed event.

The Changed event is fired whenever there is a change in the folder that it's watching....it is at this time that you want to move the file (or do whatever you need to do).


You don't have to loop forever...just handle the event.

-Frinny
All what you mentioned are done.
I need to handle the Queue after it enqueued with some files?
say there are 10 files in the queue.

So it is better to use ininfint while loop or I have to use another thread ?
I don't want to block CPU from doing other activities in my infinit while loop.
Oct 30 '08 #7
Frinavale
9,735 Expert Mod 8TB
All what you mentioned are done.
I need to handle the Queue after it enqueued with some files?
say there are 10 files in the queue.

So it is better to use ininfint while loop or I have to use another thread ?
I don't want to block CPU from doing other activities in my infinit while loop.
Did you implement the Changed event?
Could you please post your code for this...I don't understand where you are creating a queue or why.

I'm not very strong with Threading but I know it's not a good idea to have an infinite loop creating threads..this is going to gobble up your resources.
Oct 30 '08 #8
Curtis Rutland
3,256 Expert 2GB
I think your method is pretty good. Probably better than processing them all on the same thread.
Oct 30 '08 #9
Did you implement the Changed event?
Could you please post your code for this...I don't understand where you are creating a queue or why.

I'm not very strong with Threading but I know it's not a good idea to have an infinite loop creating threads..this is going to gobble up your resources.
Expand|Select|Wrap|Line Numbers
  1.  protected override void OnStart(string[] args)
  2.         {
  3.  
  4.             pq = new Queue();
  5.             FileSystemWatcher watcher = new FileSystemWatcher();
  6.             watcher.Path = myPath;
  7.             watcher.NotifyFilter = NotifyFilters.FileName;
  8.             watcher.Filter = "*.txt";
  9.             watcher.Renamed += new RenamedEventHandler(OnRenamed);
  10.             watcher.EnableRaisingEvents = true;
  11.             do
  12.             {
  13.                 if (pq.Count > 0)
  14.                 {
  15.                string nextFile = (string) queue.Dequeue();
  16.                       ProcessRequest(nextFile);
  17.                 }
  18.             } while (true);
  19.         }
  20.         private static void OnRenamed(object source, RenamedEventArgs e)
  21.         {
  22.             string x = "some thing";
  23.             pq.Enqueue(x);
  24.         }
  25.  
Now, Queue is fill with files name and I want to manage the files inside it?
Oct 30 '08 #10
Frinavale
9,735 Expert Mod 8TB
I think your method is pretty good. Probably better than processing them all on the same thread.
Maybe I'm not thinking straight but why does the OP need an infinite loop to do this?

The way I understand it:

When the Service starts it checks if there is stuff that has to be processed...

The FileWatcher class should watch the folder and do any processing only when the Changed event is fired....it could create a new thread at that point to do the file processing if it's getting fired a lot...

So how does the looping benefit the OP in his solution?
Oct 30 '08 #11
Maybe I'm not thinking straight but why does the OP need an infinite loop to do this?

The way I understand it:

When the Service starts it checks if there is stuff that has to be processed...

The FileWatcher class should watch the folder and do any processing only when the Changed event is fired....it could create a new thread at that point to do the file processing if it's getting fired a lot...

So how does the looping benefit the OP in his solution?
Frinavale
1) what do you mean by OP ?
2) FileWatcher will add the file name to the queue, and there is another events will add some files to the queue, so the FileWatcher do what is supposed to do, adding to the queue.

later, I have to check the queue, pop first item then burn it on a DVD,
pop the 2nd and save it in another folder, etc...
So I need to watch the Queue this time, if it contain entries, process the request.
so it need to be in infinit loop.

any way, I was asking may be the ininfint loop is not a good solution, I am looking for creating an event that will fired if there are some files in the queue, or if the Queue.count > 0, which is better than ininfint loop.

thanks
Oct 30 '08 #12
Curtis Rutland
3,256 Expert 2GB
Maybe I'm not thinking straight but why does the OP need an infinite loop to do this?

The way I understand it:

When the Service starts it checks if there is stuff that has to be processed...

The FileWatcher class should watch the folder and do any processing only when the Changed event is fired....it could create a new thread at that point to do the file processing if it's getting fired a lot...

So how does the looping benefit the OP in his solution?
I was just answering the question of whether it's better to use processes or handle them all in the current thread. I don't know enough about the OP's question to answer the rest.

Khayralla, OP means "Original Poster," which of course, would refer to you. So Frinny was asking me why you needed a loop.
Oct 30 '08 #13
Curtis Rutland
3,256 Expert 2GB
Does anyone know of an object like a Queue, that has events? Something like an event that fires when an item is enqueued/dequeued?
Oct 30 '08 #14
Frinavale
9,735 Expert Mod 8TB
Does anyone know of an object like a Queue, that has events? Something like an event that fires when an item is enqueued/dequeued?
Maybe create a new object that inherits from the Queue....
Oct 30 '08 #15
Does anyone know of an object like a Queue, that has events? Something like an event that fires when an item is enqueued/dequeued?
insertAlias, it is a good question.
thanks
Oct 30 '08 #16
Curtis Rutland
3,256 Expert 2GB
I'll look into it, see if I can't figure something like that out. That would solve your problem pretty well, right?
Oct 30 '08 #17
I'll look into it, see if I can't figure something like that out. That would solve your problem pretty well, right?
yes, so there is no need for infinte loop.
Oct 30 '08 #18
Curtis Rutland
3,256 Expert 2GB
This is the code for a class called QueueV2 that extends Queue to provide events for when items are Enqueued/Dequeued.

QueueV2
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace QueueV2
  5. {
  6.     public delegate void QueueV2EventHandler(object sender, EventArgs e);
  7.  
  8.     public class QueueV2<T> : Queue<T>
  9.     {
  10.         #region Custom Events
  11.  
  12.         public event QueueV2EventHandler ItemEnqueued;
  13.         public event QueueV2EventHandler ItemDequeued;
  14.  
  15.         protected virtual void OnItemEnqueued(EventArgs e)
  16.         {
  17.             ItemEnqueued(this, e);
  18.         }
  19.         protected virtual void OnItemDequeued(EventArgs e)
  20.         {
  21.             ItemDequeued(this, e);
  22.         }
  23.  
  24.         #endregion
  25.  
  26.         #region Original Methods
  27.  
  28.         public new void Enqueue(T item)
  29.         {
  30.             base.Enqueue(item);
  31.             OnItemEnqueued(new EventArgs());
  32.         }
  33.         public new T Dequeue()
  34.         {
  35.             T value = base.Dequeue();
  36.             OnItemDequeued(new EventArgs());
  37.             return value;
  38.         } 
  39.  
  40.         #endregion
  41.     }
  42. }
  43.  
Here's how you would create a new queue and assign handlers to the events.
Expand|Select|Wrap|Line Numbers
  1. QueueV2<string> qv2 = new QueueV2<string>();
  2. qv2.ItemEnqueued += new QueueV2EventHandler(qv2_ItemEnqueued);
  3. qv2.ItemDequeued += new QueueV2EventHandler(qv2_ItemDequeued);
  4.  
Here's how you would define your handlers.
Expand|Select|Wrap|Line Numbers
  1. void qv2_ItemDequeued(object sender, EventArgs e)
  2. {
  3.   //whatever
  4. }
  5.  
  6. void qv2_ItemEnqueued(object sender, EventArgs e)
  7. {
  8.   //whatever 
  9. }
  10.  
I enjoyed the opportunity to learn how to do this.

Hope it helps.
Oct 30 '08 #19
I will test it, I hope it will fit with my needs.
thanks a lot.
Oct 30 '08 #20
tlhintoq
3,525 Expert 2GB
Frinavale
1) what do you mean by OP ?
2) FileWatcher will add the file name to the queue, and there is another events will add some files to the queue, so the FileWatcher do what is supposed to do, adding to the queue.

later, I have to check the queue, pop first item then burn it on a DVD,
pop the 2nd and save it in another folder, etc...
So I need to watch the Queue this time, if it contain entries, process the request.
so it need to be in infinit loop.

any way, I was asking may be the ininfint loop is not a good solution, I am looking for creating an event that will fired if there are some files in the queue, or if the Queue.count > 0, which is better than ininfint loop.

thanks
Its nice to think that you can just take action as soon as the FileWatcher tells you there is a new file... but you can't.

SystemFileWatcher fires a New event as soon as the file begins to be written to hard drive, not when it is done. So if you try to act on it too soon it may not be finished being written. If you look for a Changed event, I have generally seen three for most files. I *think* they are related to the created and modified dates being written/changed upon completion as well as the size of the file changing while it is being written. You see all of this clearly if you are writing across a network.

My solution in the past is to add the file path to a simple string[] then run through the array once every couple seconds from a timer. If a file completes whatever processing it is meant to have (move, print, whatever) then it can come off the list. This allows it to remain in its original location, or not, as needed by your program.
Oct 30 '08 #21
thanks tlhintoq

Actually I am copying files with a different extension (say .part), and when it is fininhed copying, the extension will be change to another ext. So the FilewatchSyatem will watch all files *.part, and then it will be fired when there is a change in the ext.
thanks
Nov 12 '08 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: BK | last post by:
Can someone point me to a code sample that illustrates executing long running tasks (asynchronous) from a web application in ASP.NET? I assume that Web Services might come into play at some point,...
1
by: Dan | last post by:
Good Day, I have a Windows Service that is responsible for processing somes files. One method of the service invokes another method which may take a while to complete its work. Something like...
3
by: Matt C. | last post by:
This is my first attempt at writing a .NET service, and also my first attempt at using threads. I don't know what I'm doing. Below is some simplified code from my service class (inheriting from...
0
by: Claire | last post by:
My application has a thread reading byte arrays from an unmanaged dll(realtime controller monitoring). The array represents an unmanaged struct containing a series of header fields plus a variable...
0
by: FatboyCanteen | last post by:
I have a page when it starts loading, it will perform a long time process! So, the Page is empty during this time. I want to add a Processing Message, eg Processing. Processing.. Processing... ...
5
by: PontiMax | last post by:
Hi, when I press the OK button of my dialog box a long-running task is initiated. Therefore I would like to make visible a div section right after clicking the button where a user-friendly...
18
by: Larry Herbinaux | last post by:
I'm having issues with garbage collection with my long-standing service process. If you could review and point me in the right direction it would be of great help. If there are any helpful...
9
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my...
8
by: CedricCicada | last post by:
Greetings! Here's my script: ######## start of script class ScannerCommand: taskName = '' scanList = def __init__(self):
3
by: lai_waiman | last post by:
Dear All, I have problems on doing some long lasting job in a web services. Let me first provide some background information first. I have a ASP.NET web page, which will call another Web...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.