|
How do you remove the "Read-Only" attribute from a file? I've got the following code:code:
|
![]() |
|
![]()
|
# ? Jun 10, 2024 22:23 |
|
You need to apply the changes you made back to the file using File.SetAttributes. There are examples at http://msdn2.microsoft.com/en-us/library/system.io.file.setattributes(VS.71).aspx
|
![]() |
|
Thank you, I think I finally got it.
|
![]() |
|
Here is more a general question that I don't know if there is a rule around. At what point do you leave the connection to a database open for reuse? I know if you are going to be hitting the DB 100 times a minute that its more resonable to leave the connection open instead of closing and disposing it. But what about 10 times a minute? Do you guys have a make or break count?
|
![]() |
|
Fastbreak posted:Here is more a general question that I don't know if there is a rule around. Thanks to connection pooling, you're probably better off just disposing them as soon as you're done. See http://msdn2.microsoft.com/en-us/library/8xx3tyca.aspx
|
![]() |
|
^ ![]() Because ADO.NET manages the connection pool for you, I would suggest that you always create and open a new connection. Ten times a minute doesn't warrant a static or persistent connection, in my opinion. If you are accessing a connection multiple times in a narrow scope, then you can leave it open. With the Compact Framework, I don't believe there is a connection pool, and the overhead can be significant for just about everything. If you happen to be using the .NET CF, then I'd recommend building your own connection pool (or using a single connection if you are sure you won't need to have two open readers at once) and optimizing your queries to reduce round trips.
|
![]() |
|
SLOSifl posted:If you are accessing a connection multiple times in a narrow scope, then you can leave it open. Edit: Rephrasing this... We use a singleton design for our database connection. We can use the connection pool or just manage the open connection ourselves, we prefer the later. But I guess pooling does this anyways whether or not we leave it open, so my question is moot. Fastbreak fucked around with this message at 17:27 on Mar 28, 2008 |
![]() |
|
Fastbreak posted:Thats my whole question, what do you think makes a scope narrow? This is a matter of programmer convenience, not efficiency. Basically if you're doing multiple queries within the same method (or block of a switch statement, or whatever), it would actually take more code and be less clear to dispose of the connection and open a new one than to just reuse the existing one - so reuse it. But if you want to use the same connection for two queries which are more than a full screen of code apart, don't. Your guide should be, "What makes things most clear and uncluttered for the programmer," because a good DB access library will take care of efficiency behind the scenes.
|
![]() |
|
Yeah, I basically meant if you're doing a bunch of sequential queries in a single method. Open Connection -query -query -query Close Connection Because there's no reason to dump the Connection back into the connection pool between queries if you know you're just going to open it again immediately. It's a matter of convenience. If you calling a bunch of queries sequentially in their own methods: DataMethod1 DataMethod2 DataMethod3 I would just open and close a connection in each method. Really, the most important thing is that you close/dispose the connection when you're done so it gets released properly. If leaving it open between queries is confusing or will cause problems if or when you refactor, then don't.
|
![]() |
|
So essentially, keeping a single connection open permanantly has no real benefit or drawback to just using the pool? Never thought of it that way.
|
![]() |
|
Drawback to using a single connection: You can only have one open reader on a connection at a time. A common quick-fix is to add logic to the property to create a new connection and open it rather than returning a specific connection variable. That easily takes care of the creation and opening part, but you still have to update all your code to close it when done, via a using statement or otherwise. In other words, if you ever find yourself needing to run two queries concurrently for any reason, you have to go back and touch everything. edit: vv yeah, the using construct is a great bonus. Plus, the connection pool is automatic - essentially free. You don't have to do anything other than decide not to avoid it. SLOSifl fucked around with this message at 19:44 on Mar 28, 2008 |
![]() |
|
An added benefit to the connection pool is that you can make it habit to always encase the connection in a using block. If you do this for a while, it'll become second nature, and the few cases where you don't do it and accidentally forget to close the connection will stick out more in your code.
|
![]() |
|
SLOSifl posted:Drawback to using a single connection: You can only have one open reader on a connection at a time. Not entirely true. I think you can specify MultipleActiveReaders on one connection in the connection string for sql server 2005.
|
![]() |
|
Huh, I'd never heard of that. The connection string property is MultipleActiveResultSets, and does exactly what it sounds like. It is also a property of the SqlConnectionStringBuilder class. I still think it's a good practice to utilize connection pooling, especially when writing code that you might want to use against a non-SQL2005 database. SLOSifl fucked around with this message at 19:44 on Mar 29, 2008 |
![]() |
|
How do I force a ListBox that's bound to an ObservableCollection<T> to update when one of those collections is updated? They show a new ListBoxItem instantly when a new <T> is added to the collection but I don't know how to update the ListBoxItems that are already there ![]() edit: Solved. You need to implement One Way or Two Way binding for this sort of thing: MSDN posted:The objects in your collection must satisfy the requirements described in the Binding Sources Overview. In particular, if you are using OneWay or TwoWay (for example, you want your UI to update when the source properties change dynamically), you must implement a suitable property changed notification mechanism such as the INotifyPropertyChanged interface. cowboy beepboop fucked around with this message at 13:19 on Mar 30, 2008 |
![]() |
|
this is OT a bit but can someone provide me with a link to the Visual Studio 2008 EULA, or specifically whichever EULA pertains to the Academic Edition? And if you could tell me exactly how you found it that would be great too. I just went googling for half an hour without luck, so my google-fu is pretty weak.
|
![]() |
|
Anyone know of any free graphing controls for the .net compact framework?
|
![]() |
|
I have an object that implements IDictionary. They elements are keyed by string, and the values are all one of these things 1. A string 2. An object that implements IList that is composed of objects implementing IDictionary 3. An object that implements IDictionary 4. A simple address object I am looping through it turning it into a nested XML representation using Xml.Linq. I can get the values from the main dictionary no problem. Is there a non-hackish way to tell if something implements IDictionary so that I can loop through it and create the inner elements? Or tell if it implements IList? The resulting XML should look like code:
I am pretty new at .NET. edit: Ok, figured it out. Its as simple as using the 'is' operator. I was looking for instanceof and knew there had to be something.... Begby fucked around with this message at 02:41 on Apr 1, 2008 |
![]() |
|
I have to say, that's a really awkward way to store your data. What is the benefit of storing everything in an untyped dictionary rather than building your class around the data it contains? If this is just an LINQ experiment, then never mind.
|
![]() |
|
Someone please please explain why this isn't working like I am expecting it to. This page's example is either wrong or I'm an idiot. Byte[] testArray1 = { 0, 0, 0, 0x15 }; Byte[] testArray2 = { 0x15, 0, 0, 0 }; Int32 int1 = BitConverter.ToInt32(testArray1, 0); Int32 int2 = BitConverter.ToInt32(testArray2, 0); the debugger says: code:
![]() It does explain why I was allocating gigabyte sized chunks of ram though. edit: I read the example wrong. gently caress. What kind of sick person reads reads binary that way round anyway ![]() cowboy beepboop fucked around with this message at 09:18 on Apr 1, 2008 |
![]() |
|
SLOSifl posted:I have to say, that's a really awkward way to store your data. What is the benefit of storing everything in an untyped dictionary rather than building your class around the data it contains? This is data from a third party com API that I am accessing via .NET, so I unfortunately have no control over it being untyped. The best part is how you never know what order it will be returned in or how many values / sub-objects will be in it.
|
![]() |
|
Why are my Thread objects leaking memory? I call thread.Start(), they finish, but all sorts of memory is leaked all over the place. My 'Live instances' graph is constantly growing mostly with objects created somewhere after a ThreadHelper.ThreadStart(); code:
Am I not understanding how this works? I'm pulling out hair here, I can't understand why my app is leaking memory all over the place. Do threads need to be explicitly disposed of somehow? This is the allocation call stack for pretty much all 'new live instances' code:
Pivo fucked around with this message at 14:44 on Apr 1, 2008 |
![]() |
|
It may have something to do with fact that you're using an anonymous delegate to fire off the threads, depending on what it's doing with variables around it. Anonymous delegates that reference objects in a method scope can cause problems; the compiler extracts the delegate to class-level and promotes the scope of the referenced objects, which can result in unexpected behavior if you're expecting things to go out of scope immediately when the method exits (which isn't a guarantee in the first place, as C# doesn't have RAII semantics). I might have some of the details wrong, I'm sure biznatchio could correct about every third word in that sentence ![]()
|
![]() |
|
I seem to have fixed it by calling Abort() to ensure the thread is dead before replacing it with another one. edit: Well, that slowed the leak down, but memory usage is still growing, with 99% of new objects being created by that threaded method. How would I replace the anonymous delegate? I need access to instance variables. The thread takes a string and renders it to a bitmap. So, it needs a reference to the object where to place the bitmap, as well as the text string itself... Pivo fucked around with this message at 16:19 on Apr 1, 2008 |
![]() |
|
Pivo posted:I seem to have fixed it by calling Abort() to ensure the thread is dead before replacing it with another one. Instance variables, no problem, just make a method in the same class. But I think you meant method variables, in which case ParameterizedThreadStart code:
|
![]() |
|
Pivo posted:edit: Well, that slowed the leak down, but memory usage is still growing, with 99% of new objects being created by that threaded method. How would I replace the anonymous delegate? I need access to instance variables. The thread takes a string and renders it to a bitmap. So, it needs a reference to the object where to place the bitmap, as well as the text string itself... code:
EDIT It appears to be related to making the DrawingContext call on a new thread. If I call func() directly from my timer instead of creating a new thread with it as its ThreadStart there isn't any leak. Perhaps there's some local thread storage created by WPF that doesn't get cleaned up? Can you use a single thread that waits on an event to create the bitmap? fankey fucked around with this message at 17:57 on Apr 1, 2008 |
![]() |
|
Yes, that's pretty much how I'm doing it. Yes, that's causing my memory leak. Will have to do more research ... edit: No way, can't use a single thread. The problem is that text rendering is so slow that doing it on the main thread causes visible stutter. If you look back in this thread, I've been battling that since day 1. Multithreading it fixed the stutter, but caused the memory leak. I just can't win. Pivo fucked around with this message at 18:13 on Apr 1, 2008 |
![]() |
|
Pivo posted:Yes, that's pretty much how I'm doing it. Yes, that's causing my memory leak. code:
fankey fucked around with this message at 23:21 on Apr 1, 2008 |
![]() |
|
Works beautifully. I know zip about multithreading - AutoResetEvent is new to me! Thanks so much.
|
![]() |
|
Begby posted:This is data from a third party com API that I am accessing via .NET, so I unfortunately have no control over it being untyped. The best part is how you never know what order it will be returned in or how many values / sub-objects will be in it.
|
![]() |
|
csammis posted:It may have something to do with fact that you're using an anonymous delegate to fire off the threads, depending on what it's doing with variables around it. Anonymous delegates that reference objects in a method scope can cause problems; the compiler extracts the delegate to class-level and promotes the scope of the referenced objects, which can result in unexpected behavior if you're expecting things to go out of scope immediately when the method exits (which isn't a guarantee in the first place, as C# doesn't have RAII semantics). Well, and the fact that the only thing that's tied to the scope is something allocated on the stack; everything else falls under the purview of the garbage collector, which doesn't offer any guarantees on when things will be cleaned up -- so relying on things going "out of scope" doesn't work in the first place. And to be precise, anonymous delegates that don't use receive variables from their containing scope are promoted as methods to the class, and function exactly the same as an explicitly created delegate to a method. It's only when variables need to be implicitly curried into the delegate's scope does the compiler build a private class for those variables to live in and the corresponding target method for the delegate. The only real thing I've noticed that you need to be aware of in this regard when it comes to anonymous methods is that there's a performance hit in the anonymous method and its containing scope when using the curried variables due to the fact that they're rewritten by the compiler into field accesses rather than as locals, so you'll lose some JIT optimizations. I'm sure there are some other gotchas, but I can't really come up with any off the top of my head. (I'd suspect potential gotchas with value types.) In any case, looking at the original problem, it's already been answered as a cross-thread issue with WPF. I don't know WPF that well to have pointed to it authoritatively as the problem, but from the code example given that's what I would have suspected right away as well, since there was nothing technically wrong with the code itself that would have caused a leak, assuming GetAsFrozen() is not blocking indefinitely, which would manifest more noticably as a thread leak and less noticably as an object leak. I would, however, raise an objection about using a new full-fledged Thread object for something that looks like it could be done in the ThreadPool or with a self-managed pool instead, for setting what appears to be a cross-thread state variable without locking, and for the apparent lack of reentrancy control on the background task; but all of those objections except the first could be easily explained away by it being a simplified example. ![]() edit: Oh! And a superobjection to the proposed design of using Abort() to terminate the thread! Don't do that! biznatchio fucked around with this message at 23:10 on Apr 1, 2008 |
![]() |
|
biznatchio posted:I would, however, raise an objection about using a new full-fledged Thread object for something that looks like it could be done in the ThreadPool or with a self-managed pool instead, for setting what appears to be a cross-thread state variable without locking, and for the apparent lack of reentrancy control on the background task; but all of those objections except the first could be easily explained away by it being a simplified example.
|
![]() |
|
fankey posted:I'm confused - I don't see how my proposed solution has any of these issues you raised. If there is an issue I'd appreciate it if you could explain - I'm relatively new to C#/.NET. I was referring to the original code, not the suggested alternatives (well, except Abort, which was mentioned).
|
![]() |
|
I have written this method that takes data from an excel sheet and imports it into a DataGridView. The problem is that is skipping over any cell that has text in it. So the cell in the DataGrid is just blank where text should be. I feel like I am missing something simple here, any advice? This is for windows form app using Visual C#code:
|
![]() |
|
Ado.Net (2.0): Is there a way to do a select on a DataTable, that has a DataRelation with another table, such that the filter string is applied to the first table, and order is applied on the second table? The same type of thing you can do when you've grouped tables in SQL. I guess I could just select on the DataTable with a filter, then put each related child row from the resulting rows in a new DataTable and sort that, but that seems pretty ugly.
|
![]() |
|
zero87t posted:I have written this method that takes data from an excel sheet and imports it into a DataGridView. The problem is that is skipping over any cell that has text in it. So the cell in the DataGrid is just blank where text should be. I feel like I am missing something simple here, any advice? This is for windows form app using Visual C# Maybe I'm missing something too, but I don't see a dataGridView1.DataBind(); call anywhere..
|
![]() |
|
Kevin H posted:Maybe I'm missing something too, but I don't see a dataGridView1.DataBind(); call anywhere.. What does DataBind() do?
|
![]() |
|
zero87t posted:What does DataBind() do? It binds data. Maybe the confusion here is because you only have to do it in ASP.NET, and not in WinForms. Or maybe it's the other way around, I always forget.
|
![]() |
|
Does anyone here have a guide on how to set up my home system (WinXP Pro SP2) as an IIS Server with ASP.NET? I can't get ASP.net to work only standard ASP. And I haven't found Microsoft's site to be any help.
|
![]() |
|
![]()
|
# ? Jun 10, 2024 22:23 |
|
I fixed my problem, I just added IMEX=1 to the connection string. It was something small ![]()
|
![]() |