© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
73 replies
VeQox

Whats the fastest way to write to the console [Answered]

[StructLayout(LayoutKind.Explicit)]
public struct CHAR_INFO
{
  [FieldOffset(0)]
  internal char UnicodeChar;
  [FieldOffset(0)]
  internal char AsciiChar;
  FieldOffset(2)] //2 bytes seems to work properly
  internal UInt16 Attributes;
}

[StructLayout(LayoutKind.Sequential)]
public struct SMALL_RECT
{
  public short Left;
  public short Top;
  public short Right;
  public short Bottom;
}

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteConsoleOutput(
  IntPtr hConsoleOutput,
  CHAR_INFO[] lpBuffer,
  COORD dwBufferSize,
  COORD dwBufferCoord,
  ref SMALL_RECT lpWriteRegion
);

public static void WriteBuffer(CHAR_INFO[] buffer, short rows, short columns)
{
  IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE);
  SMALL_RECT lpBuffer = new SMALL_RECT() { Left = 0, Top = 0, Right = (short)(Console.BufferWidth - 1), Bottom = (short)          (Console.BufferHeight - 1) };
  if (hnd != INVALID_HANDLE_VALUE)
  {
    WriteConsoleOutput(hnd, buffer, new COORD() { X = columns, Y = rows }, new COORD() { X = 0, Y = 0 }, ref lpBuffer);
  }              
}
[StructLayout(LayoutKind.Explicit)]
public struct CHAR_INFO
{
  [FieldOffset(0)]
  internal char UnicodeChar;
  [FieldOffset(0)]
  internal char AsciiChar;
  FieldOffset(2)] //2 bytes seems to work properly
  internal UInt16 Attributes;
}

[StructLayout(LayoutKind.Sequential)]
public struct SMALL_RECT
{
  public short Left;
  public short Top;
  public short Right;
  public short Bottom;
}

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteConsoleOutput(
  IntPtr hConsoleOutput,
  CHAR_INFO[] lpBuffer,
  COORD dwBufferSize,
  COORD dwBufferCoord,
  ref SMALL_RECT lpWriteRegion
);

public static void WriteBuffer(CHAR_INFO[] buffer, short rows, short columns)
{
  IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE);
  SMALL_RECT lpBuffer = new SMALL_RECT() { Left = 0, Top = 0, Right = (short)(Console.BufferWidth - 1), Bottom = (short)          (Console.BufferHeight - 1) };
  if (hnd != INVALID_HANDLE_VALUE)
  {
    WriteConsoleOutput(hnd, buffer, new COORD() { X = columns, Y = rows }, new COORD() { X = 0, Y = 0 }, ref lpBuffer);
  }              
}

This is what i currently have and it still caps at arround 30fps with 283x480 characters

Now it caps and i cant figure out a more efficient way of doing this OhNo OhNo OhNo
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

❔ Whats the fastest way to write this data to a file? (C# Console App.)
C#CC# / help
3y ago
WPF read and write to Json [Answered]
C#CC# / help
4y ago
How to write function in .net6? [Answered]
C#CC# / help
4y ago
❔ fastest way to add a datatable to database
C#CC# / help
3y ago