hi guys,
Right now , I'm reading from a file and generating some statistics
the following is the code :
static FileSystemWatcher _watcher;
static void Init()
{
string directory =
MainPage._watcher = new FileSystemWatcher(directory);
MainPage._watcher.Changed += new FileSystemEventHandler(MainPage._watcher_Changed);
MainPage._watcher.EnableRaisingEvents = true;
}
static void _watcher_Changed(object sender, FileSystemEventArgs e)
{
}
private void MainPage_Load(object sender, EventArgs e)
{
OperationClass oc = new OperationClass();
oc.LoadFileForGenerationData();
List<string> username = oc.gettingUserData();
List<int> amountoftrn = oc.getAmountOfTrn();
fillChart(username, amountoftrn);
List<int> counters = oc.LoadFileForNumberOfErrors();
lblTotalNumberOfUsers.Text = username.Count().ToString();
lblTotalNumberOfTrns.Text = amountoftrn.Sum(x => Convert.ToInt32(x)).ToString();
lblErrors.Text = counters[0].ToString();
lblFatals.Text = counters[1].ToString();
lblWarnings.Text = counters[2].ToString();
Init();
}
private void fillChart(List<string> username, List<int> amountoftrn)
{
TransactionChart.Titles.Add("Transaction Chart");
int i = 0;
foreach (var uname in username)
{
TransactionChart.Series["Transactions"].Points.AddXY(username[i], amountoftrn[i]);
i++;
}
}
So i have a FileSystemWatcher on a directory so when there is a change in this directory i will reload the mainform so the _load method will run again and the statistics will be updated.
--------------------------------------------------------------
UPDATE:
I have made a new update thanks to
lw@zi .
- Changed the filewatcher method to non-static
- made a method and put the _load content in it and called it from _changed and _load
The new problem is when i try to reload the statistics an IO exception is unhandled. this is the following code where the program is crashing (in the "using" line):
using (var filestream = new FileStream("//path to file.", FileMode.Open, FileAccess.Read,FileShare.None))
{
MyTextFileDataSet.ContentExpression = new Regex(@"^(?<date>[^ ]+) (?<time>[^A-Z]+) (?<errorMessage>[^[]+) \[1\] (?<programName>[^.]+)[.](?<formName>[^.]+)[.](?<event>[^ ]+)[^a-z]+(?<username>[^:]+):(?<message>[^.]+).+$", RegexOptions.Multiline);
MyTextFileDataSet.Fill(filestream);
filestream.Flush();
filestream.Dispose();
}
----------------------------------------------------------
If there is any problem or you did not understand do not hesitate to comment.
What I have tried:
I tried several methods to reload the MainPage (I got only 1 form.)
MainPage.ActiveForm.Dispose()
MainPage frm = new MainPage()
frm.Show()
---------
MainPage frm = new MainPage()
frm.refresh(), reload.....
when tried to dispose a nullreference excception was given.. Can someone tell me what can i do or maybe there is a workaround ?
-----------------------------------------------------------------
UPDATE OF WHAT HAVE YOU TRIED:
I tried to chnage the fileshare to ReadWrite but then a problem comes when i try to log with the log4net from another solution.
maybe it can help you, this is the app.config of the log4net.
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="TestAppender" />
</root>
<appender name="TestAppender" type="log4net.Appender.RollingFileAppender">
<lockingmodel type="log4net.Appender.FileAppender+MinimalLock"/>
<param name="File" value="//same path of the filestream." />
<param name="AppendToFile" value ="true"/>
<encoding value="utf-8" />
<rollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level [%thread] %type.%method %message%n" />
</layout>
</appender>
</log4net>
Someone can tell me what can i do ?
---------------------------------------------------