Reputation: 5322
I have a simple WCF-Service that I run in the console when testing it. Sometime I want to have the console windows on top of the other windows so I click it. This activates the "Mark and Paste" command and then freezes the whole application (service becomes unresponsive) until I press enter.
I find that rather annoying.
I don't want to disable the "Mark and Paste", because sometimes I copy text from the console.
Is there any way to detect that the application is frozen?
If I knew when this was the case, I could maybe change the windows titel to display a warning.
EDIT: This is not a duplicate, because I want to know how to handle the freeze and not why it feels frozen. I know now that only the writing to the output is frozen, which causes my application to feel like it is frozen.
Upvotes: 4
Views: 655
Reputation: 101583
As explained in this answer, in this mode terminal just stops reading from your application output, which causes your writes to that output (such as Console.WriteLine(...)
) to hang.
If you control code that writes to console, you can achieve your goal (change console title when "freeze" is detected) like this:
static async Task WriteLine(string text) {
var delay = Task.Delay(TimeSpan.FromSeconds(1));
var writeTask = Task.Run(() => Console.WriteLine(text));
var oldTitle = Console.Title;
if (await Task.WhenAny(delay, writeTask) == delay) {
// after one second Console.WriteLine still did not return
// we are probably in "mark and paste" mode
Console.Title = "FREEZED!";
}
// we cannot ignore our write, have to wait anyway
await writeTask;
Console.Title = oldTitle;
}
And using this method instead of regular Console.WriteLine
. Of course this is of limited usage, but I think it answers your question.
You can test it with simple:
static async Task Main(string[] args) {
while (true) {
await WriteLine("test");
Thread.Sleep(1000);
}
}
and observe that console title will change to FREEZED! while you are in that mode, and will change back when you are out of it.
Upvotes: 4