Tag Info

Hot answers tagged

32

This is not a question of performance. It is first and foremost a question of correctness. If you have two lock statements, you can not guarantee atomicity for operations that are spread between them, or partially outside the lock statement. Tailored for the old version of your code, this means: Between the end of the while (_runningWorkers >= ...


24

I am making a little program, which have few Threads, constantly running. At some point, I may want to stop one of them, and then, after random period of time, to start it again. What is the best way to create a thread? The best way is to not do it at all. If I have work that needs to be done in parallel to another program, the best practice is to start ...


11

IMHO you are asking the wrong question - you should not care so much about efficiency trade-offs, but more about correctness. The first variant makes sure _runningWorkers is only accessed during a lock, but it misses the case where _runningWorkers might be changed by another thread in the gap between the first lock and the second. Honestly, the code looks ...


10

In C# it is trivial to provide optional dependency injection without coupling yourself to your dependency too tightly: public class SomeOtherClass { private readonly ISomeClass _someClass; public SomeOtherClass(ISomeClass dependency = null) { _someClass = dependency ?? new SomeClass(); } } (or you can make the constructors explicit if ...


8

There's a development principle along the lines of DRY and SOLID called YAGNI that is designed to help streamline your development efforts in getting things done and not getting paralysed with indecision over what to do. If you later find that you need to enhance your class, then you will. YAGNI says not to worry so much over it now 'cos you probably won't ...


4

Sounds like you can cover this using straight-forward inheritance. Create a base class with the common implementation, and create subclasses that override that implementation with their own, unique behaviour. You can read up on MSDN here. EDIT: I figured I should elaborate with an example. Define a base class that implements the default behavior of your ...


4

In my purview, Object is undefinitive, and belongs in a binary format, not in a human-readable text. I don't see the relationship between these two things; there are plenty of good reasons to choose text versus binary: this is not one of them. How it appears is an implementation detail only. any derived type of Object The biggest challenge, then, ...


4

If you do not apply DI as long as you do not really need it (not even for unit testing), nothing bad will happen. The code does not become error prone, "overly complicated", or hard to maintain. And if you decide to refactor the dependency out later, it will most probably not be much more effort than doing it now. That's a case where the YAGNI principle ...


3

My question is then, how do I know specifically which classes need to be disposed? Does it implement IDisposable? Yes? Then it needs to be disposed. If not, not. You can maybe get away without it in some apps (C# and modern operating systems in general are better about cleaning up after you), but it is still wrong®. There are a very few framework ...


3

The other answers are quite good and clearly address the correctness concerns. Let me address your more general question: How much work should I place inside a lock statement? Let's start with the standard advice, that you allude to and delnan alludes to in the final paragraph of the accepted answer: Do as little work as possible while locking a ...


3

With regards to the two ways of starting the thread, they are roughly equivalent, so it is a matter of whether you prefer a lambda. For stopping the thread, the most error proof way is to check a field value for a change. This can be done using a memory barrier, for example, using a lock statement or a Monitor will automatically provide a memory barrier. ...


3

The web server is saving the data, not the user, so it doesn't matter what the user's time is. The object should be saved with a timestamp in GMT (universal) time format. Save the user's GMT timezone offset, Then add that offset to the timestamp (which is in UTC) to present to the user, his own local time. For example, since I'm in CA, my ...


2

Not so much. Maybe. The problem is that if Al from LA marks something with 1/1/2015 10AM and 7/1/2015 10AM, those will be different offsets from GMT. "What the user expects it to be" can get really dicey given our overly complex way of measuring times. Where at all possible, work using UTC (which may or may not be different from GMT depending on your ...


2

The general case here is identifying the external interface. It should be possible to write an interface for two systems or modules to interact, define what the method signatures are, data formats, preconditions/postconditions, etc. with zero implementation behind it. Next, separate teams work on separate code that use that interface. Perhaps one team ...


2

First, I think this is a perfectly good question. It's an interesting area and i don't get why was this downvoted. You don't need winsock you can disable the network a adapter and change it's settings programmatically... but network issues could be very tricky to simulate this way... ...


2

What you're asking for is functionally equivalent to DRM, which does not work and never can. If you're worried about not getting paid for your work, your best recourse is contract law, not technological measures.


1

Beyond mocking the network interface, you don't need two physical machines for a system test: Disable/reenable the network card using scripts that do ifconfig/ipconfig. use a virtual machine for one end of the system and script it up/down or enable/disable the networking. Hooking into WinSock is way more work than you need to do here.


1

You test these by mocking the client and server and using IP Address 127.0.0.1 Depending on the test you are performing, it could be a simple matter of having the mock-server simply closing its end of the connection after some predefined time and then verifying that the client reconnected within the specified time-frame. It may be difficult to test some of ...


1

Generic answer: My gut feeling says if you have 30 different but very similar things, you should go one metalevel up, have one class for all of them and push the difference completely down to the data. Even if you end up with a nano VM / nano interpreter. Maybe during implementing it, you find out you really only have two or three kinds of them, ...


1

I do not think making decisions about if trial time was exhausted on client is a good idea. This can be easily fooled and can't be calculated with some precision. Since you have a web application, I guess, it would be much better to limit a number of API calls a trial user can make without payment. You can make some tests and map an average number of API ...


1

In most examples online however, this is exactly what's done Not sure how; neither WebRequest nor its concrete descendant HttpWebRequest do in fact implement IDisposable, so this using (System.Net.WebRequest wr = new System.Net.HttpWebRequest()) { //What? } does not compile. In fact what I find when I google around this topic is mostly ...


1

I keep view models in the web project, for reason you stated, it's usually only useful to the relevant view. I'm not sure why your data access layer would reference the web project though?


1

The best way to manage threads is to not do it. In C# we achieve concurrency without direct thread management by virtue of async/await, or sometimes (as mentioned by others) lambda expressions, most commonly via PLINQ. It is expensive, slow and subject to availability to start and stop threads. Instead, the above approaches use a pool of preallocated ...


1

It looks like you are using your observer as both a publisher and a subscriber, while the Provider is only a publisher. Or is it both too? This seems a bit confused. Maybe you should make some kind of Bus or Queue that you can both Publish and Subscribe to. There are so many complexities in messaging that you really should make a distinct component be ...


1

This is a normal restriction in the Windows security model since NT3.x services that only the local service account is allowed access to the desktop when run as a service and it has other restrictions when you select that option. On Windows Server since about 2003, there is also a service interaction policy you may have to adjust. You may have to reboot the ...



Only top voted, non community-wiki answers of a minimum length are eligible