Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Ok, Here's my question:-
I've read pages and pages of differing answers / opinions of how to update a UI from a different thread. My situation is subtly different but not such that it will require anything different:
I have a class from which I want to sporn at least one thread. At the moment I have two threads (one increamenting and one decrementing) both with an instance of the same class and outputting their results to the console.
I want my instance attached to my thread to be ablke to change / update or query data that is in my main / calling class. I want this to be really easy and have tried to implement one of the suggested methods from another source using a delegate but to no avail. I can't seem to get my head around how these objects can communicate with each other without having a reference to each other ?
I see the solution being something like:-
- get id of the main thread and send it to my worker thread as a parametre.
- use this reference to the calling thread to access the properties of my calling thread. or:-
Send an object containing some properties to my new object / from it...

How though ?
Posted

You don't have to send anything from one thread to another.
Both threads can read variables updated by the other thread (periodically or not) and perform actions accordingly.

As simple as that!

Of course you can choose any way to implement the alleged passing of data, one way is using a Queue.

e.g.:
C#
public class Queue<T>
{
    /// <summary>Used as a lock target to ensure thread safety.</summary>
    private readonly Locker _Locker = new Locker();

    private readonly System.Collections.Generic.Queue<T> _Queue = new System.Collections.Generic.Queue<T>();

    /// <summary></summary>
    public void Enqueue(T item)
    {
        lock (_Locker)
        {
            _Queue.Enqueue(item);
        }
    }

    /// <summary>Enqueues a collection of items into this queue.</summary>
    public virtual void EnqueueRange(IEnumerable<T> items)
    {
        lock (_Locker)
        {
            if (items == null)
            {
                return;
            }

            foreach (T item in items)
            {
                _Queue.Enqueue(item);
            }
        }
    }

    /// <summary></summary>
    public T Dequeue()
    {
        lock (_Locker)
        {
            return _Queue.Dequeue();
        }
    }

    /// <summary></summary>
    public void Clear()
    {
        lock (_Locker)
        {
            _Queue.Clear();
        }
    }

    /// <summary></summary>
    public Int32 Count
    {
        get
        {
            lock (_Locker)
            {
                return _Queue.Count;
            }
        }
    }

    /// <summary></summary>
    public Boolean TryDequeue(out T item)
    {
        lock (_Locker)
        {
            if (_Queue.Count > 0)
            {
                item = _Queue.Dequeue();
                return true;
            }
            else
            {
                item = default(T);
                return false;
            }
        }
    }
}
 
Share this answer
 
If you can use a BackgroundWorker, it has a 'ReportProgress(int, object)' function that you can use to fetch data.
 
Share this answer
 
Appologise for my late reply Edo, I have been diverted onto other things. Actually my question was much more simple but your post has answered what would have been my very next question. I have implemented a locker and all is working fine.

Thanks also Daniel, I have researched the background worker too. Not sure if it is exactly what I need as I want a number of objects in a number of threads who may / will need to communicate with each other. I guess it's possible.

Many thanks for your help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900