How to lock file - c#

please tell me how to lock file in c#
Thanks

Simply open it exclusively:
using (FileStream fs =
File.Open("MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
// use fs
}
Ref.
Update: In response to comment from poster: According to the online MSDN doco, File.Open is supported in .Net Compact Framework 1.0 and 2.0.

FileShare.None would throw a "System.IO.IOException" error if another thread is trying to access the file.
You could use some function using try/catch to wait for the file to be released. Example here.
Or you could use a lock statement with some "dummy" variable before accessing the write function:
// The Dummy Lock
public static List<int> DummyLock = new List<int>();
static void Main(string[] args)
{
MultipleFileWriting();
Console.ReadLine();
}
// Create two threads
private static void MultipleFileWriting()
{
BackgroundWorker thread1 = new BackgroundWorker();
BackgroundWorker thread2 = new BackgroundWorker();
thread1.DoWork += Thread1_DoWork;
thread2.DoWork += Thread2_DoWork;
thread1.RunWorkerAsync();
thread2.RunWorkerAsync();
}
// Thread 1 writes to file (and also to console)
private static void Thread1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 20; i++)
{
lock (DummyLock)
{
Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " - 3");
AddLog(1);
}
}
}
// Thread 2 writes to file (and also to console)
private static void Thread2_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 20; i++)
{
lock (DummyLock)
{
Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " - 4");
AddLog(2);
}
}
}
private static void AddLog(int num)
{
string logFile = Path.Combine(Environment.CurrentDirectory, "Log.txt");
string timestamp = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");
using (FileStream fs = new FileStream(logFile, FileMode.Append,
FileAccess.Write, FileShare.None))
{
using (StreamWriter sr = new StreamWriter(fs))
{
sr.WriteLine(timestamp + ": " + num);
}
}
}
You can also use the "lock" statement in the actual writing function itself (i.e. inside AddLog) instead of in the background worker's functions.

Related

Monitor when a file is in use

I need to monitor when a file (*.wav) is being executed.
I know about the filesystemwatcher class. However does this class notify when a file is being used? I also came across Monitor when an exe is launched. While I realise that this has to do with an exe, is there a way to be notified that a wav file has been executed?
No, you can't get an Event when non executable files such as mp3, wav, txt and etc are opened.
But actually can check if file is opened write now...
static void Main(string[] args)
{
using (BackgroundWorker bw = new BackgroundWorker())
{
bw.DoWork += Bw_DoWork;
bw.RunWorkerAsync();
}
Console.WriteLine("Press Q to exit");
while (Console.ReadKey(true).Key!=ConsoleKey.Q){}
}
private static bool[] opened;
private static void Bw_DoWork(object sender, DoWorkEventArgs e)
{
var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.mp3");
opened = new bool[files.Length];
while (true)
for (int i = 0; i < files.Length; i++)
try
{
using (var fs = File.Open(files[i], FileMode.Open, FileAccess.Read, FileShare.None))
opened[i] = false;
}
catch{ opened[i] = true; }
}
P.S. I know it's ugly :D

Is it possible to "launch" a copy of a (large) file without waiting for the result?

I think it's pretty obvious in the title ; I want to copy a file without waiting for the result.
Functionaly I want this :
static void Main(string[] args)
{
string strCmdText = #"/C xcopy c:\users\florian\desktop\mytestfile.fil p:\";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
Console.WriteLine("Finished !");
}
Basically my main thread is released after few milliseconds.
I try to do like this :
static void Main(string[] args)
{
var t = Task.Run(() => Copy(#"c:\Users\florian\Desktop\mytestfile.fil", "p:"));
}
private static void Copy(string source, string destination)
{
using (FileStream SourceStream = File.Open(source, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(destination + source.Substring(source.LastIndexOf('\\'))))
{
SourceStream.CopyToAsync(DestinationStream);
}
}
}
My mytestfile.fil is created in my destination folder but its size is 0kb.
Regards,
Basically my main thread is released after few milliseconds. I try to do like this
You must keep the process alive somehow. The easiest way to do this - if you actually have to do the file copy on a thread of its own - is to execute your Copy method on a foreground thread:
class Program
{
static void Main(string[] args)
{
System.Threading.Thread thread = new System.Threading.Thread(()=> Copy(#"c:\Users\florian\Desktop\mytestfile.fil", "p:"));
thread.Start();
}
private static void Copy(string source, string destination)
{
using (FileStream SourceStream = File.Open(source, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(destination + source.Substring(source.LastIndexOf('\\'))))
{
SourceStream.CopyTo(DestinationStream);
}
}
}
}
...or you could execute it on the main thread since your application doesn't seem to anything else:
class Program
{
static void Main(string[] args)
{
string source = #"c:\Users\florian\Desktop\mytestfile.fil";
string destination = "p";
using (FileStream SourceStream = File.Open(source, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(destination + source.Substring(source.LastIndexOf('\\'))))
{
SourceStream.CopyTo(DestinationStream);
}
}
}
}
A third option is to make your method async and wait for it to complete synchronously in the Main method:
class Program
{
static void Main(string[] args)
{
CopyAsync(#"c:\Users\florian\Desktop\mytestfile.fil", "p:").Wait();
}
private static async Task CopyAsync(string source, string destination)
{
using (FileStream SourceStream = File.Open(source, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(destination + source.Substring(source.LastIndexOf('\\'))))
{
await SourceStream.CopyToAsync(DestinationStream);
}
}
}
}
Note that you should not use the asyncronous CopyToAsync method unless you actiually await it. Don't mix synchronous and asynchronous code :)
OK more than one error.
Your program exits before the copy completes. you need something like:
CopyToAsync was not awaited
class Program
{
private static void Main(string[] args)
{
var source = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
source.Cancel();
};
try
{
MainAsync(args, source.Token).GetAwaiter().GetResult();
return;
}
catch (OperationCanceledException)
{
return;
}
}
static async Task MainAsync(string[] args, CancellationToken token)
{
var t = Task.Run(() => Copy(#"c:\test.txt", #"c:\dest\"));
Console.WriteLine("doing more stuff while copying");
await t;
Console.WriteLine("Finished !");
}
private static void Copy(string source, string destination)
{
using (FileStream SourceStream = File.Open(source, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(destination + source.Substring(source.LastIndexOf('\\'))))
{
SourceStream.CopyTo(DestinationStream);
}
}
}
}
Your Main method returns before the Task has completed, so your application ends.
It seems like this might be an instance of the XY problem.
Keep in mind that the OS/framework is basically giving your process its own "space" to run in, so the following:
static void Main(string[] args)
{
var t = Task.Run(() => Copy(#"c:\Users\florian\Desktop\mytestfile.fil", "p:"));
}
private static void Copy(string source, string destination)
{
using (FileStream SourceStream = File.Open(source, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(destination + source.Substring(source.LastIndexOf('\\'))))
{
SourceStream.CopyToAsync(DestinationStream);
}
}
}
is no different in terms of performance than just doing
static void Main(string[] args)
{
Copy(#"c:\Users\florian\Desktop\mytestfile.fil", "p:");
}
Since your console application fundamentally isn't doing anything other than copying the file, the extra thread just adds overhead.
Also, if you're using asynchronous operations inside the Copy method, you should declare your method async Task rather than void because otherwise you can't use the await keyword inside the Copy method or wait for the Copy method to complete elsewhere in code. So:
private static async Task Copy(string source, string destination)
As written, this has a blatant race condition and probably won't work correctly because DestinationStream and SourceStream may be disposed of before SourceStream.CopyToAsync(DestinationStream) has actually finished. You need to do
await SourceStream.CopyToAsync(DestinationStream);
inside your method to prevent this.
If you're launching the Copy operation with
System.Diagnostics.Process.Start(...);
you're already effectively running this asynchronously from the perspective of the program that launched it because the program that launched it doesn't wait for the process to complete before it continues. Incidentally, I assume that that console application will do something other than just launch the process, correct? (If not, it seems a little redundant).

C# Multithreading for three threads working in parallel and sending data to each other and working in order

I have a C# windows form program that uses three threads.
*The first thread is responsible for reading files from input folder and reading all text from each file then send data as string to the second thread using a Queue.
*The second thread is responsible on parsing the string got from the first thread, if the string was a number then send this number to the third thread also using a Queue.
*The third thread is responsible on creating file for the number got from thread 2.
Also I should have a Timer that runs thread 1 every 4 seconds for example until pressing stop.( should thread 2 and thread 3 when thread 1 runs after 4 seconds ?)
Note That the three threads should be running in parallel and should be in the correct order Thread 1 then Thread 2 then Thread 3 and should be getting and sending data for each other.
Below is an image for what i described below and some of the code that I used in my program.
Please I want your help I'm stuck in this ....
Code is :
form1.cs Class
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mltithreading
{
public partial class Form1 : Form
{
public ConcurrentQueue<string> _queue = new ConcurrentQueue<string>();
public Queue<string> Queue1result = new Queue<string>();
public Queue<string> TestQueue = new Queue<string>();
public Queue<string> Queue2result = new Queue<string>();
public Form1()
{
InitializeComponent();
}
/* private Timer timer1;
public void InitTimer()
{
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 2000; // in miliseconds
timer1.Start();
} */
private void timer1_Tick(object sender, EventArgs e)
{
isonline();
}
public void isonline()
{
//
// The user selected a folder and pressed the OK button.
// We print the number of files found.
//
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
MessageBox.Show(files[0]);
var a = files[1];
var dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
FileInfo[] f = dir.GetFiles();
foreach (System.IO.FileInfo fi in f)
{
Console.WriteLine("{0}: {1}: {2}", fi.Name, fi.LastAccessTime, fi.Length);
}
MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//
// The user selected a folder and pressed the OK button.
// We print the number of files found.
//
textBox1.Text = folderBrowserDialog1.SelectedPath;
/* try
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
var dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
var output = folderBrowserDialog2.SelectedPath;
FileInfo[] f = dir.GetFiles();
Queue<FileInfo> Q = new Queue<FileInfo>(f);
while (Q.Count > 0)
{
FileInfo current = Q.Dequeue();
Console.WriteLine(current);
// Process 'current'
}
foreach (System.IO.FileInfo fi in f)
{
Console.WriteLine("{0}: {1}: {2}", fi.Name, fi.LastAccessTime, fi.Length);
}
MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
}
catch (Exception excep)
{
Console.WriteLine("An error occurred: '{0}'", excep);
} */
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog2.ShowDialog();
if (result == DialogResult.OK)
{
//
// The user selected a folder and pressed the OK button.
// We print the number of files found.
//
textBox2.Text = folderBrowserDialog2.SelectedPath;
}
}
private void button3_Click(object sender, EventArgs e)
{
string input = folderBrowserDialog1.SelectedPath;
string output = folderBrowserDialog2.SelectedPath;
var result = Thread1Start(input, output);
Console.WriteLine("Is thread Still Running :{0}", result);
//var firstThread = new Thread1();
var secondThread = new Thread2();
var thirdThread = new Thread3();
//Thread thread = new Thread(() => th.Readfiles(folderBrowserDialog1.SelectedPath, folderBrowserDialog2.SelectedPath));
//Thread thread1 = new Thread(delegate() { Queue1result = firstThread.Readfiles(folderBrowserDialog1.SelectedPath, folderBrowserDialog2.SelectedPath); });
//Thread thread3 = new Thread(() =>second.test(valueQueue));
Thread thread2 = new Thread(delegate() { Queue2result = secondThread.test(TestQueue); });
Thread thread3 = new Thread(() => thirdThread.CreateFiles(Queue2result,output));
//Thread thread5 = new Thread(second.run);
//thread1.Start();
// thread1.Join();
thread2.Start();
//thread2.Join();
thread3.Start();
//thread1.Start();
//thread1.Join();
//thread2.Start();
//thread1.Join(TimeSpan.Zero);
//Console.WriteLine(thread1.Join(TimeSpan.Zero));
// new System.Threading.Timer (ThreadTimer, "Thread 1 Started working...", 5000, 1000);
}
private void button4_Click(object sender, EventArgs e)
{
}
/* public void ThreadTimer (object data)
{
string input = folderBrowserDialog1.SelectedPath;
string output = folderBrowserDialog2.SelectedPath;
var result = Thread1Start(input, output);
Console.WriteLine("Is thread Still Running :{0}",result);
}
*/
public bool Thread1Start(string input, string output)
{
var firstThread = new Thread1();
Thread thread1 = new Thread(delegate() { Queue1result = firstThread.Readfiles(input, output); });
TestQueue = Queue1result;
thread1.Start();
Console.WriteLine("thread 1 start");
var state = thread1.Join(TimeSpan.Zero);
var secondThread = new Thread2();
var thirdThread = new Thread3();
//Thread thread = new Thread(() => th.Readfiles(folderBrowserDialog1.SelectedPath, folderBrowserDialog2.SelectedPath));
//Thread thread1 = new Thread(delegate() { Queue1result = firstThread.Readfiles(folderBrowserDialog1.SelectedPath, folderBrowserDialog2.SelectedPath); });
//Thread thread3 = new Thread(() =>second.test(valueQueue));
Thread thread2 = new Thread(delegate() { Queue2result = secondThread.test(Queue1result); });
Thread thread3 = new Thread(() => thirdThread.CreateFiles(Queue2result, output));
//Thread thread5 = new Thread(second.run);
//thread1.Start();
// thread1.Join();
thread2.Start();
//thread2.Join();
thread3.Start();
return state;
}
}
}
Thread1.cs class
here using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mltithreading
{
class Thread1
{
public void run()
{
while (true)
{
int iterations = 10;
try
{
for (int i = 0; i < iterations; i++)
{
Console.WriteLine("From Thread 1");
Thread.Sleep(2000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
public Queue<string> Readfiles(string inputDirectoryPath, string outputDirectoryPath)
{ Queue<string> thread1Queue = new Queue<string>();
try
{
var dir = new DirectoryInfo(inputDirectoryPath);
FileInfo[] f = dir.GetFiles();
foreach (System.IO.FileInfo fi in f)
{
var filePath = fi.DirectoryName + "\\" + fi.Name;
//File.Copy(filePath, outputDirectoryPath + "\\" + fi.Name);
// Open the file to read from.
string readText = File.ReadAllText(filePath);
thread1Queue.Enqueue(readText);
Console.WriteLine(readText);
Console.WriteLine("thread 1 call read files method ");
//Console.WriteLine("{0}: {1}: {2}", fi.Name, fi.LastAccessTime, fi.Length);
}
}
catch (Exception excep)
{
Console.WriteLine("An error occurred: '{0}'", excep);
}
return thread1Queue;
}
}
}
Thread2.cs class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Mltithreading
{
class Thread2
{
public void run()
{
while (true)
{
int iterations = 10;
try
{
for (int i = 0; i < iterations; i++)
{
Console.WriteLine("From Thread 2");
Thread.Sleep(2000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
public Queue<string> test(Queue<string> list)
{
Queue<string> thread2Queue = new Queue<string>();
while (list.Count > 0)
{
try
{
var a = list.Dequeue();
Console.WriteLine(a);
int j;
if (Int32.TryParse(a, out j))
{
Console.WriteLine("parsed the string {0} to integer and sent to thread 3", j);
thread2Queue.Enqueue(a);
}
else
Console.WriteLine("String could not be parsed to integer.");
Console.WriteLine("thread 2 parsing string from thread 1");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
return thread2Queue;
}
}
}
Thread3.cs Class
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Mltithreading
{
class Thread3
{
public void run()
{
while (true)
{
int iterations = 10;
try
{
for (int i = 0; i < iterations; i++)
{
Console.WriteLine("From Thread 3");
Thread.Sleep(2000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
public void CreateFiles(Queue<string> list, string outputDirectoryPath)
{
while (list.Count > 0)
{
string format = "MM-dd-yyyy--HH-mm-ss";
DateTime date = DateTime.Now;
string filename = date.ToString(format);
string file = outputDirectoryPath + "\\" + filename + ".txt";
var a = list.Dequeue();
FileInfo fi = new FileInfo(file);
if (!fi.Exists)
{
fi.Create().Dispose();
System.IO.File.WriteAllText(file,a);
Console.WriteLine("creating new text file {{0}}",file);
}
else
{
Console.WriteLine("file already exists");
}
Console.WriteLine(a);
Console.WriteLine("Thread 3 creating File with date name");
Console.WriteLine(date.ToString(format));
}
}
}
}
Thank you very much for your help .
I won´t comment on all, there´s too much stuff to talk about, but let´s have a look at your first thread.
Thread thread1 = new Thread(delegate() { Queue1result = firstThread.Readfiles(input, output); });
This line will create a thread that will replace the existing instance in your field Queue1Result (have a look at naming conventions, please!) once after reading some files (if existing). If executed again, The existing queue goes out of scope and will be replaced with a new one, probably not what you want to do.
You need to create those queue instances just once (type to use is ConcurrentQueue), those should then be used as a parameter running the thread procedure. So, redeclare the queues to readonly ConcurrentQueue to create the instances for your fields(give them a good "speaking" name, btw).
public readonly ConcurrentQueue<string> _textFromFilesQueue = new ConcurrentQueue<string>();
Once started, you want to regularly look after files again, so your code has to contain a loop.
This loop to be executed should be terminable from outside / other threads using a CancellationToken. Lets add a CancellationTokenSource to Form 1:
public CancellationTokenSource CancellationTokenSource { get; set; }
Your execution code for the first thread could / should look like this:
public void Readfiles(string inputDirectoryPath, string outputDirectoryPath, ConcurrentQueue<string> queue, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
var dir = new DirectoryInfo(inputDirectoryPath);
FileInfo[] f = dir.GetFiles();
foreach (System.IO.FileInfo fi in f)
{
var filePath = Path.Combine(fi.DirectoryName, fi.Name);
//File.Copy(filePath, outputDirectoryPath + "\\" + fi.Name);
// Open the file to read from.
string textFromFile = File.ReadAllText(filePath);
queue.Enqueue(textFromFile);
Console.WriteLine(textFromFile);
Console.WriteLine("thread 1 call read files method ");
//Console.WriteLine("{0}: {1}: {2}", fi.Name, fi.LastAccessTime, fi.Length);
}
Thread.Sleep(TimeSpan.FromSeconds(5));
}
catch (Exception excep)
{
Console.WriteLine("An error occurred: '{0}'", excep);
Throw excep;
}
}
}
Do not build up a path by concatenating strings, use Path.Combine instead. Also, exceptions that happened should just be rethrown to ensure correct handling from other threads / tasks.
I would recommend using the TPL mechanisms instead of threads, it grants the system more flexibility in handling the tasks to be executed in a parallel manner. Have a look here : https://msdn.microsoft.com/de-de/library/dd460717(v=vs.110).aspx
So, creating and executing this task:
CancellationTokenSource = new CancellationTokenSource();
Task task = new Task(() => Readfiles(input, output, _textFromFilesQueue, CancellationTokenSource.Token));
task.Start();
Stopping the task:
CancellationTokenSource.Cancel();
Be sure not to overwrite the TokenSource without calling Cancel(), else you are losing the ability to stop the tasks that have already started...
EDIT: If you really need to ignore TPL, here is the example to execute the reading method with a thread:
Thread thread = new Thread(new ThreadStart(() => Readfiles(input, output, _textFromFilesQueue, CancellationTokenSource.Token)));
thread.Resume();
Use very simple and clear code:
var inputTexts = new BlockingCollection<string>();
var inputNumbers = new BlockingCollection<string>();
var readFiles = Task.Run(() =>
{
try
{
foreach (var file in Directory.EnumerateFiles("path", "searchPattern"))
{
string text = File.ReadAllText(file);
inputTexts.Add(text);
}
}
finally { inputTexts.CompleteAdding(); }
});
var processNumbers = Task.Run(() =>
{
try
{
foreach (var text in inputTexts.GetConsumingEnumerable())
{
int result;
if (int.TryParse(text, out result))
inputNumbers.Add(text);
}
}
finally { inputNumbers.CompleteAdding(); }
});
var createFiles = Task.Run(() =>
{
foreach (var number in inputNumbers.GetConsumingEnumerable())
{
string path = ""; // set fileName
File.WriteAllText(path, number);
}
});
Task.WaitAll(readFiles, processNumbers, createFiles);
It guarantees the execution in the correct order.
You can easily add cancellation using CancellationTokenSource.
Ok. Here is a working application that uses threads and timer.
It is assumed that execution all threads is faster than the timer interval.
New threads starts with each timer tick. The old threads will continue to work if they have not yet ended.
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Button stopButton;
System.Windows.Forms.Timer timer;
public Form1()
{
//InitializeComponent();
stopButton = new Button { Parent = this, Text = "Stop" };
stopButton.Click += StopButton_Click;
timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
timer.Tick += Timer_Tick;
timer.Start();
Text = "Start";
}
private void StopButton_Click(object sender, EventArgs e)
{
timer.Stop();
Text = "Stop";
}
private void Timer_Tick(object sender, EventArgs e)
{
var inputTexts = new BlockingCollection<string>();
var inputNumbers = new BlockingCollection<string>();
var readFiles = new Thread(() =>
{
try
{
foreach (var file in Directory.EnumerateFiles("./Folder", "*.txt"))
{
string text = File.ReadAllText(file);
inputTexts.Add(text);
}
}
finally { inputTexts.CompleteAdding(); }
});
var processNumbers = new Thread(() =>
{
try
{
foreach (var text in inputTexts.GetConsumingEnumerable())
{
int result;
if (int.TryParse(text, out result))
inputNumbers.Add(text);
}
}
finally { inputNumbers.CompleteAdding(); }
});
var createFiles = new Thread(() =>
{
int counter = 0;
foreach (var number in inputNumbers.GetConsumingEnumerable())
{
File.WriteAllText("./Folder2/" + counter + ".txt", number);
counter++;
}
});
readFiles.Start();
processNumbers.Start();
createFiles.Start();
readFiles.Join();
processNumbers.Join();
createFiles.Join();
}
}
}

Stream Writer is null before program exits

I am trying to add a logfile to a program I am making, but when I tell the main thread to exit (using Application.ExitThread()), logstrmWriter is suddenly null before it ever gets there. This is a very simple script.
private static FileStream appLogStream;
internal static string logFile;
internal static StreamWriter logstrmWriter;
public static void Main()
{
logFile = Application.StartupPath + #"\Archiver.log";
appLogStream = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.Read);
TextWriter logtxtWriter = Console.Out;
StreamWriter logstrmWriter = new StreamWriter(appLogStream);
if(!console) Console.SetOut(logstrmWriter);
Application.ApplicationExit += new EventHandler(OnApplicationExit);
Application.Run();
}
internal static void OnApplicationExit(object sender, EventArgs e)
{
active = false; Console.WriteLine("Main thread is shutting down. Sending Interrupt...");
Archiver.Stop(); Console.WriteLine("Shutdown. Log and Exit");
Console.WriteLine();
logstrmWriter.Flush();
logstrmWriter.Close();
logstrmWriter.Dispose();
}
You're creating a local variable that hides the static variable in question. As such, you never initialize logstrmWriter at all.
TextWriter logtxtWriter = Console.Out;
/* StreamWriter */ logstrmWriter = new StreamWriter(appLogStream); // Remove the redeclaration here!
if(!console) Console.SetOut(logstrmWriter);

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

I have a slight problem. What my application is supose to do, is to watch a folder for any newly copied file with the extention '.XSD' open the file and assign the lines to an array. After that the data from the array should be inserted into a MySQL database, then move the used file to another folder if it's done.
The problem is that the application works fine with the first file, but as soon as the next file is copied to the folder I get this exception for example: 'The process cannot access the file 'C:\inetpub\admission\file2.XPD' because it is being used by another process'.
If two files on the onther hand is copied at the same time there's no problem at all.
The following code is on the main window:
public partial class Form1 : Form
{
static string folder = specified path;
static FileProcessor processor;
public Form1()
{
InitializeComponent();
processor = new FileProcessor();
InitializeWatcher();
}
static FileSystemWatcher watcher;
static void InitializeWatcher()
{
watcher = new FileSystemWatcher();
watcher.Path = folder;
watcher.Created += new FileSystemEventHandler(watcher_Created);
watcher.EnableRaisingEvents = true;
watcher.Filter = "*.XPD";
}
static void watcher_Created(object sender, FileSystemEventArgs e)
{
processor.QueueInput(e.FullPath);
}
}
As you can see the file's path is entered into a queue for processing which is on another class called FileProcessor:
class FileProcessor
{
private Queue<string> workQueue;
private Thread workerThread;
private EventWaitHandle waitHandle;
public FileProcessor()
{
workQueue = new Queue<string>();
waitHandle = new AutoResetEvent(true);
}
public void QueueInput(string filepath)
{
workQueue.Enqueue(filepath);
if (workerThread == null)
{
workerThread = new Thread(new ThreadStart(Work));
workerThread.Start();
}
else if (workerThread.ThreadState == ThreadState.WaitSleepJoin)
{
waitHandle.Set();
}
}
private void Work()
{
while (true)
{
string filepath = RetrieveFile();
if (filepath != null)
ProcessFile(filepath);
else
waitHandle.WaitOne();
}
}
private string RetrieveFile()
{
if (workQueue.Count > 0)
return workQueue.Dequeue();
else
return null;
}
private void ProcessFile(string filepath)
{
string xName = Path.GetFileName(filepath);
string fName = Path.GetFileNameWithoutExtension(filepath);
string gfolder = specified path;
bool fileInUse = true;
string line;
string[] itemArray = null;
int i = 0;
#region Declare Db variables
//variables for each field of the database is created here
#endregion
#region Populate array
while (fileInUse == true)
{
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
StreamReader reader = new StreamReader(fs);
itemArray = new string[75];
while (!reader.EndOfStream == true)
{
line = reader.ReadLine();
itemArray[i] = line;
i++;
}
fs.Flush();
reader.Close();
reader.Dispose();
i = 0;
fileInUse = false;
}
#endregion
#region Assign Db variables
//here all the variables get there values from the array
#endregion
#region MySql Connection
//here the connection to mysql is made and the variables are inserted into the db
#endregion
#region Test and Move file
if (System.IO.File.Exists(gfolder + xName))
{
System.IO.File.Delete(gfolder + xName);
}
Directory.Move(filepath, gfolder + xName);
#endregion
}
}
The problem I get occurs in the Populate array region. I read alot of other threads and was lead to believe that by flushing the file stream would help...
I am also thinking of adding a try..catch for if the file process was successful, the file is moved to gfolder and if it failed, moved to bfolder
Any help would be awesome
Tx
You're not disposing of your FileStream instance, so a lock remains on the file. Change your code to use using blocks:
using (var fileStream = new FileStream(...))
{
using (var reader = new StreamReader(fileStream))
{
}
}
These using blocks will ensure the instances are correctly disposed of.
Also, why are you calling Flush on the file stream? You're not writing anything with it...
I would suggest :
1° use the using syntax on StreamReader
2° use the using syntax on FileStream

Categories