473,406 Members | 2,816 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,406 software developers and data experts.

Problems with threads updating listbox

I'm writing an email module and Im having lots of strange errors with
threads.
The form has a similar look to outlook express.

I've several buttons that I use to switch between Inbox/Sent/Draft/Deleted
items for displaying in the list.
Thread 1 checks for new incoming mail every minute and adds new messages to
the list when necessary.
Thread 2 looks at the selected item in the list and sets button
enabled/disabled etc
Thread 3 is fired when the selected item in the list changes and changes
it's message status to Read after 1sec
There's an OnSelectedItemChange event that fires when the selected item
changes in the list to display the mail in a preview window.

I get problems when I click one of the buttons to change between
Inbox/Sent/Draft/Deleted then follow that quickly with selecting an item in
the list. It falls over on quantifying lstMail.SelectedItem and it's as if
the list is being built while thread 2 fires off.
SelectedItem "System.IndexOutOfRangeException"
I'm calling the thread methods using Invoke on the form so I don't really
understand why this is happening and I didnt have the problem when I did the
same thing using a Timer object rather than thread. Can someone tell me
where I'm going wrong please.

// Thread 2 loop
private void StatusThreadMethod()
{
string fnName = "StatusThreadMethod";
while(true)
{
try
{
Thread.Sleep(500);
Invoke(new EmptyEventDelegate(StatusThreadInvokeMethod));
}
catch(Exception e)
{
LogException(fnName, e);
Invoke(new OnThreadExceptionDelegate(OnThreadException), new object[]
{e});
}// catch
}// while
}// function

private void StatusThreadInvokeMethod()
{
string fnName = "StatusThreadInvokeMethod";
try
{
// Update form buttons enabled/disabled
UpdateButtons();
}
catch(Exception e)
{
LogException(fnName, e);
throw e;
}
}// function

private void UpdateButtons()
{
string fnName = "UpdateButtons";
try
{
MessageCentreMessage message = null;
// Following line is where it falls over with exception above <<<<<<<<
if (lstMail.SelectedItem != null)message =
(MessageCentreMessage)lstMail.SelectedItem;
btnRestore.Enabled = ((message != null)&&(lstMail.Tag ==
_deletedFolder));
btnReply.Enabled = ((message != null)&&(message.IsMessageIn));
btnForward.Enabled = ((message != null)&&(lstMail.Tag != _draftFolder));
btnDelete.Enabled = (message != null);
btnStandardMessage.Enabled = (lstMail.Tag != null);
btnNew.Enabled = true;
}
catch(Exception e)
{
LogException(fnName, e);
}
}// function
Aug 16 '07 #1
0 1192

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

Similar topics

5
by: Bill | last post by:
I have have two list boxes. One is a listing of all possible variables. We'll call this listbox A. The other is a listing of all the selected variables. We'll call this listbox B. If a person...
3
by: gdbjohnson-AT-yahoo-dot-ca-nospamplz | last post by:
I have a ListBox built of a simple custom object for the ListItems used to be able to hold a Data Value, and a Display Value, with accessors for each. I have overridden the ToString method to...
0
by: Ben Rush | last post by:
Greetings, I don't deal too much with the visual side of the framework, so I may be simply doing something quite stupid - but if I am I can't figure it out for the life of me. On my machine...
1
by: MrNobody | last post by:
Hi, I'm doing something where I add custom objects to a ListBox which have aToString() method overriden so it displays what I want. When adding instances of these custom objects to the ListBox I...
1
by: Spock | last post by:
Hi. I have a form with a listbox and a few label fields all bound to a dataset. When i navigate the listbox the labels change accordingly. so far everything works good. I made a button to...
3
by: Keith Mills | last post by:
Hello, please find attached a basic outline of what I am attempting to accomplish... basically I want to create a number of THREADS (which I can do fine), but I then need a method for them to be...
1
by: gn | last post by:
I have a form that is used for adding, updating and deleting records in a table, everything works fine except that one of the fields uses a list box, this works fine for adding a new record but...
6
by: Zytan | last post by:
In win32 there was a way to 'turn off' a listbox from updating until you were done adding elements. Is there a way to do this in .NET? Zytan
3
by: Reg Verrin | last post by:
I have a program that displays constantly changing prices which it sources from the web once per second. The prices are displayed on a Listbox (not the best choice but there are good reasons for...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
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,...
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...
0
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,...
0
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...

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.