473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with cross-thread UI functionality...

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);
}
}
}
}

Feb 26 '07 #1
4 3366
sherifffruitfly <sh*************@gmail.comwrote:
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?
The pause button will be responsive automatically, because you're
correctly not blocking the UI thread. Now all you need to do is make
ReadCsv take notice of the pause button. You'll need some kind of state
to say whether or not to pause, and you'll probably want to use
Monitor.Wait (on the ReadCsv thread) and Monitor.Pulse (on the UI
thread) when you want to "unpause".

See http://pobox.com/~skeet/csharp/threads for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 26 '07 #2
On Feb 25, 11:33 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
sherifffruitfly <sherifffruit...@gmail.comwrote:
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?

The pause button will be responsive automatically, because you're
correctly not blocking the UI thread. Now all you need to do is make
ReadCsv take notice of the pause button.
<snip>

You can do that by having the Pause button set some field in the
class, say this._paused = true. Then have your background thread
method poll the this._paused field at strategic intervals. If it ever
finds it set to true, then as Jon said, the method running on the
background thread should take a nap and wait for the UI thread to
"kick" it to start it again.

So, from the UI thread's point of view, turning Pause "on" means
setting this._paused to true; turning Pause "off" means setting
this._paused to false and then giving the background thread a "kick".
>From the background thread's point of view, poll this._paused at
regular intervals. If it's ever true, pause and wait to be "kicked"
awake again.

Feb 26 '07 #3
You can do that by having the Pause button set some field in the
class, say this._paused = true. Then have your background thread
method poll the this._paused field at strategic intervals.
Minor note (more info in Jon's details): you should sync. around
_paused,
or mark volatile, otherwise the background thread may be keeping
a private cached copy.

Also (Bruce): I very much like the idea of Monitor.Kick(),
Monitor.KickAll() - it
seems a lot closer to how we expect it to behave <evil grin/>

Marc

Feb 26 '07 #4
On Feb 25, 11:33 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
The pause button will be responsive automatically, because you're
correctly not blocking the UI thread.
Not through any genius of my own - it's the result of copying/
adjusting code that *you* provided someone else in this newsgroup -
lol!
Seehttp://pobox.com/~skeet/csharp/threadsfor more information.
It's appearing as though there are few questions I have that aren't
significantly addressed by one of your articles. I think I'll start
there in the future, before asking the group :)
Thanks for the tips everyone!

Feb 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Vanessa BJ | last post by:
Hi. I am an amateur Java enthusiast. I currently work as a carpenter and I moonlight on the weekends as a photographer. I'm trying to transition a career change. Which Java exam should I start...
2
by: News | last post by:
Folks, I need help with this task. I have a set of data that needs to be plotted on timeline chart. Example: Unit ProcStart ProcEnd Machine U1 5/5/03 6:01 5/5/03 6:04 M1 U2 ...
6
by: moi | last post by:
hi, im fairly new to c++ and as part of a module at uni im learning it. i know the basics i suppose, but as our final hand-in we have to alter code we wrote for an earlier assignment to use...
3
by: amanda | last post by:
Hope someone can help me with this - I've been staring at it stupidly for hours now, convinced there must be an easy way to achieve the results I want: I have a very large table recording every...
1
by: D Witherspoon | last post by:
Coming up with a scenario here. For example there is the standard .NET MailMessage class. I am creating a project (let's call it CommonBase) that has the following 2 classes ...
6
by: mutemode | last post by:
I have this query SELECT 'bracket' = CASE WHEN income BETWEEN 0 AND 49 THEN '0-49' WHEN income BETWEEN 50 AND 99 THEN '50-99' WHEN income BETWEEN 100 AND 499 THEN '100-499' WHEN income BETWEEN...
23
by: Simon Wittber | last post by:
I've just bought a new notebook, which has a dual core CPU. I write cross platform games in Python, and I'd really like to be able to use this second core (on my machine, and on user's machines)...
5
by: a Wellner | last post by:
I am trying to create a from that the user can select a street from a combo box (named street), then pick a second street from another combo box, containing only valid cross streets(named Cross). ...
2
by: deshg | last post by:
Hey everyone, I am a php programmer originally and am just helping a friend of mine update their website that they paid a designer (well that's what he called himself!) to do ages ago. I have...
8
by: aeneng | last post by:
Hello everyone, I am just starting to use python in numerical cacluation. I need you to help me to see what's wrong with the following piece of codes, which computes the cross product of two...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.