C# / Winforms multithreading

  • Thread starter Thread starter Ludwig Wittgenstein
  • Start date Start date
L

Ludwig Wittgenstein

Hello, all.

I'm developing an application that has a listview of records
(pulled from the database) and a preview pane. Basically, when the
user clicks on a record in the listview the preview pane gets
populated with additional information about the record. Each record
also have a variable number of pictures. What I want to do is have
the application fetch the pictures from the database in a separate
thread. What is the best way to do this? I tried creating a Thread
object every time and invoke a method, however If I scroll really
fast in the listview the application crashes and I cannot debug it. Is
there a specific section that I should lock() when the thread is
started? Also how do i guarantee that the last record selected is that
one that will have the most recent started thread's data?


Thanks,
 
It sounds to me that most likely your issue revolves around attempting to
update a control on the UI (main) thread from another thread. You need to use
a delegate to do this.

Here is a nice little reusable pattern that you can look at:


delegate void SetValueDelegate(Object obj, Object val, Object[] index);

public void SetControlProperty(Control ctrl, String propName, Object val)
{
PropertyInfo propInfo = ctrl.GetType().GetProperty(propName);
Delegate dgtSetValue = new SetValueDelegate(propInfo.SetValue);
ctrl.Invoke(dgtSetValue, new Object[3] { ctrl, val, /*index*/ null });
}

// Usage: SetControlProperty(label1, "Text", myString);
 
It sounds to me that most likely your issue revolves around attempting to
update a control on the UI (main) thread from another thread. You need to use
a delegate to do this.

Here is a nice little reusable pattern that you can look at:

delegate void SetValueDelegate(Object obj, Object val, Object[] index);

public void SetControlProperty(Control ctrl, String propName, Object val)
{
PropertyInfo propInfo = ctrl.GetType().GetProperty(propName);
Delegate dgtSetValue = new SetValueDelegate(propInfo.SetValue);
ctrl.Invoke(dgtSetValue, new Object[3] { ctrl, val, /*index*/ null });

}

// Usage: SetControlProperty(label1, "Text", myString);

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net



Ludwig Wittgenstein said:
Hello, all.
I'm developing an application that has a listview of records
(pulled from the database) and a preview pane. Basically, when the
user clicks on a record in the listview the preview pane gets
populated with additional information about the record. Each record
also have a variable number of pictures. What I want to do is have
the application fetch the pictures from the database in a separate
thread. What is the best way to do this? I tried creating a Thread
object every time andinvokea method, however If I scroll really
fast in the listview the application crashes and I cannot debug it. Is
there a specific section that I should lock() when the thread is
started? Also how do i guarantee that the last record selected is that
one that will have the most recent started thread's data?
Thanks,- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

If you can, it's better to use BeginInvoke() instead of Invoke().
Read more about it (and other threading issues) here:
http://kristofverbiest.blogspot.com/2007/02/avoid-invoke-prefer-begininvoke.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top