c# 在几秒钟后停止流读取器。这可能吗?

发布于 2024-09-12 15:03:33 字数 106 浏览 9 评论 0原文

我有网络请求,我使用流阅读器读取信息。我想在 15 秒后停止在这个流读取器之后。因为有时阅读过程需要更多时间,但有时却进展顺利。如果阅读过程花费的时间超过 15 秒,我该如何停止它?我开放所有想法。

I have web request and i read information with streamreader. I want to stop after this streamreader after 15 seconds later. Because sometimes reading process takes more time but sometimes it goes well. If reading process takes time more then 15 seconds how can i stop it? I am open all ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半﹌身腐败 2024-09-19 15:03:33

使用 System.Threading.Timer 并设置一个 15 秒的滴答事件。它不是最干净的,但它会起作用。或者也许是秒表

--stopwatch 选项

        Stopwatch sw = new Stopwatch();
        sw.Start();
        while (raeder.Read() && sw.ElapsedMilliseconds < 15000)
        {

        }

--Timer 选项

        Timer t = new Timer();
        t.Interval = 15000;
        t.Elapsed += new ElapsedEventHandler(t_Elapsed);
        t.Start();
        read = true;
        while (raeder.Read() && read)
        {

        }
    }

    private bool read;
    void t_Elapsed(object sender, ElapsedEventArgs e)
    {
        read = false;
    }

Use a System.Threading.Timer and set an on tick event for 15 seconds. It's not the cleanest but it would work. or maybe a stopwatch

--stopwatch option

        Stopwatch sw = new Stopwatch();
        sw.Start();
        while (raeder.Read() && sw.ElapsedMilliseconds < 15000)
        {

        }

--Timer option

        Timer t = new Timer();
        t.Interval = 15000;
        t.Elapsed += new ElapsedEventHandler(t_Elapsed);
        t.Start();
        read = true;
        while (raeder.Read() && read)
        {

        }
    }

    private bool read;
    void t_Elapsed(object sender, ElapsedEventArgs e)
    {
        read = false;
    }
睫毛溺水了 2024-09-19 15:03:33

既然您说“网络请求”,我假设流读取器包装了您通过调用 httpWebRequest.GetResponse 从 HttpWebRequest 实例获取的 System.IO.Stream ().GetResponseStream()

如果是这种情况,您应该查看 <代码>HttpWebRequest.ReadWriteTimeout

Since you say "web request", I assume that the stream reader wraps a System.IO.Stream which you obtained from a HttpWebRequest instance by calling httpWebRequest.GetResponse().GetResponseStream().

If that's the case, you should take a look at HttpWebRequest.ReadWriteTimeout.

谜兔 2024-09-19 15:03:33

您必须在另一个线程中运行该任务,并从主线程监视它的运行时间是否超过 15 秒:

string result;
Action asyncAction = () =>
{
    //do stuff
    Thread.Sleep(10000); // some long running operation
    result = "I'm finished"; // put the result there
};

// have some var that holds the value
bool done = false;
// invoke the action on another thread, and when done: set done to true
asyncAction.BeginInvoke((res)=>done=true, null);

int msProceeded = 0;
while(!done)
{
    Thread.Sleep(100); // do nothing
    msProceeded += 100;

    if (msProceeded > 5000) break; // when we proceed 5 secs break out of this loop
}

// done holds the status, and result holds the result
if(!done)
{
    //aborted
}
else
{
    //finished
    Console.WriteLine(result); // prints I'm finished, if it's executed fast enough
}

You will have to run the task in another thread, and monitor from your main thread whether it's running longer than 15 seconds:

string result;
Action asyncAction = () =>
{
    //do stuff
    Thread.Sleep(10000); // some long running operation
    result = "I'm finished"; // put the result there
};

// have some var that holds the value
bool done = false;
// invoke the action on another thread, and when done: set done to true
asyncAction.BeginInvoke((res)=>done=true, null);

int msProceeded = 0;
while(!done)
{
    Thread.Sleep(100); // do nothing
    msProceeded += 100;

    if (msProceeded > 5000) break; // when we proceed 5 secs break out of this loop
}

// done holds the status, and result holds the result
if(!done)
{
    //aborted
}
else
{
    //finished
    Console.WriteLine(result); // prints I'm finished, if it's executed fast enough
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文