为什么我的代码会一直滞后? [英] Why does my code keep lagging out?

查看:82
本文介绍了为什么我的代码会一直滞后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用asp.net中的c#编写这个代码......它一直在运行但是它的滞后。它没有崩溃,但它没有显示它的假设,除非我删除循环获取我的文件并将其输出到文本框。



这个程序检查一个目录中的文件是不是它重新启动循环,如果它在那里它发送它然后删除它重复...



< b>我尝试了什么:



Hi, ive been working on this code in c# in asp.net... Its been running but its lagging. It doesn't crash but it doesn't show what its suppose to unless I remove the loop getting my file and outputting it to a textbox.

This program checks for a file in a directory is it isn't there it restarts loop if it is there it sends it then deletes it the repeats...

What I have tried:

   protected void Button5_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "" || TextBox1.Text.Contains(" "))
            {
                Button6.Visible = true;
            }
            else
            {
                if (Directory.Exists("C:\\Users\\Logan\\Documents\\Visual Studio 2015\\Projects\\TFR\\data\\codes\\" + TextBox1.Text))
                {
                    Button6.Visible = false;
                    Button5.Visible = false;
                    TextBox1.Visible = false;
                    TextBox2.Visible = true;
                    SendText("Starting privatefeed using the code \"" + TextBox1.Text + "\"");
                    SendText("Developed and created by Logan Risen!");
                    SendText("Our discord: https://discord.gg/vJcx3sz");
                    SendText("-----------------------------------------------------------------------------");
                    Task wait = Task.Delay(2000);
                    wait.Wait();
                    Run();
                    
                }


                else
                {
                    Button6.Visible = true;
                }
            }
        }
       
   /*     private void Start()
        {
            Task t = Task.Delay(8000);
            t.Wait();
            Run();
        }
      */  private void Run()
        {
         //   System.Threading.Thread.Sleep(1000);
         
                
                try
                {
                string latest = File.ReadAllText("C:\\Users\\Logan\\Documents\\Visual Studio 2015\\Projects\\TFR\\data\\codes\\" + TextBox1.Text + "\\group.txt");
                Task delay = Task.Delay(5000);
                    delay.Wait();
                
                    if (File.Exists("C:\\Users\\Logan\\Documents\\Visual Studio 2015\\Projects\\TFR\\data\\codes\\" + TextBox1.Text + "\\group.txt"))
                    {
                        if (latest == "")
                        {
                            Console.WriteLine("N1");
                        }
                        else
                        {
                            if (latest == last)
                            {
                                Console.WriteLine("N2");
                            }
                            else
                            {
                                SendText(latest);
                            }
                        }
                        last = latest;
                        if (latest == "")
                        {
                            Console.WriteLine("N2");
                        }
                        else
                        {
                            Console.WriteLine("D");
                            File.Delete("C:\\Users\\Logan\\Documents\\Visual Studio 2015\\Projects\\TFR\\data\\codes\\" + TextBox1.Text + "\\group.txt");
                        }

                        Run();
                    }
                    else
                    {
                        Run();
                    }
                }
                catch
                {
                Run();
                }
            }
    }
}

推荐答案

因为所有页面可能滞后代码中的 wait()。你需要准确解释代码应该做什么。

它也看起来代码无休止地调用它并且永远不会退出。等待是阻止stackoverflow的唯一因素,因为你在崩溃之前停止了应用程序。



当你不明白你的代码在做什么或为什么这样做该怎么做,答案是调试器

使用调试器查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Page probably lagging because of all the wait() in code. You need to explain exactly what the code is supposed to do.
It also look that the code call itself endlessly and never exit. The waits are the only thing that prevent a stackoverflow because you stop the app before the crash.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于为什么我的代码会一直滞后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆