Create instance of classes in threads

3

Imagine that I have a class:

Class Animal
{
    //Propriedades
}

Now creating multiple instances of the class I do as follows:

Animal[] animais = new Animal[10];
for(int i = 0; i < 10; i++)
{
    animais[i] = new Animal();
}

So far, all right, in theory at the end of the execution of the for method, I will have an array with my 10 instances of class Animal .

Now I have the following problem:

Each Animal should be a Thread .

1 - How can I implement this? so that each instance of my class is a Thread ?

What I did, and I'm not sure it's correct ...

public void ThreadAnimal()
{
    Thread th;
    Animal[] animais = new Animal[10];

    for (int i = 0; i < 10; i++)
    {
        animais[i] = new Animal();
        th = new Thread(new ParameterizedThreadStart(CriarAnimal));
        th.SetApartmentState(ApartmentState.STA);
        th.IsBackground = true;
        th.Start(animais[i]);
    }
}

public void CriarAnimal(object obj)
{
    Dispatcher.Invoke(() =>
    {
        var animal = obj as Animal;
        var img = new Image()
        {
            Source = animal.Img,
            Tag = animal,
            Width = 32,
            Height = 32,
        };

        double posX = _canvas.ActualWidth - img.Width;
        double posY = _canvas.ActualHeight - img.Height;

        Canvas.SetLeft(img, rnd.NextDouble() * posX);
        Canvas.SetTop(img, rnd.NextDouble() * posY);

        _canvas.Children.Add(img);
    });
}

Will every% created% be an instance? or every Thread is only loading a reference from an instance?

2 - Let's say that the class Thread has a property Animal , and when vida reaches vida 0 ceases to exist or is my / are finalized / destroyed, how do I implement this?

    
asked by anonymous 02.12.2016 / 11:20

1 answer

4
  • Object instance is object instance. Thread is thread . Except for the fact that a thread is a specific object, one thing has nothing to do with the other.

  • Threads are terminated when they are no longer being used. Instances of objects are destroyed when they are no longer useful and C # has automatic management of this.

    Note that this is a bit more complicated. The object containing the thread can be collected shortly after giving Start() . It is no longer needed after the thread starts running. It runs independently.

    The thread itself will exist while what is contained within it ( CriarAnimal ) is running. Note that it can run even with the "official" closure of the application, since it has been placed in background .

  • At the end the code presented will create an array with 10 animals where each one will undergo some creation process. This process is assumed to be long and the 10 may occur in concurrently and concurrently , if given: / p>

    And it's important to understand that threads can slow down the code and not bring gains, on the contrary. there is a great chance that the cost of creating a thread will be greater than the cost of creating a Animal .

    Understand that this is a technique considered almost obsolete . Using tasks can help somewhat minimize the waste of thread creation.

    See more or less what a % parallel_with would be.

    See also: What's the difference between async, multithereading, parallelism, and concurrency? .

    As the comments below, the question has been modified, and indicates that this solution seems less appropriate.

        
    02.12.2016 / 11:41