|
Agreed. I've seen this approach used very successfully in several production scenarios across various systems.
|
|
|
|
|
yup - used to call this technique 'sentinel file' - very handy for processing a large data file being FTP'd to you from a mainframe, you get the mainframe to send you a small sentinel file and wait for that to kick off further processing
|
|
|
|
|
As you'll see, there are all sorts of solutions, and as JSOP points out (iirc), the notifications you get depend on the application thats creating/? the file
One strategy I used (when not using 'Sentinel Files' as per my comment to Luc), was to queue a notification from FileWatcher, and then periodically see if I could get exclusive access to the file and/or combine the notification with a dir list check - if so, she's good to go, if not, wait a period - not sure if JSOP outlined that sort of strategy
|
|
|
|
|
When .Length in the file's FileInfo is greater than 0 (and .Exists), the file has "arrived".
|
|
|
|
|
The file's size would be greater than 0 the moment Windows starts writing to it, and Exists would be true... But the file could still be in the process of being written.
If it's not broken, fix it until it is
|
|
|
|
|
|
Yes
The Size is the size of the file being copied, and it exists
If it's not broken, fix it until it is
|
|
|
|
|
Me too now ... Changed since I first used it 4 (?) years ago.
Did notice something else this time though (.NET 4.6):
Fires 3 events during the download:
1) Created
2) Changed
3) Changed
First 2 fired as soon as the download started; the last one (#3), corresponded to the download being complete.
|
|
|
|
|
Ya but the problem is that you never really know what event are going to fire. Different apps cause difference events to fire.
I'll probably do this in the Watcher events:
private FileWatcher_Changed(...)
{
while(!IsFileAvailable())
{
}
}
public bool IsFileAvailable(String sFilename)
{
bool results = false;
try
{
using (var inputStream = File.Open(sFilename, FileMode.Open, FileAccess.Read, FileShare.None))
{
results = inputStream.Length > 0;
}
}
catch (Exception)
{
results = false;
}
return results;
}
[UPDATE]
Just tried it. Worked fine.
If it's not broken, fix it until it is
|
|
|
|
|
I avoid tying up the file watcher by queueing the events and kicking off a background worker (if one isn't already running) to deal with the queued events. Better than polling (IMO).
|
|
|
|
|
Meaning each time the Changed is called you fire off a BG worker wait for the file to be done?
Makes sense.. Then multiple files can be handles at one time.
If it's not broken, fix it until it is
|
|
|
|
|
In my case, the BGW services the "queue"; and is only started by the file watcher if it is not running; and quits when the queue is empty.
You could start a new task for each new file; but will it scale? (I can even "serialize" my queue if I have to).
|
|
|
|
|
I think you've lost me here. Could you provide a small sample?
If it's not broken, fix it until it is
|
|
|
|
|
|
There are lots of workable solutions.
In my case, I was using a "Concurrent Queue" to add file names to (by file watcher), and that a BGW "pops" and services. The BGW starts and stops based on entries in the queue. The queue can be serialized and reloaded at start-up; with an initial BGW started in the process.
The above scenario is used by field equipment to upload field reports to SharePoint; while online or to synch later.
|
|
|
|
|
I ran into an oddity this afternoon, what I considered a trivial task turned out to be, well, a surprise.
I have this line:
string ditto = Path.Combine("f:\\tfs", "\\SomeDir\\NewBuild", "SomeFile.abc");
And what I get is this:
Quote: \SomeDir\NewBuild\SomeFile.abc
Can someone explain to me why it does not show as:
Quote: f:\tfs\SomeDir\NewBuild\SomeFile.abc
This happens in Microsoft Visual Studio Premium 2013, Version 12.0.31101.00 Update 4.
Project uses .NET Framework 4.5
|
|
|
|
|
|
Of course... I guess Friday is a good day to be "blind" for the obvious
Thank you
|
|
|
|
|
Hi,
class Program
{
static void Main(string[] args)
{
bool isPrime = true;
Console.WriteLine("Prime Numbers are:");
for (int i = 2; i <= 100; i++)
{
for (int j = 2; j <= 100; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.Write("\t"+i);
}
isPrime = true;
}
Console.ReadKey();
}
}
in this code,i didn't understand this line of code,Please explain me???
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
what it means???
Thanks...
|
|
|
|
|
if (i != j
&&
i % j == 0)
|
|
|
|
|
|
The code:
if (i != j && i % j == 0)
{
isPrime = false;
break;
} It's a conditional statement: if the condition matches (i.e. i is not the same as j, and i is a multiple of j) then i is not prime, so it exits the inner loop - and the inner loop only. It still executes all the steps of the outer loop.
It's a very inefficient way to list all the primes up to one hundred, is all.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Member 11161625 wrote: for (int j = 2; j <= 100; j++)
No number is ever going to be evenly divisible by anything greater than itself, so you can eliminate those numbers from your loop:
if (int j = 2; j < i; j++)
But we can do better than that.
If i is evenly divisible by a number greater than the square root of i , then it will also be evenly divisible by a corresponding number less than the square root of i . Therefore, we can ignore all numbers greater than the square root:
int squareRoot = (int)Math.Floor(Math.Sqrt(i));
for (int j = 2; j < squareRoot; j++)
But we can still do better.
Currently, you're checking for divisibility by every number in the range. But if it's not divisible by 2, then it won't be divisible by any multiple of 2. Using the Sieve of Eratosthenes[^], you can eliminate all multiples of the prime numbers, leaving just the prime numbers:
var notPrime = new HashSet<int>();
for (int i = 2; i <= 100; i++)
{
if (!notPrime.Contains(i))
{
Console.WriteLine("\t{0}", i);
for (int j = i * 2; j <= 100; j += i)
{
notPrime.Add(j);
}
}
}
There are other even more efficient ways to find prime numbers, but they tend to be much more complicated, and would be overkill for a simple scenario like this.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
What this code is telling me is: since November, last year, you have racked-up a very large number of down-votes on a large number of questions in most of which you have demonstrated you have made very little effort to educate yourself.
It is time for you to make a decision to go to work doing the basic study that anyone has to do to become competent, at an entry level, in mastering any program language (or any subject matter).
When you have code like the code shown here (relatively simple), put it in a C# project, and put break-points in it, run the code, single-step through it (F11 in Visual Studio) observing how the values of variables change, and what the flow-of-control is.
Every time you see something that puzzles you like the character "%" : select the character, or whatever: hit the F1 key and go to the built-in help browser (or go-on-line and look it up) : read the relevant content, think about it.
Study, experiment, observe, refine, analyze, conclude ... repeat this forever with enthusiasm ... that is the only way to achieve any competency. CodeProject, StackOverflow, MSDN, blog sites (many exceptionally good blog sites run by Indians who are also regulars on CodeProject): these resources are amazingly deep, and can support you in whatever you pursue with C# and .NET.
Get a good fundamental book on C# .NET and read: alternate reading with trying out small code experiments directly related to what you read. Charles Petzold's ".NET Book Zero" is a free on-line download, and a good resource. Look in the "Free Tools" forum here for my previous post (January 11, 2015) on a free (English) book on C# by an association of programmers in Bulgaria.
"If not you, who ? If not now, when ?"
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”
|
|
|
|
|
Such sage advice which, being the 5th response and does not include any code will probably not have the least impact on such as the OP.
Never underestimate the power of human stupidity
RAH
|
|
|
|