Thread.Sleep() 和构造函数修改?
我试图让对象的构造函数将字段初始化为某个内容,暂停 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种替代作弊方法,因为您说您正在使用 toString();
Here's an alternative cheating method since you said you're using toString();
老实说,我不会把它放在构造函数中。我会在主代码中执行此操作:
当然,这会阻止整个程序。如果您不想这样做,则必须编写自己的函数并将其作为单独的线程调用。再次在主代码中。
这就是说我真的无法理解你为什么要做这样的事情。
I wouldn't put it in the constructor to be honest. I'd do it in the main code:
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.