Why does splitting attention between a primary task and another task appear to improve performance on the primary task? I've been thinking about my performance at various tasks and where my focus is at during them and something's really got me confused.

It makes sense that devoting all my focus to a task while I'm doing it would yield the best results that I'm capable of. In my case, however, the opposite seems true.

If I'm trying my best to accomplish something and it's not working to my satisfaction (bugs in my program code that don't appear to have a reasonable cause for example) - and this only happens when I'm really focusing on the task - I get irritated with my apparent inability to come to the desired result and my performance declines, which irritates me more.

On the other hand, if I have my focus split (i.e. playing a movie or tv series while I'm busy with the task), I stay a lot calmer and generally my performance at said task stays pretty high.

It's very clear to me that I don't perform well under pressure, but in a lot of cases, there's no pressure aside from what I put on myself.

So my question is this: how is it that when I'm just absent-mindedly doing something, it generally turns out better than it would if I focused on it and tried my hardest?
Dual Task research

In general, I think a lot depends on what is the secondary task and how it relates to the primary task (see human multitasking).
In general, secondary tasks tend to lower performance on the primary task. 

A classic combinations where there may be facilitation is music with gross motor tasks, where the music is often experienced as motivating.
In other cases, music can serve to block out other external distractions, such as in a noisy office.

However, in general, it would be unusual for task performance to be improved by simultaneously watching television while doing the task. It might make the task more enjoyable, but it would be rare for it to improve productivity. The intrusive way that TV impinges on your attention would generally interfere with flow and focus on the primary task, especially where the primary task involves cognitive control.

Incubation

That said, there is a literature on the benefits of breaks and incubation on problem solving. So, in general, taking a break after working on a task intensely and getting stuck, can be a useful way of breaking the impasse. In particular, a good night sleep can be particularly helpful.

Motivation

Another way of framing your question is in terms of the effect of a secondary task on motivation and persistence. Making the task more enjoyable might have an effect on your persistence. 

However, drawing on the expertise literature, I'd say it's often better to work in a focussed and concentrated way for a shorter period of time, than to do a task in a half hearted way for a longer period of time. If you've truly lost the ability to concentrate, then it may be time to rest and recuperate, both in terms of good sleep and time off task.