473,473 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A question on threading

Hello,

I am writing a program related to image compression in C# (GUI
Application). In that I have a "heavy, long running" method which
among other things, calls this.Invalidate(); to make the screen
refresh to display the changes in image during processing.

The problem is that, once the program enters this method, the GUI
Window becomes non-responsive (but GUI gets updated regularly due to
this.Invalidate()).

So, I want the GUI to stay responsive while executing this method.

I tried running the method in another thread but then I cannot call
this.Invalidate() because "this" is in parent thread, and it throws
error.

How can I keep my window responsive while running this method? Any
ideas?
Mar 2 '08 #1
7 1351
On Mar 2, 8:38 pm, Rahul <rahu...@gmail.comwrote:
Hello,

I am writing a program related to image compression in C# (GUI
Application). In that I have a "heavy, long running" method which
among other things, calls this.Invalidate(); to make the screen
refresh to display the changes in image during processing.

The problem is that, once the program enters this method, the GUI
Window becomes non-responsive (but GUI gets updated regularly due to
this.Invalidate()).

So, I want the GUI to stay responsive while executing this method.

I tried running the method in another thread but then I cannot call
this.Invalidate() because "this" is in parent thread, and it throws
error.

How can I keep my window responsive while running this method? Any
ideas?
Try using a backgroundworker.
Mar 2 '08 #2

"Rahul" <ra*****@gmail.comwrote in message
news:00**********************************@e6g2000p rf.googlegroups.com...
Hello,

I am writing a program related to image compression in C# (GUI
Application). In that I have a "heavy, long running" method which
among other things, calls this.Invalidate(); to make the screen
refresh to display the changes in image during processing.

The problem is that, once the program enters this method, the GUI
Window becomes non-responsive (but GUI gets updated regularly due to
this.Invalidate()).

So, I want the GUI to stay responsive while executing this method.

I tried running the method in another thread but then I cannot call
this.Invalidate() because "this" is in parent thread, and it throws
error.

How can I keep my window responsive while running this method? Any
ideas?
Application.DoEvents() will force your thread to process messages from the
UI. Its probably not the right way to do it, but it will very probably work.
Just stick it in somewhere you expect to be called at least 50 times per
second and the UI should work just fine.


Mar 2 '08 #3
On Mar 2, 4:47 am, Mugunth <mugunth.ku...@gmail.comwrote:
On Mar 2, 8:38 pm, Rahul <rahu...@gmail.comwrote:
Hello,
I am writing a program related to image compression in C# (GUI
Application). In that I have a "heavy, long running" method which
among other things, calls this.Invalidate(); to make the screen
refresh to display the changes in image during processing.
The problem is that, once the program enters this method, the GUI
Window becomes non-responsive (but GUI gets updated regularly due to
this.Invalidate()).
So, I want the GUI to stay responsive while executing this method.
I tried running the method in another thread but then I cannot call
this.Invalidate() because "this" is in parent thread, and it throws
error.
How can I keep my window responsive while running this method? Any
ideas?

Try using a backgroundworker.
I tried to call the function through BackgroundWorker.

The "this.Invalidate()" runs properly. But this.Update() (which is the
next statement) does not work.
It throws the same exception "Cross-thread operation not valid:
Control '<FormName>' accessed from a thread other than the thread it
was created on."

this.Update() is necessary to ensure that GUI gets updated quickly, so
as to make the changes visible in pseudo-real-time.

Thanks for the reply.

Any other ideas?
Mar 2 '08 #4
On Mar 2, 5:10 am, "Peter Webb" <webbfam...@DIESPAMDIEoptusnet.com.au>
wrote:
"Rahul" <rahu...@gmail.comwrote in message

news:00**********************************@e6g2000p rf.googlegroups.com...
Hello,
I am writing a program related to image compression in C# (GUI
Application). In that I have a "heavy, long running" method which
among other things, calls this.Invalidate(); to make the screen
refresh to display the changes in image during processing.
The problem is that, once the program enters this method, the GUI
Window becomes non-responsive (but GUI gets updated regularly due to
this.Invalidate()).
So, I want the GUI to stay responsive while executing this method.
I tried running the method in another thread but then I cannot call
this.Invalidate() because "this" is in parent thread, and it throws
error.
How can I keep my window responsive while running this method? Any
ideas?

Application.DoEvents() will force your thread to process messages from the
UI. Its probably not the right way to do it, but it will very probably work.
Just stick it in somewhere you expect to be called at least 50 times per
second and the UI should work just fine.
Peter,

A brilliant hack!! Although this might not be the best possible way...
it gets the job done.

Unless someone suggests a more "elegant" solution, this would do just
fine.

Thanks a lot! :)
Mar 2 '08 #5
Hi,

The UI only should be updated by the same thread that created the Control's
windows handle.
To do that, you can use Control.Invoke which receives a delegate pointing to
the code you want to execute.
For the delegate you can use a MethodInvoker such as the following.

this.Invoke(new MethodInvoker(this.Update));

HTH
"Rahul" <ra*****@gmail.comwrote in message
news:54**********************************@s19g2000 prg.googlegroups.com...
On Mar 2, 5:10 am, "Peter Webb" <webbfam...@DIESPAMDIEoptusnet.com.au>
wrote:
>"Rahul" <rahu...@gmail.comwrote in message

news:00**********************************@e6g2000 prf.googlegroups.com...
Hello,
I am writing a program related to image compression in C# (GUI
Application). In that I have a "heavy, long running" method which
among other things, calls this.Invalidate(); to make the screen
refresh to display the changes in image during processing.
The problem is that, once the program enters this method, the GUI
Window becomes non-responsive (but GUI gets updated regularly due to
this.Invalidate()).
So, I want the GUI to stay responsive while executing this method.
I tried running the method in another thread but then I cannot call
this.Invalidate() because "this" is in parent thread, and it throws
error.
How can I keep my window responsive while running this method? Any
ideas?

Application.DoEvents() will force your thread to process messages from
the
UI. Its probably not the right way to do it, but it will very probably
work.
Just stick it in somewhere you expect to be called at least 50 times per
second and the UI should work just fine.

Peter,

A brilliant hack!! Although this might not be the best possible way...
it gets the job done.

Unless someone suggests a more "elegant" solution, this would do just
fine.

Thanks a lot! :)
Mar 2 '08 #6
On Sun, 02 Mar 2008 05:28:01 -0800, Rahul <ra*****@gmail.comwrote:
[...]
>Application.DoEvents() will force your thread to process messages from
the
UI. [...]

Peter,

A brilliant hack!! Although this might not be the best possible way...
it gets the job done.

Unless someone suggests a more "elegant" solution, this would do just
fine.
Do NOT accept that advice. It is the worst way to deal with this issue.

As mentioned by others, you can use more appropriate techniques such as
using a BackgroundWorker in conjunction with the ProgressChanged event, or
simply calling Control.Invoke() or Control.BeginInvoke() from your worker
thread to update the UI.

Note also that there's no need to call Control.Update() directly if you're
doing things correctly. For a control where you're doing your own
painting, simply calling Invalidate() will have the intended effect, and
of course for other controls, just changing their state will result in the
control itself doing the necessary invalidation.

I've yet to see an application that call Application.DoEvents() that
wasn't just plain broken. I doubt they exist and I'm certain, given your
description, that it's absolutely the wrong way to approach the issue in
your case.

Pete
Mar 2 '08 #7
On Mar 2, 6:12 am, "Alfred Myers" <alfred_my...@hotmail.comwrote:
Hi,

The UI only should be updated by the same thread that created the Control's
windows handle.
To do that, you can use Control.Invoke which receives a delegate pointing to
the code you want to execute.
For the delegate you can use a MethodInvoker such as the following.

this.Invoke(new MethodInvoker(this.Update));

HTH

"Rahul" <rahu...@gmail.comwrote in message

news:54**********************************@s19g2000 prg.googlegroups.com...
On Mar 2, 5:10 am, "Peter Webb" <webbfam...@DIESPAMDIEoptusnet.com.au>
wrote:
"Rahul" <rahu...@gmail.comwrote in message
>news:00**********************************@e6g2000 prf.googlegroups.com...
Hello,
I am writing a program related to image compression in C# (GUI
Application). In that I have a "heavy, long running" method which
among other things, calls this.Invalidate(); to make the screen
refresh to display the changes in image during processing.
The problem is that, once the program enters this method, the GUI
Window becomes non-responsive (but GUI gets updated regularly due to
this.Invalidate()).
So, I want the GUI to stay responsive while executing this method.
I tried running the method in another thread but then I cannot call
this.Invalidate() because "this" is in parent thread, and it throws
error.
How can I keep my window responsive while running this method? Any
ideas?
Application.DoEvents() will force your thread to process messages from
the
UI. Its probably not the right way to do it, but it will very probably
work.
Just stick it in somewhere you expect to be called at least 50 times per
second and the UI should work just fine.
Peter,
A brilliant hack!! Although this might not be the best possible way...
it gets the job done.
Unless someone suggests a more "elegant" solution, this would do just
fine.
Thanks a lot! :)
Hello Alfred,

I will definitely try this approach.

Thanks a lot.
Mar 3 '08 #8

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

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
2
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
11
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
0
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
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,...
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
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...
1
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...
0
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,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.