Mas
Mas

Reputation: 4616

C# Creating FileStream to Network Share Slow in Multi-threaded Application

I have an application that has many threads processing data. As part of this processing, it is writing a small file to a network share. I create the FileStream using the following code:

using (var fileStream = new FileStream("\\server\path\etc", FileMode.Create, FileAccess.Write, FileShare.None))

Under high load (writing under 100 files per second), the creation of the FileStream CAN take a long time (over 60 seconds). This occurs after the application is running under high load for a few minutes.

There must be something that's blocking the thread when creating the FileStream. I thought maybe the threads were being blocked trying to create a connection to the file share. I checked my process via Process Explorer, but could not find a TCP connection to the file server. So I'm guessing that the SMB connection to the file server is not over TCP.

Does anyone have an idea what the problem is, or how I can do things differently to achieve better performance?

Upvotes: 1

Views: 3048

Answers (1)

Dark Falcon
Dark Falcon

Reputation: 44201

I suspect you are experiencing the problem described in this KB article: Shared file access is delayed if the file is open on another computer. I have personally seen this cause Microsoft Access to block for 30 seconds because it retried the operation 30 times, each blocking for a second. Perhaps this is an extension of this problem.

If you try to open a file on a computer that is running Windows NT over the network and the file is open on another client computer that has sharing restrictions, there is a delay of approximately one second before the sharing violation error message is returned. If the client application is accessing a number of files on the server, this delay may become significant. These symptoms can be easily seen with any multi-user, file-based application, such as the Jet database engine that has shared database files.

I would start with the suggestion of trying local files first and see what happens.

Upvotes: 1

Related Questions