Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • If there’s a lock it can’t really be non-blocking. Can it really be called from multiple threads at the same time? Commented Aug 22, 2020 at 11:51
  • No Its only in one thread. I just want to write to the file in a way that doesn't stop executing of code, and doesn't affect speed/execution of the main thread. Commented Aug 22, 2020 at 11:53
  • If it’s in one thread you don’t need a lock at all. But if you want to write in the background then you’d need another thread, async doesn’t mean it will run concurrently and fire-and-forget isn’t a great idea often Commented Aug 22, 2020 at 11:54
  • That's correct I want to run it in another thread. Looks like my code is wrong. Commented Aug 22, 2020 at 11:57
  • Your question is overly broad. However, your requirement that the synchronization be non-blocking seems to be the crux of the matter. See duplicate for approaches to handling synchronization without blocking. Note that while the FileShare value passed to the FileStream constructor will prevent concurrent access to the file, it doesn't cause any sort of wait/synchronization; another thread or process trying to open the file will just fail. ... Commented Aug 22, 2020 at 16:47