Question: I am working on a multi-threaded app. I'm processing reports and keeping track of the number of reports in the current batch as well as the total number of reports processed. Whenever I update the counters, I also need to update a label on the GUI which, since the process is on a separate thread, requires a call to a delegate. Which one of these is the better way to go?

```

private void UpdateTotalCount(int newValue)
{
    totalCount = newValue;
    if (labelTotalCount.InvokeRequired)
        BeginInvoke((MethodInvoker) delegate() { 
            labelTotalCount.Text = "Total reports:" + totalcount; });
    else
        labelTotalCount.Text = "Total reports:" + totalcount;
}

```

or 

```

private int totalCount;
public int TotalCount
{
    get { return totalCount; }
    set {
        totalCount = value;
        if (labelTotalCount.InvokeRequired)
            BeginInvoke((MethodInvoker) delegate() { 
                labelTotalCount.Text = "Total reports:" + totalcount; });
        else
            labelTotalCount.Text = "Total reports:" + totalcount;
    }
}
```

**Edit:** Ok, third option.

```

private void UpdateTotalCountLabel()
{
    if (labelTotalCount.InvokeRequired)
        BeginInvoke((MethodInvoker) delegate() { 
            labelTotalCount.Text = "Total reports:" + totalcount; });
    else
        labelTotalCount.Text = "Total reports:" + totalcount;
}
// code elsewhere would look like this
totalCount++;
UpdateTotalCountLabel();
//or
totalCount+= curBatch.Length;
UpdateTotalCountLabel();

```

Answer: 
I'd use observer design pattern and move the update logic to the observer class.  

Don't mix logic and GUI.
*(Update: I misread the order of the options and had them reversed - EM)*

Since your code is "doing something" (setting the count label on an outside component), the first form is usually considered better.

However -- and OOP purists will disagree vehemently with me on this -- neither version is terribly wrong. I've done and seen both. I will not rewrite code that looks like #2 just for the heck of it.

In some contexts, the second alternative might actually be more sensible than the first. For example, if the code needs to track the counter for other reasons and sets and gets it a lot, it might make sense to think of the writing to the label as a View side-effect, and the setting/getting of the count property as the main artifact. In that case I would go for the property get/set pair.

UPDATE:
About the new third alternative:

I actually don't recommend that idea. Now you have separated, into two separate of code, two actions that always must happen together; that's a net loss. You risk forgetting doing one of them somewhere in the code. Either #1 or #2 are better than #3.