How can be setted Console.SetOut(TextWriter) with multi thread C#? - c#

Assuming I have a multi thread ForEach I would like that every cycle have the Console.WriteLine() converted into a file, the example I have found for mono thread is something like:
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
try
{
ostrm = new FileStream("C:\\Users\\Public\\Documents/Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter(ostrm);
}
catch (Exception e)
{
Console.WriteLine("Cannot open Redirect.txt for writing");
Console.WriteLine(e.Message);
return;
}
Console.SetOut(writer);
Now for multi thread I have try something like:
IList<String> testCaseList = ExcelDataAccess.GetTestCases();
Parallel.ForEach(testCaseList, new ParallelOptions { MaxDegreeOfParallelism = 1 }, currentTestCase =>`
{
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
try
{
ostrm = new FileStream("C:\\Users\\Public\\Documents/Redirect" + currentTestCase +".txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter(ostrm);
}
catch (Exception e)
{
Console.WriteLine("Cannot open Redirect.txt for writing");
Console.WriteLine(e.Message);
return;
}
Console.SetOut(writer);
Console.WriteLine("Running test: " + currentTestCase);
//Do some other stuff and some Console.WriteLine();
}
But in this way inside every file what is written is not what expected and multi thread looks not handled correctly. How to solve this?

Related

Process cannot access file because its used by another process

So I'm getting exception "Process cannot access file because its used by another process.
The exception will show when i call this particular method:
public async static Task<bool> DownloadFileFromFTP(string PathToFile, string AppName)
{
return await Task.Run(() => {
if (File.Exists("settings.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(Information));
FileStream read = new FileStream("settings.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Information info = (Information)xs.Deserialize(read);
try
{
FtpClient client = new FtpClient(info.HDSynologyIP);
string a = info.FtpPassword;
string FTPPassword = EncryDecryptor.Decrypt(a);
client.Credentials = new NetworkCredential(info.FtpUsername, FTPPassword);
client.Connect();
bool finish = client.DownloadFile(#info.Downloads + "\\" + AppName, PathToFile, FtpLocalExists.Overwrite, FluentFTP.FtpVerify.Retry);
if (finish == true)
{
client.Disconnect();
read.Close();
return true;
}
else
{
client.Disconnect();
read.Close();
return false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
read.Close();
}
read.Close();
}
else
{
MessageBox.Show("Missing settings.xml file");
return false;
}
return false;
});
}
And call it in another class like this:
await General_Functions.DownloadFileFromFTP("Ultra_Script/Basic_SW/Adobe_Reader.exe", "Adobe_Reader.exe");
It wasn't asynchronous before but i had to remake it into asynchronous method. But i think I'm closing reader and client correctly. Can it be problem of async method? Because i don't had this problem before i made it asynchronous.
Can someone explain what I'm doing wrong?
Note, I am using FluentFTP from https://github.com/robinrodricks/FluentFTP .
use using to terminate the file reader process
using(FileStream read = new FileStream("settings.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
// your code here.....
}

Value Being Overwritten When Reading From Text File

I am trying to create a list using the FileStream/StreamReader method. Everything works fine except the price calculation is reset every time a new line is added.
I believe the issue is with the save method. I am sure it is not caused from functions in my classes, since the price is showing properly. There seems to be an issue when saving the string.
This is my read method:
public static List<Customer> ReadCustomers()
{
// create an empty customer list
List<Customer> customerList = new List<Customer>();
// new Filestream
FileStream fs = null;
// new StreamReader
StreamReader sr = null;
Customer c; // for reading
string line;
string[] fields;
try
{
fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
sr = new StreamReader(fs);
while (!sr.EndOfStream)// while there is data
{
line = sr.ReadLine();
fields = line.Split(','); // split sections by commas
c = new Customer(); // initializes customer object
c.AccountNo = Convert.ToInt32(fields[0].Trim());
c.CustomerName = Convert.ToString(fields[1].Trim());
c.CustomerType = Convert.ToChar(fields[2].Trim());
c.CustomerCharge = Convert.ToDecimal(fields[3].Trim());
customerList.Add(c);
}
}
catch (Exception ex)
{
throw ex;
}
finally // always execute
{
if (fs != null) fs.Close(); // close file
}
return customerList;
}
This is where I try to save the string...
public static void SaveCustomers(List<Customer> list)
{
FileStream fs = null;
StreamWriter sw = null;
string line;
try
{
fs = new FileStream(path, FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs);
foreach (Customer c in list) // for each customer in the list
{
line = c.AccountNo.ToString() + ", " + c.CustomerName.ToString() + ", " +
c.CustomerType.ToString() + ", " + c.CustomerCharge.ToString(); // make a line with data
sw.WriteLine(line); // and write it to the file
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (sw != null) sw.Close(); // stream writer close
if (fs != null) fs.Close();
}
}
Calculation:
public override decimal CalculateCharge()
{
decimal peak;
decimal offpeak;
if (Kwh1 <= INDUST_BASE_HOURS)
{
peak = KWH_PEAK_BASE_PRICE;
}
else
{
peak = ((Kwh1 - INDUST_BASE_HOURS) * KWH_INDUST_PEAK) + KWH_PEAK_BASE_PRICE;
}
if (Kwh2 <= INDUST_BASE_HOURS)
{
offpeak = KWH_OFF_PEAK_BASE_PRICE;
}
else
{
offpeak = ((Kwh2 - INDUST_BASE_HOURS) * KWH_INDUST_OFFPEAK) + KWH_OFF_PEAK_BASE_PRICE;
}
return peak + offpeak;
}
In SaveCustomers(), are you sure you want to open the file:
fs = new FileStream(path, FileMode.Create, FileAccess.Write);
You may want:
fs = new FileStream(path, FileMode.Append, FileAccess.Write);
FileMode.Create will destroy the file if it exists.
FileMode.Append will append to an existing file it exists.
Maybe for the purposes of clarity around testing, you output to another file rather than the one you read in.
Try using this by using the append parameter:
new StreamWriter("c:\\file.txt", true);
http://msdn.microsoft.com/en-us/library/36b035cb.aspx
Or you can see related answers here, which had similar problems
C# add text to text file without rewriting it?

StreamWriter object doesn't work at all

This code works fine within SaveFileDialog
private void buttonSaveAs_Click(object sender, EventArgs e)
{
try
{
if (selectedFileInfo != null)
{
// Save File
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = explorerTree2.SelectedPath;
// set a default file name
saveFileDialog1.FileName = Path.GetFileNameWithoutExtension(selectedFileInfo.Name) + "-COPY" + selectedFileInfo.Extension;
// set filters - this can be done in properties as well
saveFileDialog1.Filter = "HTM files (*.htm)|*.htm|HTML files (*.html)|*.html|XML files (*.xml)|*.xml|Text files (*.txt)|*.txt|All files (*.*)|*.*";
#region Define filter index
if (selectedFileInfo.Extension.Equals(".htm")) // HTM files (*.htm)|*.htm
{
saveFileDialog1.FilterIndex = 1;
}
else // All files (*.*)|*.*
{
saveFileDialog1.FilterIndex = 5;
}
#endregion
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
{
sw.Write(scintilla1.Text);
sw.Flush();
sw.Close();
}
}
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
And another does not. And there NO is any error as well. Any clue?
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
if (selectedFileInfo != null)
{
using (FileStream fs = new FileStream(selectedFileInfo.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
StreamWriter sw = new StreamWriter(fs);
sw.AutoFlush = true;
sw.Write(scintilla1.Text);
sw.Flush();
sw.Close();
}
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
P.S. This option does not work as well
using (FileStream fs = new FileStream(selectedFileInfo.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.AutoFlush = true;
sw.Write(scintilla1.Text);
sw.Flush();
sw.Close();
}
}
I found correct solution
where scintilla1.Text is string you have to write
and selectedFileInfo is a FileInfo object. In your case scintilla1.Text might be like a string or RichTextBox or any other text editor control.
private void buttonSave_Click(object sender, EventArgs e)
{
if (selectedFileInfo != null)
{
FileStream stream = null;
try
{
stream = selectedFileInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
using (StreamWriter sw = new StreamWriter(stream))
{
sw.AutoFlush = true;
sw.Write(scintilla1.Text);
sw.Flush();
sw.Close();
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (stream != null)
stream.Close();
}
}
}

Renaming files and filesharing (filestream)

I'm using a filestream to access a file (making an md5 ComputeHash). If I attempt to rename the file during this time (which fails as the file is being accessed). So far so good, but when I then try to open the file anew after the original filestream is closed I get the info that the file is open in another process.
Code:
using (Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
MD5 md5 = MD5.Create();
byte[] mymd5computed = md5.ComputeHash(fileStream);
......
}
Thread.Sleep(50);
Thread a = new Thread (()=>{(FileStream sourceStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){....} });
Like I said if while during the computation of the MD5 I try to rename the file I get the info that the file is still locked.
The lock on a file sometimes isn't released right away when you close your stream, so there are some other solutions you can use to wait until you can access the file again. One of them is explained here: http://www.codeproject.com/Tips/164428/C-FileStream-Lock-How-to-wait-for-a-file-to-get-re.
Recap:
public static void Lock(string path, Action<FileStream> action) {
var autoResetEvent = new AutoResetEvent(false);
while(true)
{
try
{
using (var file = File.Open(path,
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.Write))
{
action(file);
break;
}
}
catch (IOException)
{
var fileSystemWatcher =
new FileSystemWatcher(Path.GetDirectoryName(path))
{
EnableRaisingEvents = true
};
fileSystemWatcher.Changed +=
(o, e) =>
{
if(Path.GetFullPath(e.FullPath) == Path.GetFullPath(path))
{
autoResetEvent.Set();
}
};
autoResetEvent.WaitOne();
}
}
}
Sample use:
Lock(#"c:\file",
(f) =>
{
try
{
f.Write(buf, 0, buf.Length);
}
catch(IOException ioe)
{
// handle IOException
}
});
Hope it helps! :)

reading and writing file using autoresetevent in C#

I have written a simple program of thread synchronization. But when I run this program I get an error "The process cannot access the file 'D:\Vivek.txt' because it is being used by another process." Why I am getting this error.
class Program
{
const string Filepath = "D:\\Vivek.txt";
static AutoResetEvent writerwaithandle= new AutoResetEvent(true);// Signaled state
static AutoResetEvent readerwaithandle = new AutoResetEvent(false);
static void Main()
{
if (File.Exists(Filepath))
{
File.Delete(Filepath);
}
File.CreateText(Filepath);
CreateWriterThread();
CreateReaderThread();
Console.ReadKey();
}
private static void CreateWriterThread()
{
for (int i = 1; i <= 10;i++ )
{
var thread = new Thread(WriteFile);
thread.Name = "Writer " + i;
thread.Start();
Thread.Sleep(250);
}
}
private static void CreateReaderThread()
{
for (int i = 1; i <= 10; i++)
{
var thread = new Thread(ReadFile);
thread.Name = "Reader " + i;
thread.Start();
}
}
private static void WriteFile()
{
writerwaithandle.WaitOne();
var stream = new FileStream(Filepath, FileMode.Append);
var streamwriter = new StreamWriter(stream);
streamwriter.WriteLine("written by"+Thread.CurrentThread.Name+DateTime.Now));
streamwriter.Flush();
streamwriter.Close();
readerwaithandle.Set();
}
private static void ReadFile()
{
readerwaithandle.WaitOne();
if (File.Exists(Filepath))
{
var stream = new FileStream(Filepath, FileMode.Open);
var streamreader = new StreamReader(stream);
var text = streamreader.ReadToEnd();
streamreader.Close();
Console.WriteLine("Read by thread {0} \n",Thread.CurrentThread.Name);
Console.WriteLine(text);
}
writerwaithandle.Set();
}
}
when I replace the code from
if (File.Exists(Filepath))
{
File.Delete(Filepath);
}
File.CreateText(Filepath);
to
if (!File.Exists(Filepath))
{
File.CreateText(Filepath);
}
the program shows the same error for first time. after that it never gives any error.
please anyone tell me the bug area, reason and what should be the best solution.
When you use FileStream always use it with using like
using (var fileStream = new FileStream(Filepath, FileMode.Open))
{
Your code...
}
This ensures that the stream gets disposed properly.
You can also use the StreamReader along with using
using (var fileStream = new FileStream(Filepath, FileMode.Open))
{
using (var reader = new StreamReader(stream))
{
Your code...
}
}
Be vigilant, look at the documentation of File.CreateText
Creates or opens a file for writing UTF-8 encoded text.
Return Value Type: System.IO.StreamWriter A StreamWriter that writes
to the specified file using UTF-8 encoding
It means that there is no need to create a new FileStream because FileStream is already created and returned when you use File.CreateText. You should only use that created FileStream in your code.
Here is the fixed version of your code:
class Program
{
const string Filepath = "D:\\Vivek.txt";
static AutoResetEvent writerwaithandle = new AutoResetEvent(true);// Signaled state
static AutoResetEvent readerwaithandle = new AutoResetEvent(false);
static void Main()
{
if (File.Exists(Filepath))
{
File.Delete(Filepath);
}
//File.CreateText(Filepath);
CreateWriterThread();
CreateReaderThread();
Console.ReadKey();
}
private static void CreateWriterThread()
{
for (int i = 1; i <= 10; i++)
{
var thread = new Thread(WriteFile);
thread.Name = "Writer " + i;
thread.Start();
Thread.Sleep(250);
}
}
private static void CreateReaderThread()
{
for (int i = 1; i <= 10; i++)
{
var thread = new Thread(ReadFile);
thread.Name = "Reader " + i;
thread.Start();
}
}
private static void WriteFile()
{
writerwaithandle.WaitOne();
var streamwriter = File.CreateText(Filepath);
//var stream = new FileStream(Filepath, FileMode.Append);
//var streamwriter = new StreamWriter(stream);
streamwriter.WriteLine("written by" + Thread.CurrentThread.Name + DateTime.Now);
streamwriter.Flush();
streamwriter.Close();
readerwaithandle.Set();
}
private static void ReadFile()
{
readerwaithandle.WaitOne();
if (File.Exists(Filepath))
{
var stream = new FileStream(Filepath, FileMode.Open);
var streamreader = new StreamReader(stream);
var text = streamreader.ReadToEnd();
streamreader.Close();
Console.WriteLine("Read by thread {0} \n", Thread.CurrentThread.Name);
Console.WriteLine(text);
}
writerwaithandle.Set();
}
}

Categories