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

Problem with the ListBox

Hello All,

I am running in to a situation where the listbox is not refreshing for me.
I am using a timer to cycle every second and call the timer_elapsed() event.
in the time_elapsed event Method I have code that checks to see if a DB
table has been updated.. if so the new new data is uploaded to the listbox
by calling

lstbox1.datasource = myDBDataSet;

and this works great.. but only on the first time the event gets the data
from the database and loads the listbox.

the second time it goes and gets the new data from the database..
I do a
lstbox1.datasource = null; // I have to do this before I do a
lstbox1.items.clear().
lstbox1.items.clear();

this does not Clear out the list...
I then do

lstbox1.datasource = myDBDataSet;

and it does nothing....

thanks in advanced..


Nov 16 '05 #1
3 1945
What kind of timer are you using, and is the timer_elapsed() event running
on the main thread? Depending upon the timer, your elapsed event might be
running on a background thread. If you do anything that affects the UI
(including setting up databinding), that code has to run on the main thread.
I'm not sure whether this would cause the problem that you are having, but
it is worth checking out.
"jayderk" <ja*****@hotmail.com> wrote in message
news:eC**************@TK2MSFTNGP12.phx.gbl...
Hello All,

I am running in to a situation where the listbox is not refreshing for me.
I am using a timer to cycle every second and call the timer_elapsed() event. in the time_elapsed event Method I have code that checks to see if a DB
table has been updated.. if so the new new data is uploaded to the listbox
by calling

lstbox1.datasource = myDBDataSet;

and this works great.. but only on the first time the event gets the data
from the database and loads the listbox.

the second time it goes and gets the new data from the database..
I do a
lstbox1.datasource = null; // I have to do this before I do a
lstbox1.items.clear().
lstbox1.items.clear();

this does not Clear out the list...
I then do

lstbox1.datasource = myDBDataSet;

and it does nothing....

thanks in advanced..

Nov 16 '05 #2
it is of type
private System.Timers.Timer tmrPolling;

I will try and use the other timer and see if that works...

any other tips will help?

Later,

jay

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:#u**************@TK2MSFTNGP09.phx.gbl...
What kind of timer are you using, and is the timer_elapsed() event running
on the main thread? Depending upon the timer, your elapsed event might be running on a background thread. If you do anything that affects the UI
(including setting up databinding), that code has to run on the main thread. I'm not sure whether this would cause the problem that you are having, but
it is worth checking out.
"jayderk" <ja*****@hotmail.com> wrote in message
news:eC**************@TK2MSFTNGP12.phx.gbl...
Hello All,

I am running in to a situation where the listbox is not refreshing for me. I am using a timer to cycle every second and call the timer_elapsed()

event.
in the time_elapsed event Method I have code that checks to see if a DB
table has been updated.. if so the new new data is uploaded to the listbox by calling

lstbox1.datasource = myDBDataSet;

and this works great.. but only on the first time the event gets the data from the database and loads the listbox.

the second time it goes and gets the new data from the database..
I do a
lstbox1.datasource = null; // I have to do this before I do a
lstbox1.items.clear().
lstbox1.items.clear();

this does not Clear out the list...
I then do

lstbox1.datasource = myDBDataSet;

and it does nothing....

thanks in advanced..


Nov 16 '05 #3
I think that's your problem. Your Timer_Elapsed event is running on a
different thread. The Windows UI is not thread safe (not a .Net limitation,
but a Windows thing). You need to make sure that any code that affects the
UI runs on the main thread.

Your databinding code affects the UI indirectly -- when you unbind and
rebind, it implicitely causes the listbox to repaint itself. If this
repainting does not occur on the main thread, the results can be very
unpredictable (could be unresponsive controls, exceptions, or it could just
crash your application).

There are two ways around it. You could use a System.Windows.Forms.Timer
class, which (I believe) invokes its elapsed event on the main thread.

In my opinion, a better way to do this would be to use some functionality
that is built into .net to ensure that your event handler runs on the
correct thread. You are going to be interested in two methods that are
members of your form (actually, they are members of Control, and anything
that inherits Control -- including a form -- has them:
InvokeRequired(), and BeginInvoke()

InvokeRequired returns true if it is called from a thread other than the
main thread. BeginInvoke marshalls your call to the main thread without
blocking the calling thread. You can use these two items to do something
like this:

// your elapsed event handler
public void Timer_Elapsed(object sender, EventArgs e)
{
// check if invoke is required
if(this.InvokeRequired)
{
// call this method again, on the right thread
this.BeginInvoke(new EventHandler(this.Timer_Elapsed), new object[]
{sender, e});
}
else
{
// do whatever this event handler is supposed to do
}
}

Here's what's going on:
On the first pass, this event will be running on a separate thread. The
call to this.InvokeRequired will return true. We call BeginInvoke.
BeginInvoke expects a delegate to the method that should be called and an
array of object containing the parameters to be passed to the method. So, we
pass it a delegate to this method (kind of a recursive call), and we pass
the original method parameters. In the help, you will see that there is a
BeginInvoke, and an Invoke method. The difference is that BeginInvoke does
not block the calling thread, Invoke does. Some MS bloggers that I read
have advised that you use BeginInvoke unless you specifically need to block
the calling thread. This helps to avoid certain deadlock issues.

As a result of the call to BeginInvoke, Timer_Elapsed is called a second
time. This time, it is called on the main thread, so this.InvokeRequired
returns false. Execution procedes to the "else" portion of the if
statement, which is where you want to to your work with the List control.

I like this method because it is more robust -- it is guaranteed to work no
matter what kind of timer you use (or even if you use some other means, like
a background thread). On the other hand, if you use a
System.Windows.Forms.Timer, you don't need to use the Invoke code above, and
so you might get better performance.
"jayderk" <ja*****@hotmail.com> wrote in message
news:eN**************@TK2MSFTNGP12.phx.gbl...
it is of type
private System.Timers.Timer tmrPolling;

I will try and use the other timer and see if that works...

any other tips will help?

Later,

jay

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:#u**************@TK2MSFTNGP09.phx.gbl...
What kind of timer are you using, and is the timer_elapsed() event running
on the main thread? Depending upon the timer, your elapsed event might

be
running on a background thread. If you do anything that affects the UI
(including setting up databinding), that code has to run on the main

thread.
I'm not sure whether this would cause the problem that you are having, but it is worth checking out.
"jayderk" <ja*****@hotmail.com> wrote in message
news:eC**************@TK2MSFTNGP12.phx.gbl...
Hello All,

I am running in to a situation where the listbox is not refreshing for

me. I am using a timer to cycle every second and call the timer_elapsed()

event.
in the time_elapsed event Method I have code that checks to see if a DB table has been updated.. if so the new new data is uploaded to the listbox by calling

lstbox1.datasource = myDBDataSet;

and this works great.. but only on the first time the event gets the data from the database and loads the listbox.

the second time it goes and gets the new data from the database..
I do a
lstbox1.datasource = null; // I have to do this before I do a
lstbox1.items.clear().
lstbox1.items.clear();

this does not Clear out the list...
I then do

lstbox1.datasource = myDBDataSet;

and it does nothing....

thanks in advanced..



Nov 16 '05 #4

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

Similar topics

2
by: Matt Sawyer | last post by:
Hi, I'm attempting to do a drag and drop operation from one listbox to another. I have my listboxes setup with SelectionMode = MultiExtended so that I can use the shift key, cntrl key, etc. to...
1
by: Josema | last post by:
Hi to all, I have a class (persons) that derives from collection base: and another class (person) with this properties: -ID -Name When i have complete filled the object Persons with all...
3
by: Lloyd Sheen | last post by:
I have a page that works fine. I am trying to optimize the page by overriding some of the Information that is being saved in the hidden VIEWSTATE. If I make the properties of the dropdown False...
1
by: yamne | last post by:
I have a problem. When I click in edit datagrid button I show two listbox and two button. I use two button to move data between two listbox. My problem is that I can't call the listbox in the...
0
by: Ken Varn | last post by:
I have a strange problem with a databound listbox. It may be because of how I use it, but I am stumped by this behavior. Here is the scenario: I have a multiselect databound listbox that...
4
by: amber | last post by:
Hello I'm not sure if I should give up trying to find an answer here...or just keep posting my problem.. I'm having problems with a listbox.. I have a listbox that is populated when a user...
2
by: tangokilo | last post by:
Hello and thanks for your help, I have the following Listbox created in VisualStudio 2003 designer, desiring to select multiple entries from that list: -------------------------------...
5
by: Alien2_51 | last post by:
I have a problem with a ListBox control that is on a TabControl, it seems to be forgetting which items are selected in the list when I tab off the current tab, here's my winform code... I even...
1
by: Mart?n Llanos via .NET 247 | last post by:
I've this problem, in my aspx i put a ListBox, when i add lines to this ListBox and and click de button "Guardar", a java script function is called. In that java script I have this code: var...
1
by: faheem ramzan via .NET 247 | last post by:
I have a problem using listbox. I used to put items in listbox dynamically and maintained size automatically in listbox sothat there is no scroll bar in the listbox. I have a problem when the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.