2
Answers

Add item to listview in backgroundworker without refreshing the listview

learner learner

learner learner

13y
4.8k
1
You cannot vote on your own post
0

I have loaded many images in a listview using ImageList in c#. When many many images are loaded then it takes a long time. So I call the method in backgroundworker. In the backgroundworker I had to add images to ImageList and add ImageList to ListView. So I have used safeinvoke() method listView1.SafeInvoke(d=>d.Items.Add(item)).
. Everything works fine. Images are displayed one by one in the listview. But the problem is the listview is continuously refreshing when a new item is added. This refreshing seems disturbing to the user. When I have added the item without using background worker (listView1.Items.Add(item)) then it worked fine. It was added one after another smoothly without refreshing. I want to add item without refreshing the listview.

Where is the problem? Is the problem of backgroundworker or the SafeInvoke () method? How to solve it?

sample code:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

string[] fileNames = Directory.GetFiles(@"E:\Image");

foreach (string flName in fileNames)
{

Bitmap btmap = (Bitmap)Image.FromFile(flName);
scanNo++;

//imageList.Images.Add(newBtmap); when backgroundworker was not used

this.SafeInvoke(d=>d.imageList.Images.Add(newBtmap));

ListViewItem item;
item = new ListViewItem();
item.ImageIndex = scanNo - 1;
item.Text = scanNo.ToString();

// listView1.Items.Add(item); when backgroundworker was not used

listView1.SafeInvoke(d=>d.Items.Add(item));

}

}

http://www.codeproject.com/Articles/52752/Updating-Your-Form-from-Another-Thread-without-Cre?msg=4151406#xx4151406xx

public static TResult SafeInvoke<T, TResult>(this T isi, Func<T, TResult> call) where T : ISynchronizeInvoke
{
if (isi.InvokeRequired) {
IAsyncResult result = isi.BeginInvoke(call, new object[] { isi });
object endResult = isi.EndInvoke(result); return (TResult)endResult;
}
else
return call(isi);
}

public static void SafeInvoke<T>(this T isi, Action<T> call) where T : ISynchronizeInvoke
{
if (isi.InvokeRequired) isi.BeginInvoke(call, new object[] { isi });
else
call(isi);
}
Answers (2)
0
Vulpes
NA 98.3k 1.5m 12y
Not sure where your post has gone on this but, if you need to control the 3rd party application depending on the selection of some specification on the form, then the easiest way to do it would be to pass the application command line arguments if it's been written to accept them.

You can pass the arguments in the Process.Start method. For example:

   string arguments = "1 abc"; // separate arguments by spaces

   System.Diagnostics.Process.Start("someapp.exe", arguments);

If  the application doesn't accept command line arguments, then you might be able to use SendKeys.Send to send it some keystrokes - for example to open a menu and select an option. This isn't as easy as it sounds since there may be synchronization difficulties and you may need to build in delays between sending the keystrokes to overcome these. Check out the MSDN docs here:

http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx 


0
Vulpes
NA 98.3k 1.5m 12y
Well, what's usually done is to put a button on the form, which when pressed, launches the external application:

private button1_Click(object sender, EventArgs e)
{
   System.Diagnostics.Process.Start("someapp.exe");
}

If you wanted the form to display and then the external application to launch automatically, you could do it by handling the form's Shown event:

private void Form1_Shown(object sender, EventArgs e)
{
   System.Diagnostics.Process.Start("someapp.exe");