In C#; what is the most efficent way to check for changes from a background thread?

Damien

I create a thread for each of my network communications and they add to a list responses whenever they hear back from a client. I'm starting up the task below at execution to see if any communications come in. It displays the most recent one on screen.

Task task = new Task(
(() =>
 {
    int i = 0;
    while (true)
    {
        if (responses.Count > i){
            Debug.WriteLine(responses[i]);
            int index = Form.ActiveForm.Controls.IndexOfKey("responseBox");
            Form.ActiveForm.Invoke((MethodInvoker) (() => Form.ActiveForm.Controls[index].Visible = true));
            Form.ActiveForm.Invoke((MethodInvoker) (() => Form.ActiveForm.Controls[index].Text = responses[i]));
            i++;
        }
    }
 }));
task.Start();

My question is; is there a better way for me to do this? It seems wrong to me to have the task constantly working for something that doesn't happen very often.

Edit: I'm very new to C#, so if there's something obvious please don't hesitate to point it out.

Update:

As per the nice tutorial from MS that sidewinder linked I added a simple event to the Add function of List. as So:

public delegate void ChangedEventHandler(object sender, EventArgs e);

public class listWithChanges<T> : List<T> 
{
    public event ChangedEventHandler Changed;

    protected virtual void OnChanged(EventArgs e)
    {
        if (Changed != null)
            Changed(this, e);
    }

    public new void Add (T item)
    {
        base.Add(item);
        OnChanged(EventArgs.Empty);
    }
}

and added to my output with a delegate

responses.Changed += ((o, e) => {
                                int index = Form.ActiveForm.Controls.IndexOfKey("responseBox");
                                Form.ActiveForm.Invoke((MethodInvoker) (() => Form.ActiveForm.Controls[index].Visible = true));
                                Form.ActiveForm.Invoke((MethodInvoker) (() => Form.ActiveForm.Controls[index].Text = responses[responses.Count - 1]));
                              });
Genos

Events would be a nice solution.

An event is an implementation of the Observer pattern, in which the source (the network communications) warn it's observers(whoever calls the task in your example) that something happened.

It's a lot more efficient since it doesn't waste CPU usage in an infinite loop, the method only executes when a client responds.

C# has a great support for events, consider reading the MS Tutorial(originally posted by Sidewinder94).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the most efficent / fastest way to iterate values in an iptables rule?

From Dev

Most efficent way to move a directory in haskell

From Dev

In Javascript, what is the most compact, elegant and efficent way to bring the last element of an array to the beginning?

From Dev

What is the most efficent way to allow users to bookmark pages (ids, nodes, etc) with mysql?

From Dev

What is the most efficient way to check if a C++ container is empty?

From Dev

What is the most professional way to size webpage background?

From Dev

What is a reliable way to check if a thread is still running?

From Dev

What is the most efficient way to store recent changes for a mysql table

From Dev

What it is the best way to perform a task in background (thread or call method from application_start)?

From Dev

What it is the best way to perform a task in background (thread or call method from application_start)?

From Dev

What is the C#/.NET 4.0+ way to implement a cancellable background thread?

From Dev

What is the most efficient way to use the or operator in C?

From Dev

What is the most efficient way to use the or operator in C?

From Dev

are there an efficent way to sort a parametermap?

From Dev

What is the most efficient way to check for a sequence of numbers in a JavaScript array?

From Java

What is the most pythonic way to check if multiple variables are not None?

From Dev

What is the most efficient way to check each line up to a certain character

From Dev

What is the most pythonic way to check if multiple variables are not None?

From Dev

What is the most efficient way to check if item exists in Arraylist?

From Dev

What is the most effective way to check if user exists in database?

From Dev

What is the most efficient way to check each line up to a certain character

From Dev

What is the most efficient way to check if Node is currently installed?

From Dev

What's the most efficient way to programmatically check if the year is changed

From Dev

Check what thread a method is being called from

From Dev

How to check state of one thread from another in a thread safe way?

From Dev

What is the quickest way to check if an svn working copy has changes?

From Dev

What is the fastest way to check recursively for changes in a Git working copy with jgit?

From Dev

Most efficient way with Pandas to check pair of values from 2 series?

From Dev

Most efficient way with Pandas to check pair of values from 2 series?

Related Related

  1. 1

    What is the most efficent / fastest way to iterate values in an iptables rule?

  2. 2

    Most efficent way to move a directory in haskell

  3. 3

    In Javascript, what is the most compact, elegant and efficent way to bring the last element of an array to the beginning?

  4. 4

    What is the most efficent way to allow users to bookmark pages (ids, nodes, etc) with mysql?

  5. 5

    What is the most efficient way to check if a C++ container is empty?

  6. 6

    What is the most professional way to size webpage background?

  7. 7

    What is a reliable way to check if a thread is still running?

  8. 8

    What is the most efficient way to store recent changes for a mysql table

  9. 9

    What it is the best way to perform a task in background (thread or call method from application_start)?

  10. 10

    What it is the best way to perform a task in background (thread or call method from application_start)?

  11. 11

    What is the C#/.NET 4.0+ way to implement a cancellable background thread?

  12. 12

    What is the most efficient way to use the or operator in C?

  13. 13

    What is the most efficient way to use the or operator in C?

  14. 14

    are there an efficent way to sort a parametermap?

  15. 15

    What is the most efficient way to check for a sequence of numbers in a JavaScript array?

  16. 16

    What is the most pythonic way to check if multiple variables are not None?

  17. 17

    What is the most efficient way to check each line up to a certain character

  18. 18

    What is the most pythonic way to check if multiple variables are not None?

  19. 19

    What is the most efficient way to check if item exists in Arraylist?

  20. 20

    What is the most effective way to check if user exists in database?

  21. 21

    What is the most efficient way to check each line up to a certain character

  22. 22

    What is the most efficient way to check if Node is currently installed?

  23. 23

    What's the most efficient way to programmatically check if the year is changed

  24. 24

    Check what thread a method is being called from

  25. 25

    How to check state of one thread from another in a thread safe way?

  26. 26

    What is the quickest way to check if an svn working copy has changes?

  27. 27

    What is the fastest way to check recursively for changes in a Git working copy with jgit?

  28. 28

    Most efficient way with Pandas to check pair of values from 2 series?

  29. 29

    Most efficient way with Pandas to check pair of values from 2 series?

HotTag

Archive