Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 25-Feb-13 4:02am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.:
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;
            }
        }
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you can use a BackgroundWorker, it has a 'ReportProgress(int, object)' function that you can use to fetch data.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS
0 CPallini 405
1 Sergey Alexandrovich Kryukov 272
2 OriginalGriff 165
3 George Jonsson 159
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,860
2 CPallini 5,135
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 6 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100