Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have multiple threads in my program, one of which is operating on an internal data structure. Due to some error, this thread quits leaving the data structure in an invalid state. How can other threads properly validate the state of data structure on later access? In general, how to handle such a scenario?
The best answer is to make sure that threads don't quit leaving the data structure invalid. Other than that, the only solution is something like:
In class:
bool m_data_valid = true; // Or possibly 'false' and set it true in constructor
In mutating thread:
m_data_valid = false;
... // Mutate structure
m_data_valid = true;
In other threads:
if (!m_data_valid)
fixup(); // Or whatever you were going to do.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
I have a Customer data object. That object has to be evaluated by different functions. Each function will return true or false.
Since each function evaluate different case scenarios, I need to submit the Customer object to each one of them.
The way the functions were written, only one of them is going to return true.
(The functions are used in another part of the code, so I am using them as it is, so I don't have to repeat code).
The WorkStatus is an Enum. So, if the function returns true, then the Enum will be set with the specified value.
So:
WorkStatus result = 0;
if (notStarted) result = WorkStatus.NotStarted;
if (started) result = WorkStatus.Started;
if (inProgress) result = WorkStatus.InProgress;
if (withBacklog) result = WorkStatus.WithBacklog;
if (finalized) result = WorkStatus.Finalized;
return (int)result;
I made this way, but I am sure there is some better way. I just can't figure this out.
Obs.: I wrote the question again. I hope it's more clear. Sorry for the previous lack of details.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In my WebApi project I have an endpoint that should delay a small block of code.
Let's make an example:
I need to implement a mechanism that permit the client to book for a resource. The booking time should only have a duration of 120 seconds and then expire.
In terms of code I have something like this:
//booking
foreach (var item in list) {
item.Status = ItemStatus.Booked;
}
await _context.SaveChangesAsync();
//Setup a delayed "thread" which removes the booking
Task.Delay( TimeSpan.FromMinutes( 2 ) )
.ContinueWith( x => {
//loop on the list and set the status to "ReadyForSale"
//if this is still in the Booking
} );
return;
I would like to understand if a solution like this satisfy my requirement. The current thread should not be blocked from the delayed task and I need to find a way to pass the list of items to the delayed task.
I think there is a risk that the task will never run. What if the sever crashes or IIS decides it needs to recycle the pool.
There is the risk that the state would never be restored.
I would probably set a bokedAt DateTime field in the database and then check if that time compared to now is more than two minutes to determine if the item is booked or not. Perhaps even a computed column that checks this and returns the state.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why does Random.Next() have Next in it's name? I know what it does, but the name doesn't seem to correspond to it.
It returns the next number in the infinite sequence of numbers generated from your Random instance's seed.
In computer science jargon, a "generator" is a specific kind of function: one that returns a different result each time it is called. It is traditional to call this function something like next(), because they are often used to return the next piece of a sequence (perhaps infinite). RNGs are just a special case of generator function, returning the next value in a calculated sequence.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What is better method to use for object preparation logic:
a) with return value
List<Users> users = LoadUsers();
users = PrepareUsers(users);
b) or with void type
List<Users> users = LoadUsers();
PrepareUsers(users)
Are you setting properties on existing User objects or are you creating new ones?
If you're simply changing existing objects, then there's no reason why you'd want to return them, it's redundant. Worse, it's misleading - the client will think his objects were left untouched and that you're creating new objects when in fact you're not.
If you're creating new ones, well then, you obviously need to return them.
Alternative b. since you are working with the same user objects, there is no reason to reassign the variable.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My system clock is going crazy randomly at any moment and changing the system clock's date/time to a random one. It's not the lithium battery nor a virus because I checked. Also it's not something from the Windows.System.Time itself.
I want to create a process that will, on an interval, check to see if the system clock's date/time matches the global date/time and if not, it would sync.
I need this to run in the background. I am not even sure if a Windows process is correct way to accomplish this. I am open to any other solutions as well.
Create a new c# empty project. Click on the project and go to Properties change the output type to Windows Application (This will remove the console).
Create a new class example: Example.cs
Write the static entry point eg:
public class Example
{
static void Main(string[] args)
{
}
}
Insert your code in the Main routine.
This will create a process that contains no console/window/service.
I'm guessing this is what you want.