Jump to content
Xtreme .Net Talk

snarfblam

Leaders
  • Posts

    2156
  • Joined

  • Last visited

  • Days Won

    2

Everything posted by snarfblam

  1. I personally don't find much need for prioritizing when it comes to optimizing my code. I've adapted a programming style where as I code I evaluate the potential for unacceptable use of resources (disk space, RAM, CPU). I am also a minimalist, preferring to keep things as simple (which is not the same as "easy") as possible. I often use structs instead of classes when there is no impact on maintainability or readability (saves on RAM and garbage collection). I often use simple binary formats for data because they are more compact and, in some ways, less error-prone than human readable formats. Don't get the wrong idea. I understand the need to find a balance between the time it takes to complete the project, maintainability, and the level of optimization. I am not micro-optimizing. I am simply diligent and conscientious when it comes to coding. So, when does performance become a real issue? Simple. When it impacts the user's experience. Speed optimizations are of relatively little importance until you encounter a bottleneck and you find yourself looking at a noticeable lag or unusually slow progress bar. For example, I made a post here quite some time ago explaining that constructors are an order of magnitude slower than a normal instance method when it comes to initializing a struct. Well, even though I deal with structs much more often than your typical C# programmer, I still use constructors 99% of the time. If I need to initialize an array of structs and the GUI freezes for a second when this happens, then I use a non-constructor initialization method.
  2. snarfblam

    Now

    Did you use the correct port? The example used port 13. Most time servers use port 123.
  3. Wow, a thread about me. I must be special. Or something. To be honest, since I've started trying to use the Dvorak keyboard I've moved and changed computers for a while. I haven't had the time and patience to use Dvorak lately, and, likewise, I haven't had the time or inclination to change my signature. But now that I have things back in order, I think I'm going to give Dvorak another go. The first two days were a little difficult, but within a week or I was typing dvorak at a third of the speed I could with QWERTY, and having used it in the past, I think it will probably be easier this time around. The idea, of course, is to eventually be able to type faster and more easily... eventually. And to show people that I'm hardcore.
  4. How are you examining the pixels?
  5. snarfblam

    Now

    Would this help? Connecting To A Network Time Server It's in C#, but even if you program in VB it should be easy to translate.
  6. Re: Remove and insert items I tend to apply concepts such as scalability, extensibility, abstraction, etc. with discretion: only when I foresee a need. It is enough effort to consistently write code that is well formed and self-explanatory (or commented). Most of the time, I simply focus on getting the job done. Otherwise I tend not to get the job done. Perhaps this is due to differences between commercial programming and hobby programming. Ideally, all code would always be extremely scalable, just in case, but you can't cover every single base or over-analyze every aspect of every bit of code. Otherwise the project never gets finished. You seem really intent on your O(n) versus O(1) point. The concept is absolutely correct and certainly has merit. I don't argue that. If someone said, "Hey, you, there, programmer guy, which would you prefer? O(n) or O(1)?" Well, then, yes, it is a no brainer. But that's not what's happening. Someone is writing code, and anything that works in all foreseeable, reasonable, realistic cases is fine by me, even if it does not satisfy every ideal of computer science. When your listbox begins to contain hundreds of thousands of items bottlenecks will pop up everywhere. How items are moved up and down the list will be the least of your problems. Am I saying that your solution is not better? Of course not. I'm saying that it is better in a way that probably won't matter, based on a concept that is not really truly relevant (not because it can not be applied, but because it applies only beyond the domain of reality). And what I am really getting at, what this really breaks down to, is that what you are touting is micro-optimization, and there is no need to do it. Frankly, it is hardly worth all this discussion for something particularly trivial. It just frustrates me when I reiterate that I am not arguing with the concept, but rather with the conclusion, and I just keep getting the concept explained back to me, as though I must not understand it because I didn't reach the same conclusion. I've shared my two cents, and then some, and I think I've thoroughly explained my logic. Feel free to rebut, but I think there is more than enough information and opinion expressed here, and I'm done. These days it seems I spend more time discussing and less time doing.
  7. Re: Remove and insert items Mskeel, I understand that a linked list makes for quicker insertion. I wasn't disagreeing. But now that we are on the topic, when you say:
  8. Like you said, the problem is that you are running this code in Form_Load, when the Form object has already been created and handles allocated. (I also don't recommend using Application.Exit; if this.Close doesn't work, it is an indicator that something else is wrong with your approach.) This code should be executed in your application's Main function.
  9. Re: Remove and insert items ListView.ObjectCollection stores its items internally within a ListView.ItemArray, which, in turn, stores items in an array of ListView.ItemArray.Entry objects which stores your actual item and a small amount of data for managing the entry. Or, to put it simply, the items are ultimately stored in an array. Regardless, these sort of operations do not become particularly "expensive" until they are performed an enormous number of times or they are performed on extremely large arrays. What you are really looking at here is a very small bit-block transfer for each insertion. What would concern me more would be the UI and Win32 updates that have to be made for each insertion which could very well be slow and ugly, regardless of the internal representation of this list.
  10. Why not just select the last item each time you add an item... listBox1.SelectedIndex = listBox1.Items.Count - 1;
  11. See Re: Passing in a string to a thread I am about to start? [C# 2002].
  12. I found one answer to this question in the intellisense that popped up when I typed new Thread(. Assuming it won't be a problem to change the signature of Operation1, try this: C# Code[HorizontalRule]magic hidden text[/HorizontalRule]{ ···string sType ="Update"; ···// Create a thread that accepts a parameter ···Thread th1 = new Thread ( ······new ParameterizedThreadStart(Operation1)); ···// Execute thread with specified parameter ···th1.Start(stype); } // method signature has been changed private void Operaton1(object oType) { ···// String comparison has been modified ···if ("Update".Equals(oType)) ···{ ···} ···else ···{ ···} }[HorizontalRule]Why are you quoting me?[/HorizontalRule] Also note that the object passed to the thread doesn't have to be a string--it could be an object that specified any kind of data you'd like. You could even tailor a class that the invoked thread can write data back to in order to return data from a thread.
  13. There is rarely a situation where a DoEvents loop is your best solution, especially with threading. Shaitan, why don't use you use a BackgroundWorker component. If you want to do something when the operation is complete, you can use the WorkCompleted event of the BackgroundWorker. This method would leave your UI thread completely free to go about business as usual. [Edit]Just saw a post you wrote saying you are using dotnet 1.x... oh well, sorry. You could write a method that would be invoked on the UI thread from a background thread when the operation is finished.[/edit]
  14. Making a file broswer in C# is tricky; I've tried it. One problem is that using the built in ShellIcon class you have no way of knowing when icons can be re-used and you end up allocating a massive amount of duplicate icons, which takes time to load and puts a large burden on the garbage collector. A better, albeit very much more complex solution would be to use the Windows API, which provides a method of getting the icons that can avoid loading duplicates. You would have to do some research, however, because I don't have an example and I don't know the specifics. Another recommendation might be to avoid using the DirectoryInfo class (sticking with the simpler Directory class might be faster and easier on the GC). When I tried to make my own browser, what I found most effective for speeding things up was, firstly, using a background thread that pre-cached data before the user requested it, and secondly, rendering with GDI instead of GDI+ (I was doing my own rendering).
  15. You can import the CompareFileTime function from the Windows API to use it in C#. C# Code[HorizontalRule]magic hidden text[/HorizontalRule][DllImport("kernel32.dll")] static extern int CompareFileTime ( ____ ref FILETIME lpFileTime1, ____ ref FILETIME lpFileTime2); [HorizontalRule]Why are you quoting me?[/HorizontalRule] Note that the reference operator in C++ becomes the ref keyword in C#... C# Code[HorizontalRule]magic hidden text[/HorizontalRule]if (-1 == CompareFileTime (ref ftMostRecent, ref ft)) { ____ // ... do something ... // }[HorizontalRule]Why are you quoting me?[/HorizontalRule]
  16. ListBox does not support this feature out of the box. In order to achieve this result you would have to owner draw your ListBox.
  17. I was going to make that recommendation, but I was kind of hoping that someone would post an example that involved something like overriding WndProc to intercept the appropriate message. The OwnerDraw feature is intended for rendering the UI, not broadcasting events. It is unlikely that it will ever become a problem, but the concern would be that a future version of DotNet would implement owner drawing differently in a way that would render this technique malfunctional (i.e. if the drawn combo box items are chached and don't need to be redrawn). That being said, PD's suggestion is probably still the way I would go, but mostly out of laziness.
  18. Just for future reference, I believe you can also use the TypeOf...Is operator to see if an object is an instance of a class or interface, as in: [Color=Blue]Public Function[/Color] IsDisposable(o [Color=Blue]As [/color][Color=DarkRed]Object[/Color]) [Color=Blue]As[/color] [Color=DarkRed]Boolean[/Color] [Color=Blue]Return[/Color] ([color=blue]TypeOf [/Color]o [Color=Blue]Is[/Color] [Color=DarkRed]IDisposable[/color]) [Color=Blue]End Function[/Color] [/Code] If you need code to be more dynamic, you can use reflection. [Code] [Color=Blue]Function [/Color]DynamicTypeCheck([Color=Blue]ByVal [/Color]o [Color=Blue]As[/color] [Color=DarkRed]Object[/Color], [Color=Blue]ByVal [/Color]t [Color=Blue]As[/color] [Color=DarkRed]Type[/Color]) [Color=Blue]As[/color] [Color=DarkRed]Boolean [/Color] [Color=Blue]Return [/Color]t.IsInstanceOfType(o) [Color=Blue]End Function[/Color] [Color=Green]' Code Example[/Color] MessageBox.Show(DynamicTypeCheck(someObject, [Color=Blue]GetType[/Color]([Color=DarkRed]IDisposable[/Color])))
  19. I'm guessing that Form1 might be the application's main form. If this is the case, then F.Show() won't work. (Hence the ShowDialog?) If I'm right then there is a right way to approach the situation, and "the other way." What you should probably do is write a custom Main() for your program. How this is done might vary from version to version with VB (I haven't used newer versions). You would also need to inherit the ApplicationContext class and write your own custom ApplicationContext that manages your forms for you. The "other way" is to simply hide your Form1 before showing Form2, but this prevents resources from being released in a timely fasion and is not the preferred technique.
  20. shyampc, this is a programming board. The Graphics and Muiltimedia forum is still limited in scope to DotNet programming. As far as your problems with photoshop... 1. Is the layer locked? Is Edit>Clear disabled? 2. If you open or paste an image and it appears to be grayscale, you are probably working in grayscale mode. Go to Image>Mode, and select RGB or CYMK.
  21. Amir, how could one check to see if a string contains one of 800 values without iterating through those values? Not that it can't be done, but is there an algorithm or technique you would propose?
  22. Hmmm... the "copy" constructor was Microsoft's recommended technique. I'm not against use of the ICloneable interface, but unless you need to use this class in some abstract manner, neither approach has any merit over the other. I would say go with what's most intuitive.
  23. I don't know if there is a good workaround. You could try to hack your own. You can get and set the scroll position of a scrollable container, so you might be able to monitor the position and correct it when the undesired scrolling occurs, but you will almost certainly see some undesirable flicker. There is another "feature" that you might run into when working with this, too. When you read the autoscroll position I believe you will get a pair of positive coordinates: the position of the viewport (top-left corner). When you set the autoscroll position, for no particular reason, the coordinate values should be negative: the offset of the controls' positions. Confused?
  24. I think this bug may be a "feature." I've found many complaints about this behavior when searching google and MSDN, but the best answer I could find was "that's just how it works."
  25. DotNet does not provide a mechanism to clone objects for you. You could write a function to automate it for you using reflection (assuming you have reflection permission), but even this can be problematic (which is why C++ has copy constructors). Shallow copies will often cause conflicts with shared resources and deep copies can not be automated. Things can get hairy when you are creating memberwise clones of objects, because objects reference other objects, and the DotNet runtime has no way of knowing which referenced objects should also be duplicated and which ones should not (instead, they would only have their reference copied). For instance, if you wanted to copy an instance of your class, strings should probably not be duplicated. If you wanted to copy an instance of a Form object, controls would have to be duplicated (you can't use the same controls on two Forms at once). What's more, unmanaged resources would have to be allocated for the duplicated controls. There is no reasonable way that DotNet could possibly sort this all out for you, which is why they don't provide a built-in function to copy objects. For something that seems so simple, copying objects is actually very complex and has to be done on a case-by-case basis, which means that you have to do it yourself. Your best bet is to create a C# style "copy constructor," i.e. your constructor that accepts an object and clones itself from that.
×
×
  • Create New...