|
The other day I posted about a problem[^] with FileSystemWatcher raising events before the file is completely copied into the watched folder.
Thanks for Gerry Schmitz[^] I think I've come up with a workable solution:
I would appreciate any comments on this idea.
[1] The FileSystemWatcher detects the file. It's a large file that take a while to get fully copied into the folder:
private void OnCreated(object sender, FileSystemEventArgs e)
{
if (HasAnotherFileEventOccuredRecently(e.FullPath))
{
return;
}
QueueFile(e);
}
The HasAnotherFileEventOccuredRecently method is called to see if this is the first time a FileSystemWatcher event has been raised for the file. Multiple events are raised for the file, so if any events for the file have already been called, then ignore them. I didn't include this code for brevity.
[2] Next QueueFile is called:
private void QueueFile(FileSystemEventArgs fileSystemEventArgs)
{
var worker = new BackgroundWorker();
worker.DoWork += Worker_DoWork;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
_queue.Add(worker);
worker.RunWorkerAsync(fileSystemEventArgs);
}
The BG Worker snippets...
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
var args = (FileSystemEventArgs)e.Argument;
if (args != null)
{
while (!IsFileAvailable(args.FullPath))
{
}
}
e.Result = args;
}
and
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
var args = (FileSystemEventArgs)e.Result;
var worker = _queue.FirstOrDefault(x => x == sender);
if (worker != null)
{
worker.Dispose();
_queue.Remove(worker);
}
this.OnCreated(args);
}
[3] and finally the IsFileAvailable method:
public bool IsFileAvailable(string filename)
{
var results = false;
try
{
using (var inputStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
{
results = inputStream.Length > 0;
}
}
catch (Exception)
{
results = false;
}
return results;
}
If it's not broken, fix it until it is
modified 22-Feb-16 16:59pm.
|
|
|
|
|
|
Am trying to get a list of all sql sever instance in my network using this code but i get an empty list
var dt = SqlDataSourceEnumerator.Instance.GetDataSources();
to verify i put in CMD this "sqlcmd -L" and "osql -L" and i get the list.
so what am doing wrong, i dont know if OS matter am using Win10. Also someone know how to do this using dlls thar sqlserver have?
thanks.
|
|
|
|
|
The code I use is pretty much identical:
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
DataTable dt = instance.GetDataSources();
foreach (System.Data.DataRow row in dt.Rows)
{
Console.WriteLine("ServerName = {0}", row["ServerName"]);
}
I just tried it under Win 10, and it works fine for me.
What does the debugger say?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
i use your code now and it's the same result.
DataTable dt = instance.GetDataSources();
dt.Rows.Count = 0
any idea why?
|
|
|
|
|
How's your network? Can you see any instances in the VS Solution Explorer pane or SSMS?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
as i said before i run this command in CMD "sqlcmd -L" and i get the list, in this case for now just found my server. In "SSMS" and "VS server exporer" i see my server too.
|
|
|
|
|
Very odd - sorry I can't help further, but as I say, it works for me here under Win 10.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
ok man, thanks for your help.
|
|
|
|
|
You're not alone:
Two of the bug reports have been closed as "fixed", but there's no comment from Microsoft on how to obtain the fix.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
At least that explains why it worked here - the app I was testing in targets .NET 3.5.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
As I was putting together a reply to a QA question today (which, imho, implied using a Stack as a DataSource): [^].
Thinking about what may actually happen under the hood when you re-bind a DataSource to a content source so that a databound Control refreshes at run-time, like this: (use of 'DisplayMember and 'ValueMember left out here)
// refresh
bindSource = new BindingSource(); // assume 'bindSource is declared
bindSource.DataSource = StringStack; // assume a valid Stack exists
listBox1.DataSource = bindSource; // yeah, a ListBox is alive
reminded me that I have never been able to find any information on whether this is an "expensive" operation ... and I don't have the hip-high waders and anti-snake-bite anti-venin needed to go into that dismal swamp of CLR/IL.
I'd appreciate your thoughts on the possible memory/computation time associated with this type of operation.
«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.”
modified 22-Feb-16 9:04am.
|
|
|
|
|
You've posted the link to edit your answer, rather than the link to your answer[^].
I'm not sure it makes a lot of sense to ask whether an operation is "expensive" without specifying what you're comparing it to. Everything is expensive if you're comparing it to doing nothing.
There's no need to dig into the IL - the source code[^] is available, along with all of the comments. At a quick glance, the DataSource property setter[^] calls the SetDataConnection method[^], which eventually calls the SetItemsCore method[^]. There's quite a lot going on in there, but nothing that looks particularly horrendous.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Link is fixed, thanks, Richard,
While I appreciate the level you are at, where you can look at .NET source and make informed inferences about potential use-cases, I'm not there, and don't even want to go there ... perhaps if I were forty years younger ...
«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.”
|
|
|
|
|
|
The very same way you added the hidediv() function, add an other function to remove read-only...
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
When you have a question about an article, the best place to ask your question is in the forum at the end of that article. There's very little chance that the author is going to see your question in a completely unrelated forum.
This space for rent
|
|
|
|
|
private void Image()
{
string cmd = "/c " + " ImageRegionIterator.exe" + " output.dcm";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = cmd;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
richTextBox1.Text = process.StandardOutput.ReadToEnd();
}
|
|
|
|
|
And what is wrong with what you have done?
|
|
|
|
|
Try adding a line after Start:
process.Start();
process.WaitForExit();
richTextBox1.Text = process.StandardOutput.ReadToEnd();
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
An example: I create a list of #2880 'PointF instances which represent points on a hypothetical circle with a radius of #1000 units (360 degrees "sampled" at a "frequency" of 0.125d). I serialize this List in compressed form to a file, and write code to access it, and de-serialize it, on demand. All this, easy to do.
Then, as I want to use a set of points for a circle of a different size radius, I can open the file, de-serialize to a new List<PointF> ... no problem. And, of course, I can then "down-sample" that data to create a smaller list of PointF, that would, in effect, give me data for a "smaller" circle.
If I had a set of data too large for available memory, I could, of course, do a a file read where I down-sample by some factor, importing only the data set whose size would be usable given available memory.
But, assuming the whole-banana can be de-serialized into available memory, what's the best practice (faster, less memory use) for "transforming" the "master list of PointF" ... where that transform may involve both scaling and translation ... but, let's exclude rotation.
Possible strategies ?
1. foreach loop, and apply translation and scale per PointF
2. use a Matrix to achieve transform. Using this code now:
private static PointF[] TransformPtF(List<PointF> pts, Matrix mtrx)
{
PointF[] result = new PointF[pts.Count];
pts.CopyTo(result);
mtrx.TransformPoints(result);
return result;
} 3. use 'unsafe code ... perhaps requiring "master data" to be saved in a different format ? byte-array ?
4. use Linq 'ConvertAll with a custom converter ?
5. or ?
Appreciate your thoughts.
thanks, Bill
«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.”
modified 21-Feb-16 0:40am.
|
|
|
|
|
I'm not sure wether this will help, but, have you considered a memory-mapped file for accessing your data. That would give you speed to do any sort of processing (possibly more aligned with your point #1) while the system 'pages' the data to and from memory and the file on disk - transparent to you .. maybe this is "5. or ?"
|
|
|
|
|
edit: there's some code here that's suggestive: [^] : perhaps binary serializing the PointF data as one big Byte Array is a strategy, then reading it into a memory stream, is a strategy.
Thanks, Garth, I'll look for an example of that. And, I haven't explore the idea of de-serialzing the "master list" of PointF to a memory stream and working with that ... not sure if I could somehow use Matrix.Tranform there.
«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.”
modified 21-Feb-16 2:11am.
|
|
|
|
|
When it comes to situations where I need to play with streams of data, I tend to go to Rx as my default grab bag of tools. Basically, Rx (Reactive eXtensions) is a very clever implementation of the Observable pattern that allows you to work with a Linq style syntax and do operations such as throttling, transformation and so on.
This space for rent
|
|
|
|
|
This should probably be a separate question, but I often find myself in a position where I want to change the threads of the subroutine that is called.
For instance, say I want the points created by MouseMove it's quite easy to get them observing on the UIThread:
var Moveing = Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove").Select(evt => evt.EventArgs.GetPosition(this));
Moveing.Subscribe(evt => {
this.Title = evt.ToString();
});
But after 1 minute assume that I want to calcualte some long running prossess off the UIThread, like the avarage posision of the mouse for instance, and when completed post the result back to the UIThread. The ObserveOn or SubscribeOn didnt quite seem to fit, as I do already ObserveOn the UI thread in this case. I could use the method SubscribeOn but that seemed to imply that the subscribed code ran in a different thread, and I coudnt really post the result on the UI thread.
I was hoping that the Rx would make this so easy for me, but it's giving me some headaches insted.
|
|
|
|