Implement your own Thread in C# [closed] - c#

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 8 years ago.
Improve this question
I was asked at an interview today about how I would implement my own Thread. As a hint I was told that implmenting Runnable was one thing to consider, and what are the others?
I was completely stumped but even after researching it online, I still have no idea what the answer is or if it was even a valid question. I'm leaning towards the latter.
So my question is:
What things would you need to consider if you wanted to write your own implementation of the Thread class in C#?

First up, Runnable probably refers to Action. Action is a delegate type used to store a reference to a method. You use this to tell a thread what method to start on.
I expect they only wanted you to illustrate how to;
start a thread with a delegate (that is how Runnable enters the fray).
-or-
schedule work to be performed on a worker thread. I would use a call to Task.Run(new Action(....)) for this. It is concise and modern.
I would always do the latter until instructed to detail the lower level Thread class.

Related

Modify method logic MOQ [closed]

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 4 years ago.
Improve this question
I am new to unit testing so my questions might seem basic, apologies for that.
I am trying to figure out if there is such API in MOQ that can help me to modify a method logic.
I mean,
When calling . I want to increase lets say my "count" variable by 1 and then call the method or do something else, doesn't really matter.
There is no code yet, this is a principle question. Could not find it in the MOQ Documentation on GitHub.
Hope I was clear and you can help me with that.
Thank you!
You can't 'modify' the method logic per se, but you can replace it entirely.
When you create a mock of an interface, you aren't instantiating a specific concrete type of that interface. Instead, you are allowing your mocking framework to create a 'mock' version of that interface; an object which, by default, has no functionality but requires no work to instantiate.
The easiest way to do this is to use Moq's Setup functionality, which is explained well here:
https://github.com/Moq/moq4/wiki/Quickstart

Real world explanation of using ConfigureAwait to false in c# [closed]

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 5 years ago.
Improve this question
I am learning async/await and I came across the blog in which it's mentioned about using ConfigureAwait with async/await. It read's like this:
ConfigureAwait accepts a Boolean continueOnCapturedContext parameter: passing true means to use the default behavior, and passing false means that the system doesn’t need to forcefully marshal the delegate’s invocation back to the original context and can instead execute the delegate wherever the system sees fit.
The information does not tell much in detail, can anybody explain the real world example of using it. I also searched further and found out that it should be used with HTTP calls and such, but didn't got concrete answer for why should we use it.
Reference link: https://blogs.msdn.microsoft.com/windowsappdev/2012/04/24/diving-deep-with-winrt-and-await/
This is useful for scenarios where a single thread handles multiple actions, think Dispatcher thread in WPF or the host thread in IIS.
It is most obvious in Asp.Net (on Windows and full .net, hosted in IIS) -> if you do not specify .ConfigureAwait(false) and the request takes a significant amount of time, no other requests can be processed by the same w3wp.exe process.
The whole app is essentially blocked.
What this is doing is saying that the control can return to this stack using another thread from the threadpool, essentially unblocking the main thread.

AutoResetEvent and await [closed]

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 6 years ago.
Improve this question
I want to wait for an event signal from another thread without blocking the waiting thread. Ideally using await.
I thought of this solution:
await Task.Run(() => myAutoResetEvent.WaitOne());
I would like to know if there is something conceptually wrong with it and if there are better alternatives
Thanks!
What is wrong with doing this?
You're using a thread pool thread just to wait for a "signal".
what is a better alternative?
You can use an async-compatible AutoResetEvent, such as the one in my AsyncEx library or the one on Stephen Toub's blog.
Also, most of the time auto reset events are the wrong thing to use. manual reset events are much more common. I'd recommend re-evaluating this choice of synchronization primitive.

Group Box in different Thread [closed]

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 8 years ago.
Improve this question
I have 5 Group Box in my software I want to run them differently for example I have one group box which takes lot of time around 20-30 minutes till then my software hangs. SO I want to make other functionality working when a particular group box is working any solution for it?
Group boxes don't "work". If they're just some GUI for a resource intensive task, you probably want to run that task asynchronously (or multi-threaded, depending on your requirements) and only use the GUI for settings / updates / whatever. The key is to never do much work on the GUI thread - if a GUI action takes more than say 50-100ms, it should probably be done elsewhere. Note that using await makes this very easy to do properly - if that's not available, a BackgroundWorker is probably the best option. Do note it doesn't shield you from synchronization issues, though!

What is the atomic variables used for in C# [closed]

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
I've been hearing the term atomic variable for a while now and so far I don't no what is that,
so I'd like to see an example and why we use atomic variable if possible in C#
and thank you very much.
Atomic operations are thread-safe operations that execute atomically, that is there is no thread-switch while the operation is executing (or at least the result of a thread-switch is not observable from the outside) so practically the operation is executed as a one-step. On the .Net platform this is provided by the Interlocked class. Other platforms, such as Java provide various other classes, like AtomicInteger. An instance of the AtomicInteger (in Java) could be called an atomic variable, so I'm guessing that's what you are referring to when you say an atomic variable.
The main point about Atomic/Interlocked objects is that they don't require any outside locks or other synchronization objects to achieve atomicity and thus thread-safety.

Categories