Hi All,
I am using winform threads and I have craeted a new thread to perform
soem lengthy operations asynchronously.
My problem is that while this operation is being performed i am
updating the window status bar to display the status of the operation.
But as soon as the window loses the focus it freezes (mean the staus
bar is not updated anymore).
following is my code (in UI thread):
AsyncCallback ac = new AsyncCallback(A SyncCallBackMet hod);
mi = new MethodInvoker(R unPackage);
mi.BeginInvoke( ac, null);
while((Helper.H elperInstance). Status != Helper.Success)
{
if(!InvokeRequi red)
{
informationPane l.Text = "Now running: " +
(Helper.HelperI nstance).NowRun ning;
if(progressPane l.ProgressPosit ion == 50)
progressPanel.R eset();
progressPanel.S tep();
Thread.Sleep(50 0);
}
}
progressPanel.R eset();
informationPane l.Text = "Operation Completed Successfully.";
ShowSummary(); //displays the results in a datagrid
FYI : when showSummary() is executed window is able to display the
final results.
Any help will be appreciated.
Thanks.
Vivek 8 13599
This is just snippets of your code, correct? What's your basic strategy
here in psuedo code? It looks to me like you're calling Thread.Sleep()
on the UI thread and that would be terrible. How is your UI thread
supposed to process messages? vd*****@gmail.c om wrote: Hi All,
I am using winform threads and I have craeted a new thread to perform soem lengthy operations asynchronously. My problem is that while this operation is being performed i am updating the window status bar to display the status of the operation. But as soon as the window loses the focus it freezes (mean the staus bar is not updated anymore).
following is my code (in UI thread):
AsyncCallback ac = new AsyncCallback(A SyncCallBackMet hod); mi = new MethodInvoker(R unPackage); mi.BeginInvoke( ac, null);
while((Helper.H elperInstance). Status != Helper.Success) { if(!InvokeRequi red) { informationPane l.Text = "Now running: " + (Helper.HelperI nstance).NowRun ning; if(progressPane l.ProgressPosit ion == 50) progressPanel.R eset(); progressPanel.S tep(); Thread.Sleep(50 0); } }
progressPanel.R eset(); informationPane l.Text = "Operation Completed Successfully.";
ShowSummary(); //displays the results in a datagrid
FYI : when showSummary() is executed window is able to display the final results.
Any help will be appreciated. Thanks.
Vivek
Yes I am calling thread.sleep in UI thread.
My UI thread is supposed to check for a variable
((Helper.Helper Instance).Statu s) after every 500 ms and update the UI.
When the Status == Success it proceeds and calls the ShowSummary() to
display the summary of results.
Is anything wrong in calling thread.sleep() on UI thread?
Thanks.
You NEVER want your UI thread suspended, that's the whole point of
doing the work asynchronously in the separate thread. What you really
want here is for your worker thread to call back to the UI thread and
say "I've completed this much work" and/or "I'm done". Meanwhile your
UI thread is free to process messages.
That's right. And thats what I am doing in my code. While loop is just
to update the UI Status Bar.
Now i've changed the code and am updating the UI on a separate thread
....here is the code........... ...
//main thread
AsyncCallback ac = new AsyncCallback(A SyncCallBackMet hod);
mi = new MethodInvoker(R unPackage);
mi.BeginInvoke( ac, null);
Thread t = new Thread(new ThreadStart(UIU pdate));
t.Start();
Thread.CurrentT hread.Join();
//new method which i added now
public delegate void UpdateStatusDel egate();
public void UIUpdate()
{
while((Helper.H elperInstance). Status != Helper.Success)
{
if(!InvokeRequi red)
{
informationPane l.Text = "Now running: " +
(Helper.HelperI nstance).NowRun ning;
if(progressPane l.ProgressPosit ion == 50)
progressPanel.R eset();
progressPanel.S tep();
return;
}
else
{
BeginInvoke(new UpdateStatusDel egate(UIUpdate) );
Thread.Sleep(50 0);
}
}
}
But now nothing is happening. Not even a single update on UI.
Please advise.
Thanks.
Now what's the Thread.CurrentT hread.Join() for? That doesn't make any
sense, you're saying "have this thread wait for itself to complete" so
it's obviously not going to complete. I really think you need to brush
up on .net Threading, there are plenty of good sources out there and it
would do you a lot of good. http://www.yoda.arachsys.com/csharp/threads/ vd*****@gmail.c om wrote: That's right. And thats what I am doing in my code. While loop is just to update the UI Status Bar. Now i've changed the code and am updating the UI on a separate thread ...here is the code........... ...
//main thread
AsyncCallback ac = new AsyncCallback(A SyncCallBackMet hod); mi = new MethodInvoker(R unPackage); mi.BeginInvoke( ac, null);
Thread t = new Thread(new ThreadStart(UIU pdate)); t.Start(); Thread.CurrentT hread.Join();
//new method which i added now public delegate void UpdateStatusDel egate();
public void UIUpdate() { while((Helper.H elperInstance). Status != Helper.Success) { if(!InvokeRequi red) { informationPane l.Text = "Now running: " +
(Helper.HelperI nstance).NowRun ning; if(progressPane l.ProgressPosit ion == 50) progressPanel.R eset(); progressPanel.S tep(); return; } else { BeginInvoke(new UpdateStatusDel egate(UIUpdate) ); Thread.Sleep(50 0); } } }
But now nothing is happening. Not even a single update on UI.
Please advise. Thanks.
I agree becoz I am a newbie to threads....I put
Thread.CurrentT hread.Join() there because I have created a new thread
't' which is going to keep updating the UI untill some condition
becomes true. And I want my main UI thread to keep waiting for the
thread t to finish its job before proceeding to some other task (thats
why Join(), its in the main threads execution block ). Am I doing
something wrong ?
Thanks for that link..I'll go through it.
Appreciate your help.
OK, lets take this back to basics.
If you UI thread is *waiting* for something, then the thread is not doing
anything. That would be the definition of "not responding".
I haven't read the entire chain, but it sounds like you actually need to
change the second thread to call back to the UI to tell it that it has
finished, so the UI thread can request the next thing. This can be done with
async-callbacks or events.
Doing background processing often takes more than changing one line of code;
for instance - the following (pseudocode) will *NOT* achieve the desired
result. It will hang the UI until everything has finished:
void SomeHandler(obj ect sender, EventArgs args) {
foreach(Somethi ng s in thingsToDo) {
s.StartSomethin gOnTheBackgroun d();
s.WaitForComple tion();
}
}
Instead you would need to do something like (as an example; multiple
approaches exist) putting the "things to do" into a private Queue<T> on the
form, and then have something that kicks off the top item; when it is
notified of completion (callback or event) it checks for the next, and
starts that, etc. In between all of this, the UI is available for processing
messages such as "paint".
Does that help any?
Marc
Awesome !
That really helped. Really appreciate your help Marc.
Thanks a lot.
Vivek This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mareal |
last post by:
I am trying to write a basic load balancer (in our web service) solution. The
purpose of this load balancer is to keep an array updated with server status.
We have several servers that can be accessed in order to retrieve information.
From within the “Application_Start” (Global.asax), I create as many threads
as servers we have available. The idea is that each thread will query each
server and update a flag in the public array. When a...
|
by: Steve Smith |
last post by:
I have written an application that launches approximately 150 threads with
Thread.ThreadStart()
Each thread uses CDO 1.21 to logon to a different Exchange mailbox and
send/receive a number of mail messages, reporting back to the UI thread
through the use of a Queue object. When all messages that are expected have
been received, each thread sends a final update to the UI and the method
should exit, which should terminate the thread.
...
|
by: Steve Smith |
last post by:
I have written a winforms application that launches approximately 150
threads with
Thread.ThreadStart()
Each thread uses CDO 1.21 to logon to a different Exchange mailbox and
send/receive a number of mail messages, reporting back to the UI thread
through the use of a Queue object. When all messages that are expected have
been received, each thread sends a final update to the UI and the method
should exit, which should terminate the...
|
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.
Note, the Windows task manager shows 2 CPUs on the Performance tab with
hyper-threading is turned on.
Both Python 2.3.5 and 2.4.3 (downloaded from python.org) have this
problem.
The operating system is MS Windows XP Professional.
|
by: brian.wilson4 |
last post by:
Our group is currently comparing winforms vs webforms.....app is Corp
LAN based - we have control of desktops.....Below is pros and cons list
we have come up with - if anything strikes you as untrue or you would
like to add - please comment - thanks.....
Rich Client PROS
1. User experience (* indicates feasible on web with AJAX) -
a. Single, unified application experience
b. Windows/Office-like look and feel -
i.Tabs - drag and drop...
| |
by: Peted |
last post by:
Just some threading questions
1. in a MDI app if you have multiple child forms, does each child
form run in its own thread ? Eg do you get error trying to update one
control on form1 from form 2
2. if you have a timer control running on a childform, and you either
a. minimize or
|
by: Jules Winfield |
last post by:
Guys,
I've been designing applications for the financial services industry using
MSFT technologies for many years (Win32 API, MFC, and now .NET WinForms).
All applications are Internet-based, with a "thick client" which makes calls
to my grid of servers via a socket or remoting connection.
Customers are pleased with my work but it seems that over the past twelve
months or so, those same customers have expressed a strong demand to...
|
by: =?Utf-8?B?Y2hlbmRyaWNrcw==?= |
last post by:
I have a C# winforms application that makes periodic web service calls in
background thread to my web service server. These calls work fine almost all
the time but on rare occassions the web service method call will never return
and the entire application will freeze, even though the call is being made on
its own background thread, not the main UI thread.
Does anyone know any reason why a particular web service method call would
cause my...
|
by: =?Utf-8?B?cmFuZHkxMjAw?= |
last post by:
Visual Studio 2005, C# WinForms application:
Here’s the question: How can I increase the standard 1 MB stack size of the
UI thread in a C# WinForms application?
Here’s why I ask:
I’ve inherited some code that at the view (User Interface) layer kicks off a
background worker thread. At the service layer (think CAB service layer),
there’s quite a lot of the following:
|
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,...
|
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...
| |
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,...
|
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...
|
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 projectplanning, coding, testing, and deploymentwithout 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |