473,796 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using LumenWorks.Fram ework.IO.Csv;
using System.IO;
using System.Threadin g;

namespace cdjCsvTest
{
public partial class CsvTest : Form
{
public CsvTest()
{
InitializeCompo nent();
}

private void btn_Go_Click(ob ject 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.GetFieldHead ers();

string updateText = "";
int tooMuch = 200;
int curr = 0;
while (cr.ReadNextRec ord() && curr<tooMuch)
{
for (int i = 0; i < numFields; i++)
{
updateText = headers[i] + ": " + cr[i] +
Environment.New Line;
UpdateText(upda teText);
}

UpdateText(Envi ronment.NewLine );

curr++;
}
}

delegate void UpdateTextDeleg ate(string param);

private void UpdateText(stri ng param)
{
if (this.txt_CsvTe xt.InvokeRequir ed)
{
//We're in the wrong thread, so this gets us to the
right one
this.txt_CsvTex t.Invoke(new
UpdateTextDeleg ate(UpdateText) , new object[] { param });
}
else
{
//Now we're in the right thread.
txt_CsvText.App endText(param);
}
}
}
}

Feb 26 '07 #1
4 3378
sherifffruitfly <sh************ *@gmail.comwrot e:
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.co m>
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.com wrote:
sherifffruitfly <sherifffruit.. .@gmail.comwrot e:
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.com wrote:
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
2518
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 with? Programmer? Or Developer? How much of the exam will rely on memorization? How much will be from programming skill? How do they measure my programming ability from one exam?
2
2985
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 5/5/03 6:03 5/5/03 6:05 M1 U3 5/5/03 6:03 5/5/03 6:04 M2 : etc. There are about 40K units and 30 serving machines. The data is in
6
2642
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 classes as opposed to structures. the program basically modelled a simple polygon using point, line and polyogon structures. and had various functions to find the properties of the shape such as area, perimeter, etc. the question reads along the...
3
2811
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 sale made by one of 90 salespeople (2 years of data), and the date the sale was made. I need a report that shows a list of the salespeople down the left hand side and then for each month the total number of sales and the total value of sales. I...
1
2232
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 EmailMessage_Base ( inherits System.Net.Mail.MailMessage and provides additional methods and properties) EmailMessage_Abstract ( inherits EmailMessage_Base and adds some business logic including what default return addresses are and
6
1535
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 500 AND 1000 THEN '500-1000' ELSE 'Other' END, count(income) AS number FROM #persons GROUP BY CASE
23
6101
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) for any new games I might write. I know threads won't help (in CPython at least) so I'm investigating other types of concurrency which I might be able to use. I really like the PyLinda approach, however I need to be able to pass around all the...
5
1804
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). When the second combo box is completed I want a third control, a text box (named ID)to be filled in with a id for that intersection. I have a table that contains the fields: Fr--street name To--cross street name Node--The unique id for the...
2
1833
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 built the whole site (ASP.NET using VB.NET) but am having problems on the most basic thing! I have a page which querys an MS SQL database and takes url variables to query it, so it takes the value of keyword= and perpage= (eg from...
8
1764
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 vectors and returns the result. u and v are two 3x1 matrix. when I import the function, error message show like this Traceback (most recent call last): File "<stdin>", line 1, in ?
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10244
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.