Solved

Threading problems, not working

Posted on 2004-08-01
2
210 Views
Last Modified: 2010-04-15
HI,

I need to read a large file, and find certain words in the file, then list them in a listview.

I can do it, but the problem is that it has to read the whole file before it updates the listview. I need to to update the listview as it reads the file, ie. one addition at a time, while it is also continues to read the file. I added the basic functions that I am using, not the full code, but you should be able to see what I am doing for the code below.

t = new Thread(new ThreadStart(Add_Words));
t.Start();

//Words is a property that gets the search words

//This is in a function that reads the file
while((str = reader.ReadLine()) != null)
{
link=str;
if (str.IndexOf(search_word) > 0)
{
this.Words = s;
//I truncate the string to get only the word
Add_Words();
}
}

private void Add_Words()
{
item = new ListViewItem(this.Words);
lst_words.Items.Add(item);
}
0
Comment
Question by:unomateo
  • 2
2 Comments
 
LVL 10

Accepted Solution

by:
smegghead earned 125 total points
ID: 11687593
You have to invoke the call to Add_Words..

All calls to the GUI must be done on the main thread.

So..

Create a delegate for a method with no parameters

      private delegate void CallUpdate();

then, rather than calling Add_Words();  

do the following

      this.Invoke(new CallUpdate(Add_Words));

Smg.
0
 
LVL 10

Expert Comment

by:smegghead
ID: 11687594
Also, you might want to do a 'Refresh' on the control after adding each entry.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# 2015 Downloading text file and preserving the line breaks 5 36
Question! 4 36
Reading the Web.Config using IIS 7.5? 4 35
Moving code from one TFS branch to another 3 25
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question