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 - protected override void OnStart(string[] args)
-
{
-
-
do
-
-
{
-
if (queue.Count > 0)
-
-
{
-
itemFile nextFile = (itemFile ) queue.Dequeue();
-
System.Threading.Thread t = new
-
System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ProcessRequest));
-
t.Start(nextFile as object );
-
}
-
}
-
} while (true);
-
..
and I feel it is not the optimal way.
Can any one suggest best than the above code
thanks
Khayralla
21 1388
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
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
Have you tried using the File.Move Method to move the files?
My question is about
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.
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
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.
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.
I think your method is pretty good. Probably better than processing them all on the same thread.
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.
-
protected override void OnStart(string[] args)
-
{
-
-
pq = new Queue();
-
FileSystemWatcher watcher = new FileSystemWatcher();
-
watcher.Path = myPath;
-
watcher.NotifyFilter = NotifyFilters.FileName;
-
watcher.Filter = "*.txt";
-
watcher.Renamed += new RenamedEventHandler(OnRenamed);
-
watcher.EnableRaisingEvents = true;
-
do
-
{
-
if (pq.Count > 0)
-
{
-
string nextFile = (string) queue.Dequeue();
-
ProcessRequest(nextFile);
-
}
-
} while (true);
-
}
-
private static void OnRenamed(object source, RenamedEventArgs e)
-
{
-
string x = "some thing";
-
pq.Enqueue(x);
-
}
-
Now, Queue is fill with files name and I want to manage the files inside it?
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?
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
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.
Does anyone know of an object like a Queue, that has events? Something like an event that fires when an item is enqueued/dequeued?
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....
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
I'll look into it, see if I can't figure something like that out. That would solve your problem pretty well, right?
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.
This is the code for a class called QueueV2 that extends Queue to provide events for when items are Enqueued/Dequeued. QueueV2 -
using System;
-
using System.Collections.Generic;
-
-
namespace QueueV2
-
{
-
public delegate void QueueV2EventHandler(object sender, EventArgs e);
-
-
public class QueueV2<T> : Queue<T>
-
{
-
#region Custom Events
-
-
public event QueueV2EventHandler ItemEnqueued;
-
public event QueueV2EventHandler ItemDequeued;
-
-
protected virtual void OnItemEnqueued(EventArgs e)
-
{
-
ItemEnqueued(this, e);
-
}
-
protected virtual void OnItemDequeued(EventArgs e)
-
{
-
ItemDequeued(this, e);
-
}
-
-
#endregion
-
-
#region Original Methods
-
-
public new void Enqueue(T item)
-
{
-
base.Enqueue(item);
-
OnItemEnqueued(new EventArgs());
-
}
-
public new T Dequeue()
-
{
-
T value = base.Dequeue();
-
OnItemDequeued(new EventArgs());
-
return value;
-
}
-
-
#endregion
-
}
-
}
-
Here's how you would create a new queue and assign handlers to the events. -
QueueV2<string> qv2 = new QueueV2<string>();
-
qv2.ItemEnqueued += new QueueV2EventHandler(qv2_ItemEnqueued);
-
qv2.ItemDequeued += new QueueV2EventHandler(qv2_ItemDequeued);
-
Here's how you would define your handlers. -
void qv2_ItemDequeued(object sender, EventArgs e)
-
{
-
//whatever
-
}
-
-
void qv2_ItemEnqueued(object sender, EventArgs e)
-
{
-
//whatever
-
}
-
I enjoyed the opportunity to learn how to do this.
Hope it helps.
I will test it, I hope it will fit with my needs.
thanks a lot.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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...
|
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...
...
|
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...
|
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...
|
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...
|
by: CedricCicada |
last post by:
Greetings!
Here's my script:
######## start of script
class ScannerCommand:
taskName = ''
scanList =
def __init__(self):
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |