Tag: 控制台

为什么Console.WriteLine会加速我的应用程序?

好的,这有点奇怪。 我有一个算法来找到最高可能的数值回文,它是两个因子的倍数,每个因子都有K个数字。 我用来找到最高有效回文的方法是查看数字集的最高可能回文(即,如果k = 3,则最高可能是999999,然后是998899等)。 然后我检查回文是否有两个K位数的因子。 为了调试,我认为将每个我正在检查的回文打印到控制台是个好主意(以确保我得到它们。令我惊讶的是,添加 Console.WriteLine(palindrome.ToString()); 每次寻找回文的迭代都会使我的运行时间从大约24秒降低到大约10秒到14秒。 为了validation,我多次运行该程序,然后注释掉Console命令并运行了几次,每次使用 Console命令时都会更短。 这看起来很怪异,有什么想法吗? 如果有人想对此进行打击,这是来源: static double GetHighestPalindromeBench(int k) { //Because the result of k == 1 is a known quantity, and results in aberrant behavior in the algorithm, handle as separate case if (k == 1) { return 9; } ///////////////////////////////////// //These variables will be used […]

如何在程序执行时拥有控制台?

我正在尝试编写一个程序,它在控制台或GUI模式下工作,具体取决于执行参数。 我设法编写以下示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Runtime.InteropServices; namespace wfSketchbook { static class Program { [DllImport(“Kernel32.dll”)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool AttachConsole(int processId); [DllImport(“Kernel32.dll”)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool AllocConsole(); [DllImport(“Kernel32.dll”)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool FreeConsole(); private const int ATTACH_PARENT_PROCESS = -1; /// /// The main entry […]

如何以毫秒精度在特定时间触发C#函数?

我有2台计算机的时间通过NTP同步,这确保时间只有几毫秒的差异。 其中一台计算机将通过TCP向另一台计算机发送消息,以便在将来的两台计算机上的指定时间启动某个c#function。 我的问题是:如何在特定时间以毫秒精度(或更好)触发C#中的函数? 我需要在程序代码中执行此操作(因此任务计划程序或其他外部程序将无济于事)。 总是在一个单独的线程中循环来比较当前时间和目标时间不是一个很好的解决方案我猜。 更新: DateTime.Now不能在解决方案中使用,因为它具有低分辨率。 似乎Thread.Sleep()可以通过导入强制具有1 ms的分辨率: [DllImport(“winmm.dll”, EntryPoint=”timeBeginPeriod”)] public static extern uint MM_BeginPeriod(uint uMilliseconds); 和使用: MM_BeginPeriod(1); 要恢复到先前的分辨率导入: [DllImport(“winmm.dll”, EntryPoint = “timeEndPeriod”)] public static extern uint MM_EndPeriod(uint uMilliseconds); 并使用: MM_EndPeriod(1); 更新2: 我用很多值测试了Thread.Sleep(),看起来它作为一个平均值会趋向于指定的时间跨度。 仅调用Thread.Sleep()一次通常会在目标值时间范围内保持大约半毫秒左右,因此对于毫秒级分辨率来说非常精确。 使用winmm.dll方法timeBeginPeriod和timeEndPeriod似乎对结果的准确性没有影响。 解: 一种方法是使用timeSetEvent(不建议使用)或CreateTimerQueueTimer。 目前的问题是,两者都需要作为一个参数,剩余的时间直到函数触发而不是它应该触发的时间。 因此必须计算触发器所需时间之前的延迟,但DateTime.Now提供低分辨率。 我找到了一个允许高分辨率获取当前DateTime的类。 所以现在剩下的时间可以用高分辨率计算,并作为参数传递给CreateTimerQueueTimer。

pConsole.StartInfo.RedirectStandardOutput和pConsole.Exited事件(c#)

我有一个GUI应用程序执行(在一个新的进程中)“控制台”应用程序并解析输出。 要重定向输出,请将pConsole.StartInfo.RedirectStandardOutput设置为true。 我还订阅了pConsole.Exited事件。 我看到的问题是我必须在Exited事件处理程序中使用Thread.Sleep()来获取最后的数据。 我的退出事件处理程序如下所示: Thread.Sleep(100); // Wait for additional data (if any). pConsole.OutputDataReceived -= new System.Diagnostics.DataReceivedEventHandler(this.localTerminal_DataAvailableEvent); int exit = pConsole.ExitCode; pConsole.Dispose(); pConsole = null; 似乎Exited事件在我的上一个pConsole_DataAvailableEvent之前执行。 任何人都知道这是怎么回事? 在开始执行下一个控制台应用程序之前,我还使用互斥锁/锁来确保我的Exited事件已完成。

在C#console应用程序中更改线程上下文

我有一个C#控制台应用程序,其中我可以通过TCP套接字连接获得输入。 当我通过套接字通过接收function接收输入时,如何切换到主线程? 在WPF中类似于这样的东西: public void TaskDispatcher() { if (DispatcherObjectForTaskDispatcher.Thread != System.Threading.Thread.CurrentThread) DispatcherObjectForTaskDispatcher.Invoke(new TaskDispatcherDelegate(TaskDispatcher)); else { // Do some thing in the UI thread } }

如何通过c#中的socket发送文件

我有服务器和客户端控制台应用程序,可以很好地进行通信以及发送一些字符 这是代码…… 服务器 public static void Main() { try { IPAddress ipAd = IPAddress.Parse(“127.0.0.1”); /* Initializes the Listener */ TcpListener myList = new TcpListener(ipAd, 1234); /* Start Listeneting at the specified port */ myList.Start(); Console.WriteLine(“The server is running at port 8001…”); Console.WriteLine(“The local End point is :” + myList.LocalEndpoint); Console.WriteLine(“Waiting for a connection…..”); Socket s […]

C#在控制台中右对齐文本

有没有办法让我将文本与控制台应用程序的右侧对齐? 我想在同一行但在右侧打印一个带“[ok]”的字符串。 就像你在启动Linux Distro时看到的那样。

Console.WriteLine加快我的代码?

我一直在研究如何加速我的应用程序,因为它对性能至关重要……即每一毫秒我都可以摆脱它更好。 为此,我有一个调用其他方法的方法,其中每个方法都包含一个Stopwatch计时器和Console.WriteLine调用。 即: private void SomeMainMethod() { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); SomeMethod(); sw.Stop(); Console.WriteLine(“Time for SomeMethod = {0}ms”, sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); SomeOtherMethod(); sw.Stop(); Console.WriteLine(“Time for SomeOtherMethod= {0}ms”, sw.ElapsedMilliseconds); //… } 问题是每当我注释掉Stopwatch和Console.WriteLine行代码运行大约20ms(而不是50)慢,这是我需要的很多。 有人知道为什么吗? 编辑: SomeMainMethod方法和类中的其他方法也包含在类似于上面的Stopwatch和Console.WriteLine调用中。 SomeMainMethod及其调用的方法是类的一部分,该类是从控制台测试平台调用的类库的一部分,所有这些都是单线程的。 有关更多信息:该应用程序在x86 .NET 4.6.1发布模式下运行,并启用了优化。 我也在视觉工作室2013中运行它,而不是在它之外。

通过java运行.net控制台时“句柄无效”

我正在尝试通过Java运行dot net console应用程序: process = Runtime.getRuntime().exec(commandLine); 我得到以下输出: Detecting The handle is invalid. 当通过控制台(windows)直接运行它时没有问题: Detecting 100% Done. 100% 我正在以这种forms运行更多的应用程序,但没有问题。 得到了这个堆栈跟踪: Detecting at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded) at System.Console.get_CursorTop() at AutomaticImageOrientation.HelperClasses.General.WriteProgressToConsole(Int32 lastIndex, Int32 totalImages) at AutomaticImageOrientation.MainManager.DetectImage(String[] files, String outputPath, String& globalErrorMessage, Dictionary`2& foundRotations) 问题是当.net应用程序尝试写入控制台时有什么解决方案? 找到导致问题的那一行: Console.CursorLeft = 0; 你知道为什么吗?

如何读取用户按下的键并将其显示在控制台上?

我试图让用户“输入任意一个键”,当按下该键时,它会显示“你按下’键’”。 你能帮忙解决这段代码中的错误吗? 这就是我写的: using System; class Program { public static void Main(string[] args) { Console.Write(“Enter any Key: “); char name = Console.Read(); Console.WriteLine(“You pressed {0}”, name); } }