c# multithreading and resource locks

I noticed that in your method, you aren't closing the StreamWriter with .Close method after writing. That means you are leaving it open for the garbage collector to finalize and close the file which could lead to intermittent blocking.

"using" is sugar for try/finally for objects that implement `IDisposable`

Code:
using (StreamWriter sw = File.CreateText(filepath))
{
     sw.WriteLine(message);
}

results it the following intermediate code

Code:
StreamWriter sw = File.CreateText(filepath)
try 
{
     sw.WriteLine(message);
}
finally
{
    sw.Dispose();
}
 
"using" is sugar for try/finally for objects that implement `IDisposable`

Code:
using (StreamWriter sw = File.CreateText(filepath))
{
     sw.WriteLine(message);
}

results it the following intermediate code

Code:
StreamWriter sw = File.CreateText(filepath)
try
{
     sw.WriteLine(message);
}
finally
{
    sw.Dispose();
}
Correct.. With .NET6 you also do not need the braces around the usage. Disposal will happen when variable goes out of scope. Removes a bit of clutter.
 
"using" is sugar for try/finally for objects that implement `IDisposable`

Code:
using (StreamWriter sw = File.CreateText(filepath))
{
     sw.WriteLine(message);
}

results it the following intermediate code

Code:
StreamWriter sw = File.CreateText(filepath)
try
{
     sw.WriteLine(message);
}
finally
{
    sw.Dispose();
}
Even shorter:

using SqlConnection sqlConnection = new (connString);
 
"using" is sugar for try/finally for objects that implement `IDisposable`

Code:
using (StreamWriter sw = File.CreateText(filepath))
{
     sw.WriteLine(message);
}

results it the following intermediate code

Code:
StreamWriter sw = File.CreateText(filepath)
try
{
     sw.WriteLine(message);
}
finally
{
    sw.Dispose();
}

Yeah, sorry, I missed the using statement when reading it.
 
File read and write operations is IO intensive so async and await must be used.
Async I get, but why await ? It will cause a wait, blocking the thread which is calling the method (and has further important code which must execute asap) to write to the log until others are done. I don't want the thread to block.
 
Async I get, but why await ? It will cause a wait, blocking the thread which is calling the method (and has further important code which must execute asap) to write to the log until others are done. I don't want the thread to block.
As far as I know and read, async and await should always go together, and should be used for all IO operations. I think you kind of have it wrong way round, not using await will "hog" the thread, and using it will put the thread back in the thread pool to be available for other things.
 
As mentioned I have a static class Program and within it a static method to write to files.

I assumed a variable declared in the static method to write the files will automatically be static as well.

I used Object locker = new Object() within the method.

I used lock(locker) around the code using the file/streamwriter.

It did not work.

When I changed Object locker to a global variable within the static class, explicitly defined as static, it worked.

private static Object _locker = new Object();

It worked.

Are variables within a static method within a static class not automatically static then ? (Note, you can't use the static modifier for a variable within a static method). Or are they "kind of" static, just not a single instance ?

Is it because it goes out of scope ? But then two or more threads already within the method should see the same locker ?
 
As mentioned I have a static class Program and within it a static method to write to files.

I assumed a variable declared in the static method to write the files will automatically be static as well.

I used Object locker = new Object() within the method.

I used lock(locker) around the code using the file/streamwriter.

It did not work.

When I changed Object locker to a global variable within the static class, explicitly defined as static, it worked.

private static Object _locker = new Object();

It worked.

Are variables within a static method within a static class not automatically static then ? (Note, you can't use the static modifier for a variable within a static method). Or are they "kind of" static, just not a single instance ?

Is it because it goes out of scope ? But then two or more threads already within the method should see the same locker ?
I'm not sure of the C# syntax, but in C++, a variable declared inside a static method is local, unless prefixed with static.

 
I'm not sure of the C# syntax, but in C++, a variable declared inside a static method is local, unless prefixed with static.
But I thought static is tied to singularity, there are no separate instances and threads entering the static method in the static class use the same (exact) variable despite it being local in scope ? (also in C++ I thought)
 
As mentioned I have a static class Program and within it a static method to write to files.

I assumed a variable declared in the static method to write the files will automatically be static as well.

I used Object locker = new Object() within the method.

I used lock(locker) around the code using the file/streamwriter.

It did not work.

When I changed Object locker to a global variable within the static class, explicitly defined as static, it worked.

private static Object _locker = new Object();

It worked.

Are variables within a static method within a static class not automatically static then ? (Note, you can't use the static modifier for a variable within a static method). Or are they "kind of" static, just not a single instance ?

Is it because it goes out of scope ? But then two or more threads already within the method should see the same locker ?
Variables in methods/functions have the same scope irregardless if they are static or not

 
But I thought static is tied to singularity, there are no separate instances and threads entering the static method in the static class use the same (exact) variable despite it being local in scope ?
Static on a class method means that the method is class scope rather than object scope (i.e., it can be invoked without a class instance, and cannot access this, or member variables, etc.).

The variables are still locally constructed and destructed inside static members unless declared static (in which case they are constructed upon first invocation).
 
As far as I know and read, async and await should always go together, and should be used for all IO operations. I think you kind of have it wrong way round, not using await will "hog" the thread, and using it will put the thread back in the thread pool to be available for other things.

As you probably saw from the link you posted, not necessarily. You can use it like EM suggested. There are reasons it shouldn't be done, but I can't remember all of them.
Error handling is one of them, although there are ways around it.
 
That scope refers to where the variables can be accessed from, not the lifetime of the variable.
The link was more to show how lock works.

Scope was the wrong word :thumbsup:

The point is, there is nothing special about a variable declared inside a static method vs a class method. It’s scope is as expected - it cannot be accessed outside the function, and it’s lifetime is as expected- it’s created on every invocation
 
Variables in methods/functions have the same scope irregardless if they are static or not
I get that, I thought all threads entering the thread at the same time and seeing the local variable will be looking at the (exact) same thing
 
The variables are still locally constructed and destructed inside static members unless declared static (in which case they are constructed upon first invocation).
I will go with this then, c# does not allow the static modifier within the static method, so I am assuming it is somehow not Truly static.
 
I will go with this then, c# does not allow the static modifier within the static method, so I am assuming it is somehow not Truly static.
Seems to work exactly as expected - the same as every other language I have used.

What languages can you define a static variable inside static method?

Why would not just do

Code:
static object _lock = new Object()

static void MyFunction() {
}

This is how it is/works????
 
Top
Sign up to the MyBroadband newsletter
X