Try to update a form from another by using FileSystemWatcher


T

Tony Johansson

Hello!

I have two different forms. Each of these have a DataGridView where the data
that is displayed is fetched from a textfile.
When I change in one DataGridView I want the other to reload the data from
the textfile and redisplay the DataGridView.

I have listed relevant code for my question below. The class is called Cash
and the other is called Stock.
It's in the Stock form that I change the textfile.
Valiables that start with _ is class instance object.

So here is what happens when I change the textfile from the other
form(Stock) I know that the
watcher_Changed is called because in Cash form I have set a breakpoint here.
When I saw that the watcher_Changed was called I just hit F5(continue) but
the DataGridView in the Cash form didn't
reload. I made another try and when the breakpoint was trigged I tried to
step in with F11 but that was not possible.
When I hit step in(F11) it just as if I had hit F5.

So can anybody give me some hint why the form with the DataGridView is not
reloading.

public partial class Cash : Form
{
DataGridViewRow _currentRow;
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
List<Product> _myReceiptList = new List<Product>();
FileSystemWatcher _watcher;

public Cash()
{
InitializeComponent();
_bindingSource.DataSource = _dataTable;
dgCash.DataSource = _bindingSource;
_watcher = new FileSystemWatcher();
_watcher.Changed += new FileSystemEventHandler(watcher_Changed);

WatchFile();
}

private void WatchFile()
{
watcher.Path =
Path.GetDirectoryName(FileManager.GetFullFileName());
watcher.Filter = Path.GetFileName(FileManager.GetFullFileName());
watcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.Size;
watcher.EnableRaisingEvents = true;
}

void watcher_Changed(object sender, FileSystemEventArgs e)
{
Common.FormLoad(_dataTable, dgCash);
}

private void Cash_Load(object sender, EventArgs e)
{
Common.FormLoad(_dataTable, dgCash);
dgCash.Columns[ColumnName.Name.ToString()].ReadOnly = true;
dgCash.Columns[ColumnName.Price.ToString()].ReadOnly = true;
dgCash.Columns[ColumnName.Count.ToString()].ReadOnly = true;
dgCash.AllowUserToAddRows = false;
}

This method FormLoad is located in class Common
*****************************
public static void FormLoad(DataTable dataTable, DataGridView dgv)
{
List<string> myList = new List<string>();
string[] rowData;
if (dataTable.Columns.Count == 0)
{
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Name.ToString(),
ColumnName.Name.ToString(), "System.String"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Price.ToString(),
ColumnName.Price.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.ProdNr.ToString(),
ColumnName.ProdNr.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Count.ToString(),
ColumnName.Count.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Type.ToString(),
ColumnName.Type.ToString(), "System.String"));
}

if (FileManager.ExistProductFile())
myList = FileManager.GetAllProducts();

foreach (string line in myList)
{
rowData = line.Split(new char[] { ',' });
Common.CreateNewRow(rowData, dataTable);
}

dgv.Columns[ColumnName.Name.ToString()].Width = 160;
dgv.Columns[ColumnName.Price.ToString()].Width = 80;
dgv.Columns[ColumnName.ProdNr.ToString()].Width = 80;
dgv.Columns[ColumnName.Count.ToString()].Width = 80;
dgv.AllowUserToDeleteRows = false;
dgv.Columns[ColumnName.ProdNr.ToString()].ReadOnly = true;
dataTable.AcceptChanges();
}

//Tony
 
Ad

Advertisements

J

joecool1969

Hello!

I have two different forms. Each of these have a DataGridView where the data
that is displayed is fetched from a textfile.
When I change in one DataGridView I want the other to reload the data from
the textfile and redisplay the DataGridView.

I have listed relevant code for my question below. The class is called Cash
and the other is called Stock.
It's in the Stock form that I change the textfile.
Valiables that start with _ is class instance object.

So here is what happens when I change the textfile from the other
form(Stock) I know that the
watcher_Changed is called because in Cash form I have set a breakpoint here.
When I saw that the watcher_Changed was called I just hit F5(continue) but
the DataGridView in the Cash form didn't
reload. I made another try and when the breakpoint was trigged I tried to
step in with F11 but that was not possible.
When I hit step in(F11) it just as if I had hit F5.

So can anybody give me some hint why the form with the DataGridView is not
reloading.

public partial class Cash : Form
{
      DataGridViewRow _currentRow;
      DataTable       _dataTable     = new DataTable();
      BindingSource   _bindingSource = new BindingSource();
      List<Product>   _myReceiptList = new List<Product>();
      FileSystemWatcher _watcher;

      public Cash()
      {
         InitializeComponent();
         _bindingSource.DataSource = _dataTable;
         dgCash.DataSource = _bindingSource;
         _watcher = new FileSystemWatcher();
         _watcher.Changed += new FileSystemEventHandler(watcher_Changed);

         WatchFile();
      }

      private void WatchFile()
      {
         watcher.Path =
Path.GetDirectoryName(FileManager.GetFullFileName());
         watcher.Filter = Path.GetFileName(FileManager.GetFullFileName());
         watcher.NotifyFilter = NotifyFilters.LastWrite |
            NotifyFilters.FileName | NotifyFilters.Size;
         watcher.EnableRaisingEvents = true;
      }

      void watcher_Changed(object sender, FileSystemEventArgs e)
      {
            Common.FormLoad(_dataTable, dgCash);
      }

      private void Cash_Load(object sender, EventArgs e)
      {
         Common.FormLoad(_dataTable, dgCash);
         dgCash.Columns[ColumnName.Name.ToString()].ReadOnly  = true;
         dgCash.Columns[ColumnName.Price.ToString()].ReadOnly = true;
         dgCash.Columns[ColumnName.Count.ToString()].ReadOnly = true;
         dgCash.AllowUserToAddRows = false;
      }

This method FormLoad is located in class Common
*****************************
public static void FormLoad(DataTable dataTable, DataGridView dgv)
      {
         List<string> myList = new List<string>();
         string[] rowData;
         if (dataTable.Columns.Count == 0)
         {
            dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Name.ToString(),
ColumnName.Name.ToString(), "System.String"));
            dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Price.ToString(),
ColumnName.Price.ToString(), "System.Int32"));
            dataTable.Columns.Add(Common.GetNewColumn(ColumnName.ProdNr.ToString(),
ColumnName.ProdNr.ToString(), "System.Int32"));
            dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Count.ToString(),
ColumnName.Count.ToString(), "System.Int32"));
            dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Type.ToString(),
ColumnName.Type.ToString(), "System.String"));
         }

         if (FileManager.ExistProductFile())
            myList = FileManager.GetAllProducts();

         foreach (string line in myList)
         {
            rowData = line.Split(new char[] { ',' });
            Common.CreateNewRow(rowData, dataTable);
         }

         dgv.Columns[ColumnName.Name.ToString()].Width = 160;
         dgv.Columns[ColumnName.Price.ToString()].Width = 80;
         dgv.Columns[ColumnName.ProdNr.ToString()].Width = 80;
         dgv.Columns[ColumnName.Count.ToString()].Width = 80;
         dgv.AllowUserToDeleteRows = false;
         dgv.Columns[ColumnName.ProdNr.ToString()].ReadOnly =true;
         dataTable.AcceptChanges();
      }

//Tony

If I read your question correctly, quick answer - use a custom event.

I'll let you figure out how, unless someone else wants to take the
time to key in some sample code.

But this link might help you to get started:

http://groups.google.com/group/micr...=en&lnk=gst&q=joecool+events#e0fb60ac088d21d0
 
T

Tony Johansson

Hello!

It works now. The problem was that I had to clear my DataTable
The code I added was the code below. This code is located in the
Common.FormLoad
if (dataTable.Rows.Count > 0)
{
dataTable.Rows.Clear();
}

//Tony

Peter Duniho said:
[...]
So can anybody give me some hint why the form with the DataGridView is
not
reloading.

Not without the actual code, no.

What you posted is both not "concise", as it includes a number of things
that are most certainly not required in order to reproduce the problem,
nor "complete", as you have at least one method that can't possibly even
compile given what you've shown so far even if we assume the usual
Designer-created code, and of course you've left out all sorts of other
parts of the implementation.

Reduce the problem to a concise-but-complete code example that reliably
demonstrates the problem, and post that here.

Pete
 
T

Tony Johansson

Hello!

Does anyone understand anything from the exception message below what my
problem might be ?
I works sometimes but most of the times it crashes with the following
exception.
"Unhandled exception has occurred in your application. If You click
Continue,
the application will ignore this error and attempt to continue. If you click
Quit
, the application will close immediaterly.

Collection was modified; enimeration operation might not execute.
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidOperationException: Collection was modified; enumeration
operation might not execute.
at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
at MediaShop.Cash.Save() in F:\C#\Ovningar\MediaShop\Cash.cs:line 73
at MediaShop.Cash.BtnSave_Click(Object sender, EventArgs e) in
F:\C#\Ovningar\MediaShop\Cash.cs:line 142
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.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
MediaShop
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///F:/C%23/Ovningar/MediaShop/bin/Debug/MediaShop.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>


Tony Johansson said:
Hello!

It works now. The problem was that I had to clear my DataTable
The code I added was the code below. This code is located in the
Common.FormLoad
if (dataTable.Rows.Count > 0)
{
dataTable.Rows.Clear();
}

//Tony

Peter Duniho said:
[...]
So can anybody give me some hint why the form with the DataGridView is
not
reloading.

Not without the actual code, no.

What you posted is both not "concise", as it includes a number of things
that are most certainly not required in order to reproduce the problem,
nor "complete", as you have at least one method that can't possibly even
compile given what you've shown so far even if we assume the usual
Designer-created code, and of course you've left out all sorts of other
parts of the implementation.

Reduce the problem to a concise-but-complete code example that reliably
demonstrates the problem, and post that here.

Pete
 
T

Tony Johansson

I must add one more thing the other DataGridView will alway be updated in
spite of the exception
message

//Tony

Tony Johansson said:
Hello!

Does anyone understand anything from the exception message below what my
problem might be ?
I works sometimes but most of the times it crashes with the following
exception.
"Unhandled exception has occurred in your application. If You click
Continue,
the application will ignore this error and attempt to continue. If you
click Quit
, the application will close immediaterly.

Collection was modified; enimeration operation might not execute.
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidOperationException: Collection was modified; enumeration
operation might not execute.
at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
at MediaShop.Cash.Save() in F:\C#\Ovningar\MediaShop\Cash.cs:line 73
at MediaShop.Cash.BtnSave_Click(Object sender, EventArgs e) in
F:\C#\Ovningar\MediaShop\Cash.cs:line 142
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.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
MediaShop
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///F:/C%23/Ovningar/MediaShop/bin/Debug/MediaShop.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase:
file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>


Tony Johansson said:
Hello!

It works now. The problem was that I had to clear my DataTable
The code I added was the code below. This code is located in the
Common.FormLoad
if (dataTable.Rows.Count > 0)
{
dataTable.Rows.Clear();
}

//Tony

Peter Duniho said:
On Thu, 07 May 2009 10:29:21 -0700, Tony Johansson

[...]
So can anybody give me some hint why the form with the DataGridView is
not
reloading.

Not without the actual code, no.

What you posted is both not "concise", as it includes a number of things
that are most certainly not required in order to reproduce the problem,
nor "complete", as you have at least one method that can't possibly even
compile given what you've shown so far even if we assume the usual
Designer-created code, and of course you've left out all sorts of other
parts of the implementation.

Reduce the problem to a concise-but-complete code example that reliably
demonstrates the problem, and post that here.

Pete
 
T

Tony Johansson

Hello!

Here I pass you relevant code which will come from three classes.
The first class is called Cash and the second class is called Common where I
have put code
that is the same between my two forms where I have a DataGridView located in
each.
The third class is some code where I handle file handling. In the
FileManager I have a method called FileInUse where I have found on the net.
This should be used to check if the file is occupied or not.
I do hope that you will find the problem!!

using System;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
public partial class Cash : Form
{
DataGridViewRow _currentRow;
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
List<Product> _myReceiptList = new List<Product>();
FileSystemWatcher _watcher;

public Cash()
{
InitializeComponent();
_bindingSource.DataSource = _dataTable;
dgCash.DataSource = _bindingSource;
_watcher = new FileSystemWatcher();
_watcher.Changed += new FileSystemEventHandler(watcher_Changed);
Common.WatchFile(_watcher);
}

void watcher_Changed(object sender, FileSystemEventArgs e)
{
Common.FormLoad(_dataTable, dgCash);
}

private void Cash_Load(object sender, EventArgs e)
{
Common.FormLoad(_dataTable, dgCash);
dgCash.Columns[ColumnName.Name.ToString()].ReadOnly = true;
dgCash.Columns[ColumnName.Price.ToString()].ReadOnly = true;
dgCash.Columns[ColumnName.Count.ToString()].ReadOnly = true;
dgCash.AllowUserToAddRows = false;
}
}
}

//Here is the file for class Common
**************************
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace MediaShop
{
enum ColumnName { Name, Price, ProdNr, Count, Type, Year, Month };
static class Common
{
public static void WatchFile(FileSystemWatcher watcher)
{
watcher.Path =
Path.GetDirectoryName(FileManager.GetFullFileName());
watcher.Filter = Path.GetFileName(FileManager.GetFullFileName());
watcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.Size;
watcher.EnableRaisingEvents = true;
}

public static DataColumn GetNewColumn(string columnName, string
columnCaption, string columnType)
{
DataColumn dataColumn = new DataColumn(columnName,
System.Type.GetType(columnType));
if (columnName == ColumnName.ProdNr.ToString())
{
dataColumn.AutoIncrement = true;
dataColumn.AutoIncrementSeed = 1;
}
if (columnName == ColumnName.Name.ToString())
dataColumn.Unique = true;
dataColumn.Caption = columnCaption;
return dataColumn;
}

public static void CreateNewRow(string[] rowData, DataTable dataTable)
{
DataRow dataRow = dataTable.NewRow();
int i = 0;

foreach (string fieldData in rowData)
dataRow[i++] = fieldData;

dataTable.Rows.Add(dataRow);
}


public static void FormLoad(DataTable dataTable, DataGridView dgv)
{
if (dataTable.Rows.Count > 0)
dataTable.Rows.Clear();

List<string> myList = new List<string>();
string[] rowData;
if (dataTable.Columns.Count == 0)
{
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Name.ToString(),
ColumnName.Name.ToString(), "System.String"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Price.ToString(),
ColumnName.Price.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.ProdNr.ToString(),
ColumnName.ProdNr.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Count.ToString(),
ColumnName.Count.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Type.ToString(),
ColumnName.Type.ToString(), "System.String"));
}

if (FileManager.ExistProductFile())
{
int count = 0;

while (FileManager.FileInUse() && count < 100)
{
count++;
}

myList = FileManager.GetAllProducts();
}

foreach (string line in myList)
{
rowData = line.Split(new char[] { ',' });
Common.CreateNewRow(rowData, dataTable);
}

dgv.Columns[ColumnName.Name.ToString()].Width = 160;
dgv.Columns[ColumnName.Price.ToString()].Width = 80;
dgv.Columns[ColumnName.ProdNr.ToString()].Width = 80;
dgv.Columns[ColumnName.Count.ToString()].Width = 80;
dgv.AllowUserToDeleteRows = false;
dgv.Columns[ColumnName.ProdNr.ToString()].ReadOnly = true;
dataTable.AcceptChanges();
}
}
}

//Here is some code from class FileManager
*********************************
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;

namespace MediaShop
{
class FileManager
{
public static List<string> GetAllProducts()
{
List<string> myList = new List<string>();
string line;
using (TextReader tr = new StreamReader(new FileStream(FILENAME,
FileMode.Open)))
{
while ((line = tr.ReadLine()) != null)
myList.Add(line);
}
return myList;
}

public static bool ExistProductFile()
{
return File.Exists(FILENAME) ? true : false;
}

public static bool FileInUse()
{
try
{
//Just opening the file as open/create
using (FileStream fs = new FileStream(FILENAME,
FileMode.OpenOrCreate))
{
//If required we can check for read/write by using fs.CanRead
or fs.CanWrite
}
return false;
}
catch
{
//check if message is for a File IO
//__message = ex.Message.ToString();
//if (__message.Contains("The process cannot access the file"))
return true;
//else
// throw;
}
}



This is code is auto generated and not written by me.
****************************************
Partial class Cash
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(Cash));
this.dgCash = new System.Windows.Forms.DataGridView();
this.BtnSell = new System.Windows.Forms.Button();
this.lblInfo = new System.Windows.Forms.Label();
this.btnRefund = new System.Windows.Forms.Button();
this.printDialog = new System.Windows.Forms.PrintDialog();
this.printDocument = new System.Drawing.Printing.PrintDocument();
this.BtnSave = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dgCash)).BeginInit();
this.SuspendLayout();
//
// dgCash
//
this.dgCash.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgCash.Location = new System.Drawing.Point(12, 118);
this.dgCash.Name = "dgCash";
this.dgCash.ReadOnly = true;
this.dgCash.Size = new System.Drawing.Size(544, 150);
this.dgCash.TabIndex = 0;
this.dgCash.DataError += new
System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dgCash_DataError);
this.dgCash.SelectionChanged += new
System.EventHandler(this.dgCash_SelectionChanged);
//
// BtnSell
//
this.BtnSell.Location = new System.Drawing.Point(345, 294);
this.BtnSell.Name = "BtnSell";
this.BtnSell.Size = new System.Drawing.Size(75, 23);
this.BtnSell.TabIndex = 1;
this.BtnSell.Text = "SELL";
this.BtnSell.UseVisualStyleBackColor = true;
this.BtnSell.Click += new System.EventHandler(this.BtnSell_Click);
//
// lblInfo
//
this.lblInfo.AutoSize = true;
this.lblInfo.Font = new System.Drawing.Font("Microsoft Sans Serif",
9.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.lblInfo.Location = new System.Drawing.Point(39, 26);
this.lblInfo.Name = "lblInfo";
this.lblInfo.Size = new System.Drawing.Size(550, 80);
this.lblInfo.TabIndex = 3;
this.lblInfo.Text = resources.GetString("lblInfo.Text");
//
// btnRefund
//
this.btnRefund.Location = new System.Drawing.Point(253, 294);
this.btnRefund.Name = "btnRefund";
this.btnRefund.Size = new System.Drawing.Size(75, 23);
this.btnRefund.TabIndex = 4;
this.btnRefund.Text = "REFUND";
this.btnRefund.UseVisualStyleBackColor = true;
this.btnRefund.Click += new
System.EventHandler(this.BtnRefund_Click);
//
// printDialog
//
this.printDialog.Document = this.printDocument;
this.printDialog.UseEXDialog = true;
//
// printDocument
//
this.printDocument.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler(this.PrintPage);
//
// BtnSave
//
this.BtnSave.Font = new System.Drawing.Font("Microsoft Sans Serif",
10.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.BtnSave.Location = new System.Drawing.Point(481, 283);
this.BtnSave.Name = "BtnSave";
this.BtnSave.Size = new System.Drawing.Size(75, 43);
this.BtnSave.TabIndex = 5;
this.BtnSave.Text = "Save";
this.BtnSave.UseVisualStyleBackColor = true;
this.BtnSave.Click += new System.EventHandler(this.BtnSave_Click);
//
// Cash
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(675, 349);
this.Controls.Add(this.BtnSave);
this.Controls.Add(this.btnRefund);
this.Controls.Add(this.lblInfo);
this.Controls.Add(this.BtnSell);
this.Controls.Add(this.dgCash);
this.Name = "Cash";
this.Text = "CashFunctions";
this.Load += new System.EventHandler(this.Cash_Load);
((System.ComponentModel.ISupportInitialize)(this.dgCash)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.DataGridView dgCash;
private System.Windows.Forms.Button BtnSell;
private System.Windows.Forms.Label lblInfo;
private System.Windows.Forms.Button btnRefund;
private System.Windows.Forms.PrintDialog printDialog;
private System.Drawing.Printing.PrintDocument printDocument;
private System.Windows.Forms.Button BtnSave;

}
}


//Tony

Peter Duniho said:
Hello!

Does anyone understand anything from the exception message below what my
problem might be ?
[...]

************** Exception Text **************
System.InvalidOperationException: Collection was modified; enumeration
operation might not execute.

Seems pretty self-explanatory to me.

I haven't seen the RBTree class...looks like it's probably an
internal/private generic class used within .NET (the name is consistent
with a "red/black tree"). But, obviously you've got some code somewhere
that is modifying a collection at the same time that some other code is
trying to enumerate it. This could be happening because of threading
issues, or on a single thread due to some form of re-entrancy in the code.

Of course, as before, it's not really possible to say anything more
specific unless you post a proper code example.

Pete
 
Ad

Advertisements

C

Cor Ligthert[MVP]

Pete,

Tony wants to create a full concurrent application with textiles.

I am unable to tell him that it is impossible (it is about real textiles not
a binary file) while it is simple with a freeware database.

Maybe you are able to do that.

Cor
 
Ad

Advertisements

C

Cor Ligthert[MVP]

Tony,

I hope Pete understand it, I am not able to tell you that you go in my idea
in the wrong direction, doing a lot of work and at the end you see the
problem.

Maybe Pete can tell that better than I, there is nothing aggressive or
whatever meant.

Cor
 

Top