|
|
Just out of curiosity, did you read the post? Did you feel it answered my question? I did not...
This blog posting predates the book I was referring to (Practical Guidelines and Best Practices (for MS VB and Visual C# Developers, Microsoft Press, 2005). It appears to merely restate the problem - do validate the arguments, don't use IsDefined . Although, it does add some reasons why IsDefined is bad...
What is the alternative, then?
|
|
|
|
|
About all you could do is test the contiguous ranges, so suppose you had the following defined:
public enum MyEnum
{
Enum1 = 0,
Enum2 = 1,
Enum3 = 3,
Enum4 = 10,
Enum5 = 20
} You could do this as
if (myValue >= (int)MyEnum.Enum1 && myValue <= (int)MyEnum.Enum3 || myValue == (int)MyEnum.Enum4 || myValue == (int)MyEnum.Enum5)
{
} It's not pretty but it would work.
|
|
|
|
|
Heh. My enum has 68 members. Basically, I'd have to loop through them all, checking equivalency. This was a solution I already passed on once...
Why oh why didn't they implement the InvalidEnumException, I will never know (yes, I know there's one in System.ComponentModel, but you can see right in the article that MS doesn't want you using that one, either).
Sigh. Write my own.
|
|
|
|
|
I am developing a program running on mobilephone using c# 2005,when my program is running,I need to know if the mobilephone is downloading or uploading data from internet online,how to do it?
|
|
|
|
|
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
|
|
|
|