|
Not sure. But I would try to watch packets and see if they are being transmitted or received.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
how to watch if the packets are being transmitted or received?
|
|
|
|
|
Through the networking classes already built into the framework.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Check whether or not the power is on?
|
|
|
|
|
I'm hoping someone can help on this one as it has been driving me mad for most of the day now...
In my application I have a panel with a number of dynamically generated controls (TextBoxes and RichTextBoxes, generated from xml) and I'd like to be able to drag these controls onto another panel. My question is rather simple (I hope) - is it possible to assign an event handler at runtime so I can perform the desired D&D and how would I go about it (any pointers would be most welcome )?
P.S. I've only been programming in C# for about 8 weeks (and liking it so far) so apologies if this is a stupid question...
|
|
|
|
|
Yes, it is possible to programatically assign event handlers to controls. I have whipped up a quick example below. Hopefully you'll be able to tweak the code below to suit your needs.
<br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
TextBox textBox = new TextBox();<br />
<br />
textBox.TextChanged += new EventHandler(this.TextBox_TextChanged);<br />
<br />
this.Controls.Add(textBox);<br />
}<br />
<br />
private void TextBox_TextChanged(object sender, EventArgs e)<br />
{<br />
}<br />
|
|
|
|
|
Jeez - simple when you know how! I can't believe I was toiling over it for that long! Thanks for your help J$ & SK Genius!
I can sleep at last!
Thanks again!
|
|
|
|
|
Hi, if you can do it with Visual Designer, then you can have a peek at the code it generates
and do something similar. There is no magic involved, the Designer just emits some code.
|
|
|
|
|
Event handlers are always asigned at runtime really...
You just have something like:
this.control.OnClick += new blahblahblah(someMethod);
Of course you will need to have already written the method you assign to the event. To get the exact code for an event and its method (as in what parameters you need, and what derivitive of EventArgs you need), add the desired event to any control, pop over to the designer generated code (probably in *filename*.designer.cs) and you will be able to see the code there, just Ctrl+F the name of the control you used.
My current favourite word is: Bauble!
-SK Genius
|
|
|
|
|
I'm writing a service that watches a folder for new files, and kicks off a thread to process the file. The problem is that two threads are being created at the same time (for the same file), and I have no idea why.
Here's the initialization for the watcher object:
string folderToWatch = "C:\\testfolder";
folderWatcher = new FileSystemWatcher(folderToWatch);
folderWatcher.BeginInit();
folderWatcher.EnableRaisingEvents = false;
folderWatcher.Filter = "*.xyz";
folderWatcher.IncludeSubdirectories = false;
folderWatcher.NotifyFilter = NotifyFilters.FileName;
folderWatcher.Created += new FileSystemEventHandler(folderWatcher_Created);
folderWatcher.EndInit();
The folderWatcher is started when the service is started.
Here's the handler for the Created event:
private class ThreadData
{
public string m_fileName;
public ErrorHandler m_errorHandler;
public int m_threadID;
}
private void folderWatcher_Created(object sender, FileSystemEventArgs e)
{
ThreadData data = new ThreadData();
data.m_fileName = e.FullPath;
data.m_errorHandler = this.m_errorHandler;
Thread thread = new Thread(new ParameterizedThreadStart(UploadFile));
thread.Name = JustFileName(e.FullPath);
data.m_threadID = thread.ManagedThreadId;
thread.Start(data);
}
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
You can't rely on only one create event per file created. This behavior is not deterministic between applications.
Notepad, for example, tests to see if a file exists by trying to create it first. Then it deletes it, and then it creates it. So you can get a bunch of the same FSW events for the creation of a single file.
By the way, I learned this by reading some article here on codeproject, as I recall . In the comments on the article, people were also talking about a horrible memory leak their FSW was causing, and nobody had a solution for that, either.
|
|
|
|
|
In fact, the Net is rife with examples of your same problem. Trying to find the article I saw before, I googled "FileSystemWatcher Notepad" and got many hits, talking about the multiple creation event problem. Here's an example:
http://community.bartdesmet.net/blogs/bart/archive/2004/10/21/447.aspx[^]
Note that a google of "FileSystemWatcher multiple creation events" would have turned up an even better review of articles on this.
Some of the articles/blogs/text linked claimed to have solutions, also.
|
|
|
|
|
|
So what's the solution?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
I don't know. Read the articles?
|
|
|
|
|
Yes, I read the articles. No, no solutions were posted in them. I already have something in mindm, but I wanted to see what others did to resolve it.
In my particular case, all of the files are guaranteed to have unique file names, so I can maintain a list of the last 100 files that have already been processed, and ignore duplicate events on the same filename, but that doesn't address some of the other issues cited in the articles you listed.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Well, one solution would be to read the articles . I did not.
Judging from what I am seeing in your code, I suggest that you check to see if you already have a thread going for that file, and terminate.
Basically, this is a design issue. The world is such that you can't count on a single create event. So, if you don't want multiple threads going, you're going to need to do your own checking.
|
|
|
|
|
I have two .csv files .. one is a filter content and the other is national data. The national data file crashes when Excel tries to open it BUT if my PM "uses Access" he can open all of the data.
I used a StreamReader and did an inline ReadLine() but that got the same result as Excel -- out of memory error. Then I read that if you get an out of memory failure using ReadLine that using the more robust Read() will resolve the issue.
I changed my code to use an inline method called GetLine() so that I didn't change my code too drasitcally and ran a test. It still runs out of memory. (code is below) Anyone have any ideas on how I can get past the I/O failure?
private string GetLine(StreamReader reader)
{
if (reader.Peek() < 0) return null;
List<char> buffer = new List<char>(limit);
while (reader.Peek() >= 0)
{
buffer.Add((char)reader.Read());
if (reader.Peek() < 0) return Debuff(buffer);
if (buffer.Count > 3 &&
(buffer[buffer.Count - 1].Equals('\r') && buffer[buffer.Count - 2].Equals('\n')) ||
(buffer[buffer.Count - 1].Equals('\n') && buffer[buffer.Count - 2].Equals('\r')))
return Debuff(buffer);
}
return Debuff(buffer);
}
|
|
|
|
|
First question how big does buffer get?
|
|
|
|
|
Well, believe it or not your question solved my problem. I added a try{}catch{} and found that the buffer exceeded 256MEG in size. Much larger than any record in the file could possibly be. So I traced the characters and found that it only contained a \n by itself at the end of each line.
Found many other inconsistancies in the file (like quoted numbers) but the application is now processing the file without exceeding memory!
Thanks for the help.
Michael
|
|
|
|
|
The advice that you received that the Read method is more robust is due to the following: The readline method goes until it finds a newline character. If you have a 2,000 GB file with no newline characters, then ReadLine attempts to read it ALL into memory (which is clearly going to throw an out of mem exception). The code you provided above is using Read, this is true. But you are still getting the same exception because you basically just reimplemented the same functionality, and are still trying to load a 2000 GB file into memory. The reason why Read is suposedly "safer" is because typically, you only need a portion of the file at any given time. Read into memory only that which you need, or at minimum put a limit to the file size you can handle. Hope this helps,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
The following code is generating files perfectly, But when i change the date on my laptop to get files with different modified times. It just overrides all the files with the recent date.
private void GenerateTestFiles_Click(object sender, EventArgs e)
{
StreamWriter outStream = null;
string message = null;
for (int i = 0; i < (100); i++)
{
string filename = "output";
filename += i.ToString();
if (i % 3 == 0)
{//multiple of three
filename += ".txt";
}
else if (i % 5 == 0)
{//multiple of 5
filename += ".log";
}
else if (i % 7 == 0)
{// multiple of 7
filename += ".exe";
}
else if (i % 9 == 0)
{// miltiple of 9
filename += ".dat";
}
else if (i % 11 == 0)
{// multiple of 11
filename += ".jpq";
}
else
{
filename += ".bmp";
}
FolderWatcher fw = new FolderWatcher();
filename = fw.FolderPath + "C:\\CsharpTempFolder\\" + filename;
try
{
outStream = new StreamWriter(filename);
outStream.WriteLine("Hello World");
outStream.Close();
}
catch (Exception Ex)
{
message = "Unable to create " + filename + "\r\n";
message += String.Format("Reason: {0}", Ex.Message);
MessageBox.Show(message);
}
}
}
1. Can i generate files with random numbers. Like, in the loop i takes random values using System.Random class. Or, add datetime object. what is the most simpliest way of doing it.
|
|
|
|
|
Wouldn't it be easier to just use Path.GetRandomFileName[^]?
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
pmarfleet wrote: Wouldn't it be easier to just use Path.GetRandomFileName[^]?
What the?!?... i can't beleive thats actually a built-in method. Why would that be there?
My current favourite word is: Bauble!
-SK Genius
|
|
|
|
|
SK Genius wrote: Why would that be there?
Because .NET rocks.
Seriously, the purpose of this method is to provide the means of generating a random directory/file name that is cryptographically secure, i.e. that could not be guessed at by a malicious script.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|