IQ DIRECT

Blog

Navigation

  • Home
  • About us
  • Portfolio
  • Reviews
  • Blog
  • Contacts

Blog

Access single file from different threads or processes and maintain consistensy without using extra locks

Sometimes you want to access a single file from different processes or threads and maintain its consistency. The example may be a common database or just a counter. When the file is opened in read-only mode, the question is trivial. But what if you want to write from different threads? Here is the code sample. The idea is to use File.Open constructor with the FileShare.None option. This option allows only one thread to access the file (either for reading or writing) and deny all other threads. The other threads should catch IOException and try to access the file subsequently with a random delay.

TPL_COM_CONTENT_READ_MOREAccess single file from different threads or processes and maintain consistensy without using...
[Code Snippet] Parse FB2 format book in C#

Here is the actual code snippet to parse FB2 format book using C#. FB2 File format is known as FictionBook format. FB2 is rather simple file format based on XML and basic HTML. The FB2 file contains sections. Sections can contain other sections as well as paragraphs. Paragraphs contain actual text and basic formatting. 

The program below loads all sections recursively and print their names to console. Also one can use the fb2verse variable to get the actual text of the paragraph. The approach below uses the XDocument class to get the XElements. The only trick with further parsing is to use the correct XNamespace. 

TPL_COM_CONTENT_READ_MORE[Code Snippet] Parse FB2 format book in C#
How to shutdown a computer programmatically in Windows

If you are writing some installer application, want to update system files, apply settings of for some other reason, you may need to shutdown (reboot, power off) a Windows 10 computer. The good news is that Windows has a pretty straightforward API for this intent, which is called ExitWindowsEx. It has only two parameters. The first one is the flags parameter which you can set to either reboot or power-off and the second one - the reboot reason, which is only used on servers and is not mandatory.  But the bad news is that the functions won't work as-is. In order to make it work, you need to enable the shutdown privilege, which is disabled by default. I advise you to enable this privilege only directly before you decide to shut down. 

TPL_COM_CONTENT_READ_MOREHow to shutdown a computer programmatically in Windows
How to read connection string from App.config

If you are writing console application in C# and need to use the database connection, probably you would like to store the connection string in the App.config file. This is a common practice for C#.NET applications. But how to read the connection string from the file? See the answer below: 

 

ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
  • Fiddler: The Powerful Debugging Tool for any Web Developer
  • [Source Code] C# True Random Password Generator
  • How to get Excel Cell text in OpenXML SDK 2.5?
  • How to assign hotkeys (stortcuts) to C# Windows Forms Controls?
  • How to Read Direct Mouse and Keyboard Events in C++ Console App?

Page 24 of 58

  • ...
  • 20
  • 21
  • 22
  • 23
  • 24
  • ...
  • 26
  • 27
  • 28

Copyright ©2026 IQ DIRECT


main version