Chris Watts
Chris Watts

Reputation: 6715

Copying ListView to ListView - C# .NET 2.0

I'm trying to access a ListView object from another thread. The way I'm doing it is creating a temporary ListView for the new thread, then copying this temporary ListView back to the original every second as the new list gets populated.

I'm having difficulty copying the ListView objects though. I've looked around and found ways of copying the items, but I also need the columns and structure to be the same too.

If I simply do:

ListView lv_temp = lv_original

then it copies it by reference, and I'll get more thread access errors.

So how do I make a complete clone by value?

Upvotes: 0

Views: 752

Answers (2)

IAbstract
IAbstract

Reputation: 19881

You need to provide an object as a DataSource. When your datasource gets updated, the UI control gets updated. Otherwise, if you run into threading issues, use the the SynchronizationContext.Current and assign to a sync field member, then:

  // since I believe you don't have lambdas in .net 2.0 I'll try to write this out proper
  // although it is untested, but I hope you get the idea
  sync.Send(new SendOrPostCallback(SendCallBack), stateObject);

  void SendCallBack(object state) {
     // perform UI tasks here
  }

SynchronizationContext is new in .net 2.0

...again, this is untested but is where I would start if I had to. Just as an aside, in .net 3.0(?) to present, we write:

  sync.Send((state) => {
     // perform UI tasks here
  }, stateObject);

Update
Found an answer How do you bind .... So there isn't a DataSource property as I had originally presumed.

Upvotes: 1

Erre Efe
Erre Efe

Reputation: 15557

What you want to do is to deep copy the list, so you can use this extension:

/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
/// 
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>

public static class ObjectCopier
{
    /// <summary>
    /// Perform a deep Copy of the object.
    /// </summary>
    /// <typeparam name="T">The type of object being copied.</typeparam>
    /// <param name="source">The object instance to copy.</param>
    /// <returns>The copied object.</returns>
    public static T Clone<T>(T source)
    {
        if (!typeof(T).IsSerializable)
        {
            throw new ArgumentException("The type must be serializable.", "source");
        }

        // Don't serialize a null object, simply return the default for that object
        if (Object.ReferenceEquals(source, null))
        {
            return default(T);
        }

        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream)
        {
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(stream);
        }
    }
}    

Now, may I ask why are you doing this, why don't you pass the list to another thread and thats all (given that you're modifying it back again). If it's a UI control (wich might be fot what I see), you can use a background ItemsSource (from the other thread) and then use it as source on the UI using a Dispatcher. If that's what you want let me know to provide more details.

Upvotes: 1

Related Questions