Should one thread wait before accessing a folder until another one finishes modifying it?

cesario678

Client
Регистрация
10.03.2022
Сообщения
225
Благодарностей
13
Баллы
18
I would like to know how to implement a function to edit or read a folder in a multithreaded environment. How can I make one thread wait while another thread is editing or modifying the folder, while the other bots wait for the completion of the editing and analysis of a TXT or CSV file, for example? Or is there another approach? Thank you.
 
Регистрация
07.08.2019
Сообщения
89
Благодарностей
102
Баллы
43
I would like to know how to implement a function to edit or read a folder in a multithreaded environment. How can I make one thread wait while another thread is editing or modifying the folder, while the other bots wait for the completion of the editing and analysis of a TXT or CSV file, for example? Or is there another approach? Thank you.
The most solid and bulletproof solution is Mutex — it's a kernel-level Windows synchronization primitive:
code:
bool acquired = false;
using(var mutex = new Mutex(false, "MyFolderMutex"))
{
  try
  {
      acquired = mutex.WaitOne();
      // Your file/folder operations here
  }
  finally
  {
      if (acquired)
          mutex.ReleaseMutex();
  }
}
Why Mutex is the most reliable:
- OS-level guarantee — Windows kernel handles the synchronization
- Cross-process — works even between different ZennoPoster instances or external apps
- Crash-safe — if your thread crashes, OS automatically releases the mutex
- Named — any process in the system can see "MyFolderMutex" and wait for it

Tip: Make your mutex name unique to the folder, e.g.:
string mutexName = "FolderLock_" + folderPath.GetHashCode();

This is the "nuclear option" — slower than lock() but guaranteed to work in any scenario.
 

cesario678

Client
Регистрация
10.03.2022
Сообщения
225
Благодарностей
13
Баллы
18
“Thank you for the help, but tell me how do I use it?
Do I create a lock in C# before the function that I need to lock, and then when execution passes through it, will the function be locked for the other instances?
Thank you.”
The most solid and bulletproof solution is Mutex — it's a kernel-level Windows synchronization primitive:
code:
bool acquired = false;
using(var mutex = new Mutex(false, "MyFolderMutex"))
{
  try
  {
      acquired = mutex.WaitOne();
      // Your file/folder operations here
  }
  finally
  {
      if (acquired)
          mutex.ReleaseMutex();
  }
}
Why Mutex is the most reliable:
- OS-level guarantee — Windows kernel handles the synchronization
- Cross-process — works even between different ZennoPoster instances or external apps
- Crash-safe — if your thread crashes, OS automatically releases the mutex
- Named — any process in the system can see "MyFolderMutex" and wait for it

Tip: Make your mutex name unique to the folder, e.g.:
string mutexName = "FolderLock_" + folderPath.GetHashCode();

This is the "nuclear option" — slower than lock() but guaranteed to work in any scenario.
“Thank you for the help, but tell me how do I use it?
Do I create a lock in C# before the function that I need to lock, and then when execution passes through it, will the function be locked for the other instances?
Thank you.”
 
Регистрация
07.08.2019
Сообщения
89
Благодарностей
102
Баллы
43
Yes, exactly. You wrap your folder operation inside the Mutex block in a C# code snippet (not an action block) — while one thread is inside, all others wait at WaitOne() until the first one calls ReleaseMutex(). Then the next one in the queue enters.

Just make sure the mutex name ("MyFolderMutex") is the same everywhere you access that folder. And always set a timeout in WaitOne so a thread doesn't hang forever if something crashes. Use a "C# code" action for this, not regular action blocks.

Or with a simple txt flag — one thread writes "BUSY" to a shared txt file, others read it and wait until it says "FREE".

Or if you're already using a database, you can use a flag in a table — one thread sets the flag to 1 (busy), others check it and wait, then the first one resets it to 0 when done. Same idea, just through DB instead of the filesystem.

Mutex is the most reliable option though — the lock file approach has a small race condition window between the check and creation.
 
  • Спасибо
Реакции: cesario678

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)