Hi all,
I'm trying to to figure out this stuff, and I've got part of the trick
in the code below, which updates a textbox with data from another
thread. What I'd like to do is put a Pause button on my form which
basically pauses the ReadCsv() method wherever it's at. Obviously, the
Pause button would need to be responsive to the user during while
ReadCsv() is churning. How do I do this?
Thanks for any pointers (heh-heh),
cdj
===============
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LumenWorks.Framework.IO.Csv;
using System.IO;
using System.Threading;
namespace cdjCsvTest
{
public partial class CsvTest : Form
{
public CsvTest()
{
InitializeComponent();
}
private void btn_Go_Click(object sender, EventArgs e)
{
Thread t = new Thread(ReadCsv);
t.Start();
}
private void ReadCsv()
{
CsvReader cr = new CsvReader(new StreamReader(@"c:
\myFavorite.csv"),true);
int numFields = cr.FieldCount;
string[] headers = cr.GetFieldHeaders();
string updateText = "";
int tooMuch = 200;
int curr = 0;
while (cr.ReadNextRecord() && curr<tooMuch)
{
for (int i = 0; i < numFields; i++)
{
updateText = headers[i] + ": " + cr[i] +
Environment.NewLine;
UpdateText(updateText);
}
UpdateText(Environment.NewLine);
curr++;
}
}
delegate void UpdateTextDelegate(string param);
private void UpdateText(string param)
{
if (this.txt_CsvText.InvokeRequired)
{
//We're in the wrong thread, so this gets us to the
right one
this.txt_CsvText.Invoke(new
UpdateTextDelegate(UpdateText), new object[] { param });
}
else
{
//Now we're in the right thread.
txt_CsvText.AppendText(param);
}
}
}
}