【问题标题】:C# - Thread.Sleep() doesn't seem to work in my Windows ServiceC# - Thread.Sleep() 在我的 Windows 服务中似乎不起作用
【发布时间】:2013-09-20 02:08:09
【问题描述】:

我可以访问一个特定的 API,该 API 实施了每秒大约 3 个 API 调用的速度限制。我正在使用 C# 创建一个 Windows 服务,我想如果我只是在调用之间放置一个“Thread.Sleep(4000)”,那么每个调用都会延迟 4 秒。好吧,我有一个“for”循环,循环了 10 次,用于测试。在一秒钟左右的时间内,它将从 API 中提取的所有 10 条记录插入到我的数据库中。所以,Thread.Sleep(4000) 没有被遵守。我读过Thread.Sleep() 仅适用于当前线程。我对线程了解不多,但我希望你们中的一个人能告诉我我做错了什么,或者至少建议一种替代方法来遵守 API 交通法规。这是我的代码的相关部分:

    using (SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=*****;Integrated Security=true;"))
    {
        for (int i = 0; i < 10; i++)
        {
            movie = api.GetMovieInfo(i);
            Thread.Sleep(4000);
            if (string.IsNullOrEmpty(movie.title))
                continue;
            string queryString = string.Format("insert into movie values ('{0}')", movie.title);

            SqlCommand command = new SqlCommand(queryString, connection);
            try
            {
                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
    }

【问题讨论】:

  • 为什么不使用Threading.Timer
  • @DonA 感谢您的建议...这与仅强制线程休眠有何不同?
  • 解决这个问题的方法不是 stackoverflow,而是记录实际发生的事情。很可能您没有运行您认为正在运行的代码,或者查询发生的地方不在您认为发生的地方。我们帮不了你。 但我们可以告诉您始终记录。
  • 我想更好的做法,除了明显的差异。我没有强迫任何事情,我正在利用 .Net 做正确的事情。
  • 这段代码是否在 Windows 服务的工作线程中?

标签: c# multithreading windows-services thread-sleep


【解决方案1】:

您可能会看到这种行为,因为您对 Thread.Sleep 的调用发生在 using 语句中。尽管我仍然希望它至少需要四秒钟,并且您说只在一秒钟内插入了所有 10 条记录。您可以尝试从 using 语句中删除对 Thread.Sleep 的调用,以查看行为是否有所改善...

见:C# exiting a using() block with a thread still running onthe scoped object

我确实认为 Threading.Timer 是一个更好的选择,但我也认为 Thread.Sleep 应该也可以工作:

    for (int i = 0; i < 10; i++)
    {
        DoAPIWork();
        Thread.Sleep(4000);
    }

    private void DoAPIWork()
    {
        using (SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=*****;Integrated Security=true;"))
        {
            movie = api.GetMovieInfo(i);

                if (string.IsNullOrEmpty(movie.title))
                    continue;
                string queryString = string.Format("insert into movie values ('{0}')", movie.title);

                SqlCommand command = new SqlCommand(queryString, connection);
                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
        }
    }

【讨论】:

    【解决方案2】:

    尝试在新线程上执行它

    例如

    for(int i = 0; i < 10; i++)
    {
         Thread executionThread = new Thread(() =>
         {
             //Call your api process here
             DoWork();
         });
         executionThread.Start();
         // This stops the current thread and wait for the executionThread
         executionThread.Join();
    }
    
    and then let your DoWork() handle the sleep.
    
    public void DoWork()
    {
       Thread.Sleep(4000);
       // Do actual work here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      相关资源
      最近更新 更多