Thread.Sleep() 和构造函数修改?

发布于 2024-12-16 18:28:19 字数 809 浏览 0 评论 0原文

我试图让对象的构造函数将字段初始化为某个内容,暂停 30 秒,然后将其设置为其他内容。

这是我的代码:

namespace Practice
{
    public enum TransactionStatus { Pending, Complete }

    public class Transaction
    {
        private TransactionStatus status;

        public Transaction()
        {
            this.status = TransactionStatus.Pending;
            //(I'm trying to set this to TransactionStatus.Complete after 30 seconds. What do I do afterwards?)
        }

        // Here is the method to do it... Am I right to think that.status this must be reset this.status in the constructor?
        public TransactionStatus SetStatus()
        {
            // sleep for 30 seconds and then proceed.
            System.Threading.Thread.Sleep(3000);
            return TransactionStatus.Complete;
        }
    }

I'm trying to get the constructor of an object to initialize a field to something, pause for 30 seconds, and then set it to something else afterwards.

Here is my code:

namespace Practice
{
    public enum TransactionStatus { Pending, Complete }

    public class Transaction
    {
        private TransactionStatus status;

        public Transaction()
        {
            this.status = TransactionStatus.Pending;
            //(I'm trying to set this to TransactionStatus.Complete after 30 seconds. What do I do afterwards?)
        }

        // Here is the method to do it... Am I right to think that.status this must be reset this.status in the constructor?
        public TransactionStatus SetStatus()
        {
            // sleep for 30 seconds and then proceed.
            System.Threading.Thread.Sleep(3000);
            return TransactionStatus.Complete;
        }
    }

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

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

发布评论

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

评论(2

掀纱窥君容 2024-12-23 18:28:20

这是一种替代作弊方法,因为您说您正在使用 toString();

private DateTime called;

public TranlationStatus()
{
    this.called = DateTime.Now; 
}

public ToString()
{
    if (this.called - DateTime.Now < new TimeSpan(0,0,20))
    {
        return TransationStatus.Pending.ToString();
    }
    else
    {
        return TransactionStatus.Complete.ToString();
    }
}

Here's an alternative cheating method since you said you're using toString();

private DateTime called;

public TranlationStatus()
{
    this.called = DateTime.Now; 
}

public ToString()
{
    if (this.called - DateTime.Now < new TimeSpan(0,0,20))
    {
        return TransationStatus.Pending.ToString();
    }
    else
    {
        return TransactionStatus.Complete.ToString();
    }
}
雄赳赳气昂昂 2024-12-23 18:28:19

老实说,我不会把它放在构造函数中。我会在主代码中执行此操作:

TransactionStatus status = new TransactionStatus();

Thread.Sleep(3000);

status.SetStatus();

当然,这会阻止整个程序。如果您不想这样做,则必须编写自己的函数并将其作为单独的线程调用。再次在主代码中。

这就是说我真的无法理解你为什么要做这样的事情。

I wouldn't put it in the constructor to be honest. I'd do it in the main code:

TransactionStatus status = new TransactionStatus();

Thread.Sleep(3000);

status.SetStatus();

Of course that will block the entire program. If you don't want that, you'll have to write your own function and call it as a separate thread. Again in the main code.

That said I really can;t understand why you want to do something like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文