|
I'm working on a project that uses the System.IO.FileSystemWatcher to monitor a folder. What I'm wanting to do is copy select types of files from the monitored folder when they are created. This works pretty slick, but the problem I run into is that the FileSystemWatcher event oftentimes fires before the file finishes copying, so copying the file results in an error.
I resolved this by creating a timer that's started on the FileSystemWatcher.created event. It waits a reasonable amount of time and then copies the file. No issues.
My problem is that this isn't a very good solution, particularly when hundreds or thousands of files are being copied onto the watched drive, as this can result in thousands of threads.
I wanted to resolve this by having the FileSystemWatcher.created thread add the files to be copied to an arrayList, then have a separate single thread monitor the array... wait for the file to be available to be copied, then start copying files. However this has more problems since the arrayList collection is changed on a separate thread which introduces a lot of locking issues, which I'd rather avoid.
Can anyone offer an idea of what the best practice for this situation would be? I appreciate any input.
Thanks!
Dana
|
|
|
|
|
Hi,
1.
yes, FileSystemWatcher is rather good at signaling the start of an action, it does not offer a Done event, and the best I ever could find was a loop, trying and waiting some, till success.
2.
hundreds of threads doesn't make sense, they all take a lot of resources. Anyway having multiple threads each copying one or a few files will not help, as soon the disk bandwidth will be the bottleneck. So don't take the multithreading hassle.
3.
the FSW events occur asynchronously, so their handlers aren't running on your main thread, instead they use a ThreadPoolThread. If you copy the file right away, you would be too soon, and also hog the thread. So you will end up using at least two threads: one for the FSW event (probably filling a queue), and one for copying the files, maybe a BackgroundWorker.
4.
Hence my proposal is: use a queue to store the incoming file paths; use a BGW that reads all entries from the queue and copies the files. If the copy fails (it will initially unless the file is really small), let it enqueue the file path again. When all entries have been processed (ignoring the ones requeued), then wait say one second, and repeat. You do need a lock for those queue operations, both at the producer side (FSW event and requeueing) and at the consumer side (BGW dequeue); make sure to keep the lock short, don't do File.Copy within the lock!
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Switch this round, and stop thinking of reacting and using the FileSystemWatcher - as it does have problems; which you've discovered now. So, if you can't copy the files like this, how can you do it? Well, a method I've had a lot of success with is to use Microsoft's Synchronisation Framework[^] which provides a file synchronisation provider, so you can transfer files based on periodical synchronisations of the folders."WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
As it was said before FSW isn't perfect, e.g. it doesn't report about each new file. My work around was to scan the whole dir every time FSW.created raised.
Regarding other questions Luc Pattyn suggested a viable approach.
PS: once you are doing such stuff you should familiarize yourself with synchronization methods.
|
|
|
|
|
Hi All
I want to make a Google's Quick Search Box(QSB) like Interface in C#. I want to know how this could be accomplished? The things I am unable to find out re:
1)Adding a button in taskbar
2)Customize UI for it
Kindly guide me.
Thanks
Sid Labs - Innovation for humanity
|
|
|
|
|
Hello, this is Vikash.
I am facing a problem related to Conversion of Hex String into Decimal Value.
The problem does not occur when the code is run in VB.net 2005.
But the same Code when run in VB6.0 gives a negative value.
This is the Code in VB.net :
dim C as string
C = CDec("&H" & "8CF1152C")
The Value of C in this case is "2364609836"
The same Code in VB6.0 gives C value as "-1930357460"
Could anyone tell me why this is happening or how to solve this problem?
Any help would be highly appreciated.
Thanks In Advance.
|
|
|
|
|
Well 0x8CF1152C has the sign bit set (assuming 32bit signed int), and therefore it's negative (if you treat it as singed int), but I don't know anything about VB or CDec so I don't really know what's going on here
|
|
|
|
|
In VB6 an Int is 16 bits and not 32 bits. This is the cause of many, many cross language problems involving VB6.
|
|
|
|
|
Harold has the right answer, the conversion fails because one is signed, and the other isn't. You can do this conversion in VB6 using this code;
Private Const OFFSET_4 = 4294967296#
Function LongToUnsigned(Value As Long) As Double
If Value < 0 Then
LongToUnsigned = Value + OFFSET_4
Else
LongToUnsigned = Value
End If
End Function
Dim longNumber As Long
longNumber = &H8CF1152C
unsignedNumber = LongToUnsigned(longNumber)
MsgBox (CStr(unsignedNumber))
That should do the trick
I are Troll
|
|
|
|
|
Thanks everyone for the reply.
Solution given by Eddy worked.
Now I am getting the same output as in VB.net.
Thanks once again.
|
|
|
|
|
Hello -> my apologies if this has been asked before....
I have a com wrapped dotnet dll. I use regasm and it runs with a result of successful. I try to create an instance of it using an asp page and fails to create object. I made a dotnet setup project (msi) and after the setup I run the asp page and it successfully creates the instance. What is setup doing that regasm isn't? I am not providing any arguments to regasm besides the dll name and path. Am i missing something to tell regasm to work properly?
Any help would be greatly appreciated. Thank you.
Nathan
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous
'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
|
|
|
|
|
nlarson11 wrote: What is setup doing that regasm isn't?
Perhaps it's not creating a TypeLibrary?
regasm.exe MyNetAssembly.dll /register /codebase /tlb
I are Troll
|
|
|
|
|
Hello Guys,
I need a help reagarding interview questions. I have an interview coming tuesday. what questions can I except in the interview. Please share you views. This position description is below
- Experience in working directly with web servers, and databases SQL Server 2008
- Knowledge of, XHTML, DHTML, CSS, JavaScript, DOM, JSON.
- Experience with application development in C#, PHP, ASP .net.
what is the best answer to tell if I asked for my experience in the last project.
Any help would be highly appeciated,
Thanks
|
|
|
|
|
1) Describe rest
2) What is a security concern with JSON and eval
3) What cross-browser issues should you be concerned with regarding the DOM
4) With CSS
5) With Javascript
6) Define DHTML
7) What is a common configuration issue with IIS regarding hosting ASP.NET and what is the solution?
|
|
|
|
|
sweety1988 wrote: what is the best answer to tell if I asked for my experience in the last project
The truth
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Now there's a novel concept.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
You're going to get any questions they feel like asking. There is no predicting, no standard set of questions. Either you know this stuff or you don't.
|
|
|
|
|
- any questions about things you have real experience with: tell a convincing story, not too technical, make your experience stand.
- be honest about experience you don't have. They will find out if your not honest. Tell them that you are a fast learner and that you will learn it quickly - even if you hesitate yourself on that.
- realize that someone who has all the experience they ask for probabilly don't wants the job because it has no challenge anymore for him.
- answer any non-technical question in a non-technical way. Reply to direct technical questions in a technical way. Be to the point and ask if your answer is enough.
- only speak about negative aspects of your current job in a general way: they don't want to know the nasty details.
- Know the company, try to understand what they want to achieve with you, participate on that. Realize that their goals mostly will not be: technical goals but bussiness goals.
- Try to find out who is the most important of the people you speak. Make sure he/she has a possitive impression of you, but don't forget the others.
And last the best tip i ever had:
Sit, speak and be there like you would be if you already had the job.
|
|
|
|
|
Is there any .Net framework classes which deal in file associations ? I am looking for the .Net equivalent of the Win32 shell functions "AssocQueryString" and "FindExecutable".
Edward Diener
|
|
|
|
|
Edward Diener wrote: Is there any .Net framework classes which deal in file associations
No.
Edward Diener wrote: I am looking for the .Net equivalent of the Win32 shell functions "AssocQueryString" and "FindExecutable".
There are no managed code versions of these functions.
|
|
|
|
|
Hi,
I want to use a simple function in a C++ DLL. The declaration in the header file for that function is:
#define API_SPEC __declspec(dllexport)
void API_SPEC API_getApiVersion (std::wstring *version)
That function receive a pointer to a UNICODE string and will put the release ID of that API in that string. (correct me if i'm wrong!)
I use VB.Net to call that DLL and read the content of that string. Here is my code:
Public Class Form1
<DllImport("C:\Api.dll", EntryPoint:="?API_getApiVersion@@YAXPAV?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@@Z", CallingConvention:=CallingConvention.ThisCall, CharSet:=CharSet.Unicode)> _
Public Shared Sub test(ByVal version As String)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim theVersion As String = New String(" "c, 20)
Call test(theVersion )
MessageBox.Show(theVersion)
End Sub
End Class
When I run that code I get:"AccessViolationException was unhandled."
My question: what is wrong with my code? I even try to use a stringbuilder instead of string type but I got the same error.
Thanks for your help!
|
|
|
|
|
Hi,
I don't have the full answer, however here are some pointers:
1.
IMO for writable strings you must use a StringBuilder, give it sufficient capacity, pass it on, then convert it to a string using ToString()
2.
your entypoint seems very bizarre, and way too long. I suggest you check it, perhaps using DUMPBIN/EXPORT
3.
I have an unfinished article[^]dealing with P/Invoke, maybe that can help you.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Thanks Luc, I will have a look to your article.
I took the entrypoint name using DUMPBIN /EXPORT.
|
|
|
|
|
Following on from Luc's comment, your exported function name is in C++ format, it should be C. Change your header file to put extern "C" { in front of the definition and } after it, and you should get exported as written.
MVP 2010 - are they mad?
|
|
|
|
|
Richard, do you mean that because the DLL has been written in C++ it can't be exported to DotNet?
That I lost a week trying to find a solution when there is not that way?
Unfortunately, because the DLL is from a third party, I can't recompile to change the export statement.
|
|
|
|