Click here to Skip to main content
15,905,867 members

Comments by Member 3652617 (Top 5 by date)

Member 3652617 21-Jun-16 15:11pm View    
Thanks Marc, but I still don't get how to do it. I tried different versions of
this.Invoke(colorTextbox(), new object[] { lines });

but can't figure it out.
Member 3652617 17-Jun-16 15:47pm View    
Oops, guess I can't post that much code.
so working function is

private void setupdatagrid()
{

if (this.InvokeRequired)
{
this.Invoke(new Action(() => setupdatagrid()));
}
else
{
dataGridView1.Rows.Clear();
dataGridView1.ColumnCount = 6;
.......

and the one I cant figure out is
private void colorTextbox(int lines)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => colorTextbox()));// what is the correct Syntax?
}
else
{

int lineNumberToSelect = 0;
...........
Member 3652617 17-Jun-16 15:45pm View    
That makes sense. I put in an evil thread.sleep and that fixed it. Now I "think" I just need 1 more piece of syntax help.
in setupdatagrid I used
if (this.InvokeRequired)
{
this.Invoke(new Action(() => setupdatagrid()));
}
and it works, but I can't figure out the syntxx for
colorTextbox(int lines) to do the same thing.
See below and thanks for all the help




<pre lang="c#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Security.Permissions;


namespace xml_reader
{

public partial class Form1 : Form
{
string filename;
public Form1()
{
InitializeComponent();
setupdatagrid();

}

private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Multiline = true;
// richTextBox1.AcceptsTab = true;
richTextBox1.SelectionColor = Color.Red;


}

private void btn_read_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "XML Files (*.xml)|*.xml";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog1.FileName;
readXML(openFileDialog1.FileName);

FileSystemWatcher watcher = new FileSystemWatcher();
string directoryName = Path.GetDirectoryName(openFileDialog1.FileName);
watcher.Path = directoryName;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch xml files.
watcher.Filter = "*.xml";

// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);

// Begin watching.
// timer1.Enabled = true;
// or
watcher.EnableRaisingEvents = true;

}
}

private void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
string path = e.FullPath;
Console.WriteLine("File: " + path + " " + e.ChangeType);
System.Threading.Thread.Sleep(1000);
readXML(path);
}

private void readXML(string Filename)
{
int linenumbers = 0;
string stringvalue;
string Pos = "POS";
string Pilot = "PILOT";
string laps = "LAPS";
string elapsed = "ELAPSED";
string seed = "SEED";
string fast = "FAST LAP";
string[] row;
setupdatagrid();

const string title = "POS PILOT LAPS ELAPSED SEED FAST LAP \n";
StringBuilder builder = new StringBuilder(title, 1024); // 1k is a fair starting "guess" ;-)
using (XmlTextReader reader = new XmlTextReader(Filename))
{
// The using statement ensures that reader is Closed when done.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
if (reader.Name == "Driver")
{
while (reader.MoveToNextAttribute()) // Read the attributes.
{
stringvalue = reader.Value;
switch (reader.Name)
Member 3652617 17-Jun-16 12:59pm View    
Thanks, I couldn't get it to run with the line
Action updateText = () => {richTextBox1.Text = builder.ToString();}
so I took out the brakets and made it
Action updateText = () => richTextBox1.Text = builder.ToString();
not sure if that broke it or not, but now the error is
on line while(reader.Read())
and its An unhandled exception of type 'System.IO.IOException' occurred in System.Xml.dll

Additional information: The process cannot access the file 'C:\Users\ldavis\Dropbox\RC\DRC\xml\driverData.xml' because it is being used by another process.
Member 3652617 17-Jun-16 11:09am View    
Thanks Matt, I will definitely look at the data grid.I made the string changes you posted. When I remove static from the Onchange method, I then get an error on the line
richTextBox1.Text = builder.ToString();
I'm assuming I would get the same error if it were a data grid. The error is
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.

Its probably what Sergey tried to explain below but it went over my head. I'm obviously not a programmer. Thanks for explaining it and I'll read it a few more times, but can you help me with this error as well please. Thanks Again.