Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not enough storage is available for `Console.ReadLine`.`

I am using a dual service/console model to test a service of mine. The code in the spotlight is:

static void Main(string[] args)
{
    // Seems important to use the same service instance, regardless of debug or runtime.
    var service = new HostService();
    service.EventLog.EntryWritten += EventLogEntryWritten;

    if (Environment.UserInteractive)
    {
        service.OnStart(args);
        Console.WriteLine("Host Service is running. Press any key to terminate.");
        Console.ReadLine();
        service.OnStop();
    }
    else
    {
        var servicesToRun = new ServiceBase[] { service };
        Run(servicesToRun);
    }
}

When I run the app under the debugger, using F5, on the line Console.ReadLine(); I get a System.IO.IOException, with "Not enough storage is available to process this command."

The only purpose of the ReadLine is to wait until someone presses a key to end the app, so I can't imagine where the data is coming from that needs so much storage.

like image 552
ProfK Avatar asked Feb 17 '15 12:02

ProfK


People also ask

What is the use of console ReadLine () in C#?

The C# readline method is mainly used to read the complete string until the user presses the Enter key or a newline character is found. Using this method, each line from the standard data input stream can be read. It is also used to pause the console so that the user can take a look at the output.

What is console in ReadLine?

This method is used to read the next line of characters from the standard input stream. It comes under the Console class(System Namespace). If the standard input device is the keyboard, the ReadLine method blocks until the user presses the Enter key.

What is the difference between console WriteLine () and console ReadLine ()?

The only difference between the Read() and ReadLine() is that Console. Read is used to read only single character from the standard output device, while Console. ReadLine is used to read a line or string from the standard output device. Program 1: Example of Console.

How do I stop the ReadLine console?

The key is to wrap Console. ReadLine() in a task.


2 Answers

This is a service, and its output is likely set to Windows Application, change the output to Console Application and this should go away.

like image 126
krystan honour Avatar answered Sep 27 '22 00:09

krystan honour


I having the same problem, I found the setting under project properties but I am creating a windows application so I can not change the application type.

This is the code I use.

Dim t As Task = New Task(AddressOf DownloadPageAsync)
t.Start()
Console.WriteLine("Downloading page...")
Console.ReadLine()

Async Sub DownloadPageAsync()

Using client As HttpClient = New HttpClient()
    Using response As HttpResponseMessage = Await client.GetAsync(page)
        Using content As HttpContent = response.Content
            ' Get contents of page as a String.
            Dim result As String = Await content.ReadAsStringAsync()
            ' If data exists, print a substring.
            If result IsNot Nothing And result.Length > 50 Then
                Console.WriteLine(result.Substring(0, 50) + "...")
            End If
        End Using
    End Using
End Using

End Sub

like image 25
Dave Moon Avatar answered Sep 24 '22 00:09

Dave Moon