473,320 Members | 1,951 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,320 software developers and data experts.

How to get controls to update during heavy activity

rob
I've got a couple stretches of code where I use a lot of CPU on
various operations. Since this is happening in a loop at unrestriicted
speed, some of the form controls do not update. (Actually surprising
that some do and some don't).

Also some buttons become unresponsive.

What's the recommended way of giving time back to the UI so it can
service and update the controls and respond to button clicks?

(Hard to think of good Google search keys for this one)
Jul 21 '07 #1
3 1517
Simple - don't do that work on the UI thread, but instead fire up a
worker thread - perhaps using BackgroundWorker or ThreadPool. The UI
thread simply starts the work going, then goes back to processing
messages. The only complexity is that the worker thread *cannot* talk
directly to the Controls (thread affinity; both read & write), so must
instead ask the UI thread for a little help, via Control.Invoke (sync)
and Control.BeginInvoke (async)

i.e.

void StartWorkHandler(object sender, EventArgs args) {
// runs on the UI thread
ThreadPool.QueueUserWorkItem(WorkMethod);
}
void WorkMethod(object state) {
// runs on the worker thread
for (int i = 0; i < 1000; i++) {
// simulate some heavy work in a loop
Thread.Sleep(500);

// update UI on every 10th cycle
if ((i % 10) == 0) {
// *important* if using async, don't access the
vars (i)
// from both threads at once; in this case, we'll
make
// a string on the worker thread that is then
never
// changed, so we can rely on it on the UI thread;
// can also just use sync (Invoke) to make this
easier,
// but done incorrectly this can show deadlocks...
string newFormText = "Cycle " + i.ToString();
BeginInvoke((MethodInvoker)delegate {
// runs (async) on the UI thread
Text = newFormText;
});
}
}
}

Marc

Jul 21 '07 #2
Rob
On Sat, 21 Jul 2007 02:29:12 -0700, Marc Gravell
<ma**********@gmail.comwrote:
>Simple - don't do that work on the UI thread, but instead fire up a
worker thread - perhaps using BackgroundWorker or ThreadPool. The UI
thread simply starts the work going, then goes back to processing
messages. The only complexity is that the worker thread *cannot* talk
directly to the Controls (thread affinity; both read & write), so must
instead ask the UI thread for a little help, via Control.Invoke (sync)
and Control.BeginInvoke (async)
Thanks, Marc. I was afraid you were going to say that. <g>

This particular stretch of code may take a lot of work to run it on
another thread. In other programs I've used Invoke for access to UI
threads, but this one won't go quietly.

In this case I'm triggering the CPU-hog process via a button. It
updates values that are polled by a dispatch loop on a
System.Forms.Timer (which runs in the UI thread). Some UI controls
DO update. Text in TextBoxes does not. Also Button events don't
propagate back.

Given that some of the controls do work, I was hoping that there was a
way to delegate enough CPU to get the others working too.
No other solution except another thread, eh?
Jul 22 '07 #3
On Sun, 22 Jul 2007 01:26:06 -0700, Rob <Ro*@nospam.comwrote:
[...]
In this case I'm triggering the CPU-hog process via a button. It
updates values that are polled by a dispatch loop on a
System.Forms.Timer (which runs in the UI thread). Some UI controls
DO update. Text in TextBoxes does not.
Are you saying that you have multiple values being polled, some of which
are reflected in the form and some of which are not? That sounds like you
have a bug in either your value-polling code or the UI-updating code.
Also Button events don't propagate back.
Propagate back from where to where? Why should button events (Click, I
assume you mean?) have to propagate at all? Should they not be handled in
the same thread that is already correctly polling values and updating the
UI?
Given that some of the controls do work, I was hoping that there was a
way to delegate enough CPU to get the others working too.
No other solution except another thread, eh?
If your code isn't running, it's not because the thread is being starved.
Unless you are doing something evil like changing thread priority, it's
not possible for one thread in your process to completely prevent other
threads from getting CPU time.

You have a bug. Maybe more than one.

Pete
Jul 22 '07 #4

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

Similar topics

3
by: datapro01 | last post by:
Running DB2 8.1.1 on AIX 5.1 We are trying to determine heavy hit tables for tuning purposes. We have been able to extract information concerning the numbers of inserts, deletes and updates for...
5
by: Paul M | last post by:
Hi All, I've been tasked with writing a z/OS C program to read and update a table (DB/2 v8). This Table contains a single row with a single column of data. This program (which will run as a...
4
by: scottcly | last post by:
I have a simple editor with a Gridview and a DetailsView. The Gridview handles DELETE and the DetailsView handles UPDATE and ADD. A panel around each one and some minor event handling is used to...
4
by: =?Utf-8?B?U3JpZGhhcg==?= | last post by:
Hi, Is it possible to Hide/Show controls during a callback? I have a radio button list that does the callback. When it does the callback I need to refresh the grid to reflect the selected value...
2
by: =?Utf-8?B?VG9u?= | last post by:
Hello, I want to understand teh benefits of ajax technology. Does anyone has a good website where AJAX EXTENSIONS is worked out so I really understand it. There a 2 main questions: 1) How about...
0
by: svgeorge | last post by:
I want to update several tables using one stored procedure. How can i do this I mean the syntax.etc. declaration etc. I know the basic syntax as below CREATE PROCEDURE <Procedure_Name, sysname,...
0
by: Scott Roberts | last post by:
I always thought that the viewstate "keys" included the control ID. As long as the control IDs were unique, there shouldn't be any conflicts. Well, it appears that that may not be the case with...
0
by: wfsmith | last post by:
I have a page with a MultiView control and 6 Views. Each view has a User control that contains various form controls (dropdowns, textboxes and CascadingDropDown Ajax.Net controls). When the...
4
by: Andrew | last post by:
I want to create a set of Activity Diagram controls for process control. I need to create a base Diagram control that acts as a container for the Activity controls ( StartPoint, EndPoint,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.