Does a static class reuse Entity Framework connection? [closed] - c#

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
Does this static class reuse Entity Framework connection and is it thread safe?
public static class TeamService
{
public static Team GetTeam(string id)
{
using (var Db = new ApplicationDbContext())
{
//TODO:
}
}
}

By default, the context manages connections to the database. The context opens and closes connections as needed. For example, the context opens a connection to execute a query, and then closes the connection when all the result sets have been processed.
-- https://msdn.microsoft.com/en-us/data/jj729737
A separate instance of context for each thread is thread-safe. As long
as each thread of execution has its own instance of EF context you
will be fine.
-- https://stackoverflow.com/a/4455827

Related

How To Unit Test A Public Method That Modifies The State Of Private Member Fields? [closed]

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 years ago.
Improve this question
I am trying to unit test the following method for a class that implements IHostedService interface.
public async Task StopAsync(CancellationToken cancellationToken)
{
await Task.Run(() =>
{
_cancellationTokenSource.Cancel();
_pollLoopThread.Join();
});
}
Currently, the CancellationTokenSource and Thread fields are private.
In such circumstances how could I unit test this method?
Refactor the class so that the thread and cancellation token are injected constructor dependencies, which can subsequently be mocked.
Add a property IsRunning to allow testing feature that the hosted service is started or stopped
Do not unit test the StopAsync method

Is accessing async .Result considered as bad practice? [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 3 years ago.
Improve this question
If I have following
public async Task<Owner> Get(int carId)
{
var car = await myDbContext.Cars.FindAsync(carId);
return car.Owner;
}
I cannot access the Owner property in the first line cause it's a async call.
If I access it using await myDbContext.Cars.FindAsync(carId).Result.Owner does that mean that I'll get stuck in a deadlock sometime or does it have some other side effects?
The difference between your current code and using .Result is that, currently, the caller of the Get(...) method will be able to continue, until awaiting themselves.
Using .Result in the Get(...) method will block any caller at that point, until the result is available, and is therefore discouraged.

Clone or duplicate a task 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 3 years ago.
Improve this question
Because task can not run twice (it would throw an exception). I am creating ahead 2 tasks.
Is there a nice way to duplicate a task instead, something maybe like that:
Task t1 = new Task();
Task t2 = new Task(t1);
Do you truly need to clone a task? If not then one way to achieve the same outcome would be to first create an Action and then create two tasks that take the same Action.
var work = new Action(() =>
{
// do work here
});
var t1 = new Task(work);
var t2 = new Task(work);

C# breaking execution of program [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 5 years ago.
Improve this question
Is there any functionality in C# with Web, so that from UI, if a button is pressed, the execution should get stopped. (Here C# is already busy with some process execution)
For example:
while (something)
{
// do something
}
while (something)
{
// do something
}
If first while loop is under execution and from UI "stop execution" button is pressed then it should interrupt the whole execution and exit.
You mean like this?
private volatile bool _stop = false;
void OnCancelClick()
{
_stop = true;
}
void WorkerProcess() //In separate thread
{
while (!_stop)
{
// do something
}
}
There are a million ways to do it. This is probably the simplest to understand.

C# background worker execute s.play() once only [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 have a background worker that executes evry time when data recived on serial port, and inside is some code, i would like to play some music file, just the first time that background worker executes, can somebody please help me ?
code:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
.....
s.play();
...
}
I would probably just use a boolean class level field and set it after first call to DoWork, and then check in each call to see if you need to initiate play.

Categories

Resources