# The process cannot access the file because it is being used by another process



## deadscenekid (Apr 26, 2006)

I have been working on a new program that writes information to a custom file associated text file and for some reason I keep getting this error, here is the exact error information:


```
System.IO.IOException was unhandled
  Message="The process cannot access the file 'C:\lc-set.lct' because it is being used by another process."
  Source="mscorlib"
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)    at System.IO.StreamWriter.CreateFile(String path, Boolean append)    at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)    at System.IO.StreamWriter..ctor(String path)    at LCTwitter.Open.OK_Button_Click(Object sender, EventArgs e) in C:\Documents and Settings\Travis James\Desktop\Programming\Current Programs\LCTwitter\LCTwitter\Open.vb:line 12    at System.Windows.Forms.Control.OnClick(EventArgs e)    at System.Windows.Forms.Button.OnClick(EventArgs e)    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)    at System.Windows.Forms.Control.WndProc(Message& m)    at System.Windows.Forms.ButtonBase.WndProc(Message& m)    at System.Windows.Forms.Button.WndProc(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.Run(ApplicationContext context)    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)    at LCTwitter.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
```
I was wondering if this could result from another form in the application using the file also to read/write information?

Here is the code from Open.vb:

```
Dim FILE_NAME As String = "C:\lc-set.lct"

            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
                objWriter.Write(txtUsername.Text + "," + txtPassword.Text + ",")
                objWriter.Close()
                MsgBox("Your Settings have been saved!")
                Me.Hide()
```
Here is the code from Form1.vb:

```
Dim LineOfText As String
        Dim objReader As New System.IO.StreamReader(FILE_NAME)


        Do While objReader.Peek() <> -1
            LineOfText = objReader.ReadLine() & vbNewLine
        Loop

        aryTextFile = LineOfText.Split(",")


        Open.txtUsername.Text = aryTextFile(0)
        Open.txtPassword.Text = aryTextFile(1)
```
The error is coming when I try to save the information in Open.vb.

Any help appreciated! Thanks :}


----------



## lotuseclat79 (Sep 12, 2003)

This sounds like a classic concurrent readers/writers problem for shared data, i.e. the file in this case. In a locking scheme that will resolve the issue, create read and write locks that are grabbed in one machine cycle (like a test & set instruction), and if the lock is not obtainable, put the request on a queue. Reading should not be a problem and you can simply add and subtract a lock count. Writing is another matter, since when a write lock is acquired, it should immediately block all reads until the write is completed, and the lock released.

That's off the top of my head. You can google for readers/writers problem or look here at Wikipedia for an explanation in greater detail of it.

-- Tom


----------



## deadscenekid (Apr 26, 2006)

Thanks for your reply, but can you possibly show me an example of how to do that in VB.NET because I have never used locks or anything like that...


----------



## lotuseclat79 (Sep 12, 2003)

Don't know VB.NET. Try googling: vb.net +file locking

-- Tom


----------

