Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » Constantly update a control in the background
       
Author Reply
patrick
posted 216 posts
since May 14, 2009 
from

Constantly update a control in the background

  Posted on: 29 Jan 2012       
Sorry for reposting, I wanted to reword this and put everythin in one spot and didnt know how to change the old one and show my edits.

Here is what i want to do.  Lets say I have a form with label and 2 listboxes.  I want to run

setTime(){
while(true){ label.text = datetime.now.tostring();}
}

setBox1(){
while(true){
items = getitems();
listbox1.items.clear();
for(int i=0;i<items.count;i++){
listbox1.items.add(items1[i]);}
}

setBox2(items2){
while(true){
items = getitems2();
listbox2.items.clear();
for(int  i=0;i<items2.count;i++){
listbox2.items.add(items2[i]);}
}

I want to run these constantly without the rest of the program waiting, so i can always display relevant information to my user.

I saw the post http://www.c-sharpcorner.com/uploadfile/835123/cross-thread-operations-in-C-Sharp/  that allows me to update a control using a new thread, but it still makes the program wait for the thread to complete.  Why is that?  Can't i constantly update a control without interrupting the rest of the program?

I tried background worker a few different ways, but always got either the error that it was busy, or it else it would say i could not access the listbox from a thread other than the one it was created on.  There has to be a way to do this, right?

I thought maybe it had something to do with invoke and delegates, but i dont really understand what delegates are or how to use them properly.  I have only coded in a single threaded environment and ive hit a wall with it.
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Constantly update a control in the background
  Posted on: 30 Jan 2012        0  
I told you in your other thread to read articles about BackgroundWorker and use the BackgroundWorker class. You should have posted replies in that thread what difficulties you were having. You should have at least included in this thread what difficulties you are having with the BackgroundWorker class.

I said in your other thread "you can use the ProgressChanged event to update the ListBox" and you are not doing that.
Thinking is a feeling; pleasant for some and unpleasant for others.
patrick
posted  216 posts
since  May 14, 2009 
from 

 Re: Constantly update a control in the background
  Posted on: 02 Feb 2012        0  
But I did say the problem I was having with the background worker, I tried background worker a few different ways, but always got either the error that it was busy, or it else it would say i could not access the listbox from a thread other than the one it was created on. 
patrick
posted  216 posts
since  May 14, 2009 
from 

 Re: Constantly update a control in the background
  Posted on: 02 Feb 2012        0  
where would the progress changed event go?  I want a continuous while loop, so there isnt really any progress.
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Constantly update a control in the background
  Posted on: 02 Feb 2012        0  
You would add an event handler for the progress changed event and then in that event handler you would update the listbox. The BackgroundWorker would take care of the delegates and the invokes and such.
Thinking is a feeling; pleasant for some and unpleasant for others.
patrick
posted  216 posts
since  May 14, 2009 
from 

 Re: Constantly update a control in the background
  Posted on: 02 Feb 2012        0  
Here is what i ended up doing, it looks like it will work, but i ran into a new problem

private void fillOrders()
{
OrderCustomerStrings.Clear();  // this is an object of list<string>, declared at the top of the form.

List<int> OrderNumbers = OrdersDataHandler.getTodaysOrders();

foreach(int i in OrderNumbers)
{
unPaidCarryoutsStrings.Add(i.ToString() + "-" + CustomerDataHandler.GetCustomerByID(OrdersDataHandler.GetCustomerIDOfOrder(i)).lName);
}
}

private void bwShowOrders_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
fillOrders();
}

private void bwShowOrders_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
pendingOrdersListBox.DataSource = OrderCustomerStrings;
bwShowOrders.RunWorkerAsync();
}

the idea was that the background worker will constantly update the List<String> object OrderCustomerStrings, which will then be drawn in the list box before being updates again by the background worker.

The problem i ran into with this is it runs the cpu at 100% usage, all the time.  i dont understand why.
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Constantly update a control in the background
  Posted on: 03 Feb 2012        0  
I do not know what getTodaysOrders does.

As I understand your code, your DoWork calls fillOrders, which calls getTodaysOrders and then adds the results to unPaidCarryoutsStrings but I do not know what unPaidCarryoutsStrings is.

I see that your bwShowOrders_RunWorkerCompleted event calls bwShowOrders.RunWorkerAsync. That won't work. The bwShowOrders.RunWorkerAsync will cause bwShowOrders_DoWork to execute and then after bwShowOrders_DoWork finishes, the bwShowOrders_RunWorkerCompleted event will called.

Are you doing this because OrdersDataHandler.getTodaysOrders takes a long time? If so, then as best as I understand things, you cannot show any data until OrdersDataHandler.getTodaysOrders finishes. You should use a background worker for a long-running process so that the UI remains responsive but perhaps for your application it is not possible to show any data until all of it has been obtained.

Do I misunderstand where the data is coming from? Is it coming from someplace where there is a delay in getting pieces of it and is the delay caused by something other than the local processor needing time to process the data? If so, then your DoWork would need to do something to wait and I do not see that happening.

I can understand that all of this is confusing. I wish I could make it less confusing. I am confused about what your requirements are.


Thinking is a feeling; pleasant for some and unpleasant for others.
patrick
posted  216 posts
since  May 14, 2009 
from 

 Re: Constantly update a control in the background
  Posted on: 03 Feb 2012        0  
I should clarify. 
The idea is to get a list of orders that have not been paid for through getTodaysOrders(), which is a function that runs a mysql command and returns a list of order numbers.
Then I sort through the orders and get the customers associated for them so I can display [order numer]-[customer last name] in a list box.
The database is updated by other systems, so I want to always display a fresh set of orders to the user, while keeping the ui responsive. 

Since I last posted, I added a Thread.Sleep(1000) to the beginning of the dowork command.

This seems to work, but I dont think it was the proper way to do it.

Oh the line that read

unPaidCarryoutsStrings.Add(i.ToString() + "-" + CustomerDataHandler.GetCustomerByID(OrdersDataHandler.GetCustomerIDOfOrder(i)).lName);

should have read

OrderCustomerStrings.Add(i.ToString() + "-" + CustomerDataHandler.GetCustomerByID(OrdersDataHandler.GetCustomerIDOfOrder(i)).lName);

where ordercustomerstrings is a variable accessible from both threads, so it gets updated by the dowork, then updates the ui in the workcomplete
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Constantly update a control in the background
  Posted on: 04 Feb 2012   Accepted Answer     0  
The fact that the reason you need to update the data is because the database is being updated by an external program I think was not mentioned previously. That is an important piece of information.

I assume you are using SQL Server. The best solution is to get SQL Server to notify you of changes; that there are changes. I do not know how to do that. Perhaps you can use a trigger, but that is about all I know.

I don't understand how it could work; the code you posted appears to me to not be something that would work. Since it works perhaps you should keep it the way it is.

An alternative is to do away with the background worker. Add a timer to the form and set it to execute every second or whatever. Do the update in the timer tick event. You do not need to be concerned about delegates or cross-thread problems or much of anything. As long as the update can complete fast enough to keep the users happy then that will work. It is not the best but it is the best balance of simplicity and performance.

An alternative would be a combination of the above. You could use a timer and in the timer tick event, use a background worker to get the current data. Then after you have all the updated data, refresh the listbox. I am not sure of the details of how to reuse a background worker but it can be done. This general alternative is possible one way or another.
Thinking is a feeling; pleasant for some and unpleasant for others.
       
Team Foundation Server Hosting
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved