473,651 Members | 3,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

interrupting a lengthy sequence

I have a lengthy sequence of operations that are executed and reported on
in a status window in a Windows Form application. Some work is done by
background threads but other work is not. I am wondering how to recognize
if the user presses an Escape (or even just a Shift key if that is
simpler) so I may then abort the remaining foreground operations. To
recognize a shift key I tried:
if ((Control.Modif ierKeys & Keys.Shift) == Keys.Shift) { ... }
My code included a Thread.Sleep(10 0) every so often to allow the user
keystrokes to be recognized, or so I thought. But neither keystrokes nor
button clicks are being recognized. What am I missing?

Nov 22 '06 #1
13 1758
"michael sorens" <m_********@com munity.nospamwr ote in message
news:op******** *******@spo-cont-2-dt.itron.com...
I have a lengthy sequence of operations that are executed and reported on
in a status window in a Windows Form application. Some work is done by
background threads but other work is not. I am wondering how to recognize
if the user presses an Escape (or even just a Shift key if that is
simpler) so I may then abort the remaining foreground operations. To
recognize a shift key I tried:
if ((Control.Modif ierKeys & Keys.Shift) == Keys.Shift) { ... }
My code included a Thread.Sleep(10 0) every so often to allow the user
keystrokes to be recognized, or so I thought. But neither keystrokes nor
button clicks are being recognized. What am I missing?

You need to give your app time to process the message loop. You can do this
by calling Application.DoE vents or by running the entire process in a
thread.

Michael
Nov 22 '06 #2
That seems to do it; thanks for the pointer!
Nov 22 '06 #3
Cool, glad to see your problem is resolved.

Yes, Application.DoE vents is the correct way for this issue. I want to
share some more background information to you:

Winform GUI normally runs in a single thread, so when the GUI thread is
processing a length operation, it can not go back to the GUI message loop
to receive various messages to process. Since the keyboard events are
dispatched to the GUI controls through message loop, this length processing
will cause the application to be unresponsive to keyboard. Another side
effect is that the application can not process WM_PAINT message in the
message loop, which means the application can not re-paint its UI anymore.
These 2 side effects will result in application-hang to the end user.

So, to resolve this problem, the normal solution is calling
Application.DoE vents() method in the length processing code every a few
time. Application.DoE vents() method will try to remove and process all
Windows messages currently in the message queue. So all the pending
WM_PAINT messages and keyboard messages will be processed during the length
operation, which makes the application keyboard aware of and UI
responsible.

Another common solution to this scenario is placing the length operation
work in the background thread so that the GUI thread can continue its
message looping without blocking. Chris Sells demonstrates this technology
in the article below:
"Safe, Simple Multithreading in Windows Forms, Part 1"
http://msdn2.microsoft.com/en-us/library/ms951089.aspx

Note: while doing multithreading in Winform, the key point to remember is
that .Net Windows Forms uses the single-threaded apartment (STA) model
because Windows Forms is based on native Win32 windows that are inherently
apartment-threaded. The STA model implies that a window can be created on
any thread, but it cannot switch threads once created, and all function
calls to it must occur on its creation thread. So while the background
thread wanted to manipulate the GUI controls methods/properties, it must
marshal the calling with Control.Invoke/BeginInvoke methods, or this may
cause some strange and hard to detect thread-safe issue. Please refer to
the the article below for more information:
"Multithrea ded Windows Forms Control Sample"
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp

If you still need any help or anything unclear, please feel free to tell
me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 22 '06 #4
Just for completeness, in many circles DoEvents() is considered a hack, with
the correct solution being to do the real work on a worker thread with
suitable UI sync code. This can be a little more work, but can help avoid
some interesting "trying to do 5 things at once" on the UI thread (not "at
once" in the CPU sense - but rather 5 separate methods started via a UI
event and interrupted by DoEvents()).

For a "utility" app, or something small, you can probably get away with
it... but once you get the knack, the "proper" way isn't much more effort -
especially if you use (for instance) the BackgroundWorke r component as a
easy route in...

Marc
Nov 22 '06 #5
Thanks for all the follow-up information; there's quite a bit to digest in
those articles. Just one final point: what is the proper way to process a
TableAdapter.Fi ll() call in the background? That operation may take quite
a while in my application. If I hand it off to a worker thread
with the Invoke mechanism described, doesn't that still lock up the UI?
Nov 22 '06 #6
Jeffrey Tan[MSFT] <je***@online.m icrosoft.comwro te:
Cool, glad to see your problem is resolved.

Yes, Application.DoE vents is the correct way for this issue.
Eek, no. Tying up the UI with a long-running task and periodically
calling Application.DoE vents is a nasty way of solving the problem. In
particular, if there are *any* blocking calls (eg file IO which might
be across a network, or a database transaction) then while that
blocking operation is taking place, the UI will be hung.

This kind of problem plagues some applications, eg Visual SourceSafe,
and is a source of great annoyance to users.

<snip>
Another common solution to this scenario is placing the length operation
work in the background thread so that the GUI thread can continue its
message looping without blocking.
That's a *far* better way of doing things.

<snip>

--
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
Nov 22 '06 #7
michael sorens <m_********@com munity.nospamwr ote:
Thanks for all the follow-up information; there's quite a bit to digest in
those articles. Just one final point: what is the proper way to process a
TableAdapter.Fi ll() call in the background? That operation may take quite
a while in my application. If I hand it off to a worker thread
with the Invoke mechanism described, doesn't that still lock up the UI?
I assume you're using data binding? If so, there's a bit of a problem -
filling the table calls UI handlers, so you have to do it in the UI
thread if you have data bindings. This is a pain. You could undo the
binding, fill the table in a different thread, and then redo the
bindings, or take the nasty hit of doing the fill in the UI thread.

Having said this, my experience with data binding is solely on 1.1 - I
believe it's been much improved in 2.0, so there may be some better
solutions there.

--
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
Nov 22 '06 #8
Hi Jon ,

Yes, I see your point. However, asking the customers to change their design
is not always practical, so I provide 2 solutions to the customer and let
them judge the choice. Sure, the second multithreading solution is better
than first one.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 23 '06 #9
Jeffrey Tan[MSFT] <je***@online.m icrosoft.comwro te:
Yes, I see your point. However, asking the customers to change their design
is not always practical, so I provide 2 solutions to the customer and let
them judge the choice. Sure, the second multithreading solution is better
than first one.
That's a reasonable thing to say - what I was most objecting to was the
idea that "Application.Do Events is the correct way for this issue" and
"So, to resolve this problem, the normal solution is calling
Application.DoE vents() method".

Application.DoE vents should generally be regarded as a much riskier way
of doing things, IMO.

--
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
Nov 23 '06 #10

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

Similar topics

2
1380
by: Leo Breebaart | last post by:
Hi all, In a GUI program I'm currently developing (wxWindows, if that matters), I am trying to get some time-consuming tasks (triggered by e.g. the user choosing a menu item) out of the interface thread and into 'worker' threads of their own, in order to stop them from locking up the GUI. If I subclass threading.Thread, and I override run() as follows:
10
2444
by: Rithish | last post by:
Hi folks. A premature and redundant question maybe... But I have a TABLE problem. This is almost something like what I want to acheive. ----------------------------------------- | | | | | | Col1 | Col2 | Col3 | Col4 |
4
1964
by: Hardy Wang | last post by:
Hi, I have a win form application, when a button is clicked, a lengthy operation will be triggered. During the time program is still running, this application seems not to be able to response to other actions. When I switch to other window, and switch back to my application, all I can see is a white window, it won't refresh itself until operation finishes. If I have some other buttons in the form, it is not possible to show up during...
3
1100
by: Ed Chiu | last post by:
Hi, I have a relative lengthy .aspx page. There are buttons, datalists datagrids everywhere. When a user scrolls down to the middle of bottom of the page and click on a button, a postback happens and the page is rerendered, but the position is at the very top. The user has to scroll down again. IS there a way to remember the position and reposition it after postback? TIA
5
1204
by: atefshehata | last post by:
hi all, i'm using iis6 installed on win2003 , dotnet framework 1.1 . My problem is.. a page on my application have a lengthy process which takes about 4 minutes (performing database transactions ..) to completes. user click on the transaction button to start this lengthy process.
6
2850
by: Cerebrus99 | last post by:
Hi all, I'm making a Windows application that does some lengthy retrieval operations from a database and possibly from a internet resource. I want to show that the operation is going on, by using an Animated .gif in a picture box. Also note that this PictureBox also acts as the button to invoke this lengthy operation. In other words, the user will click the PicBox to start the operation. When clicked, I update the Image property of...
8
1219
by: andreas | last post by:
When I do a long calculation is there a possibility to interrupt this calculation properly? For i as integer = 1 to 100000000 do something next and retrieve the i at the moment of the interrupting Thanks for any response
5
2095
by: Jonah Bishop | last post by:
I'm developing a photo album web application for use on a web site, and I'm running into a problem with lengthy operations. My application allows the user to import a number of images at once into a photo album. For each image that gets imported, I create two thumbnail images (small and medium) and insert some data into a database. The thumbnail generation process takes some time and, for relatively large amount of photos, the application...
1
3026
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the array data from 0 to3, I get this {13, JUNK VALUE, 17,38, 18} and JUNK VALUE is like 1.8e831 or something like that. This happens when I use the attach() function and use the current() function to display the values at data I really want to...
0
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
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
8701
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...
1
8466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
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...
0
5615
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
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.