在 .net 桌面应用程序中设置超时

发布于 2024-08-03 00:40:39 字数 379 浏览 2 评论 0原文

您好,我创建了一个小应用程序来移动一些文件并将它们放入数据库中。除了应用程序需要超时之外,这种方法运行良好。

如果我的应用程序在 2 小时内未完成任务,它会重新打开并锁定所有内容。我需要说的是,如果我的申请已开放 2 小时,请关闭它。

一个想法是您可以在应用程序中设置超时吗?我对此进行了谷歌搜索,并在 system.threading 中找到了一些关于 timeout=infinite 和 threading.sleep=200 的内容,但并不真正理解这一点。

另一个想法是使用计时器并计算 2 小时,然后调用 close 方法,这看起来有点作弊。

关于 C# 或 Vb 中超时的任何想法


遗憾的是,这不是针对数据库连接,这不是应用程序失败的地方。

Hi I have created a little application to move some files around and put them into a database. This is working well except that the application needs a timeout.

If my app has not completed the task within 2 hours it re opens and locks out everything. I need to say that if my application has been open for 2 hours close it.

One thought was can you set a time out in an application. I have had a google for this and found some stuff in system.threading about timeout=infinite and threading.sleep=200 but don't really understand this.

Another thought, is using the timer and counting up to 2 hours and then calling a close method, this seams like a bit of a cheat.

Any ideas on time out in either C# or Vb


Sadly this is not for a db connection this is not where the application is failing.

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

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

发布评论

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

评论(3

岁月蹉跎了容颜 2024-08-10 00:40:39

只允许应用程序的一个实例同时运行怎么样?

我猜您正在安排运行:查看此代码,将其作为在不更改计划的情况下防止应用程序出现多个实例的方法。

static void Main(string[] args)
{
    Mutex flag = new Mutex(false, "UNIQUE_NAME");
    bool acquired = flag.WaitOne(TimeSpan.FromMilliseconds(0));  // 0 or more milliseconds
    if (acquired)
    {
        Console.WriteLine("Ok, this is the only instance allowed to run.");

        // do your work, here simulated with a Sleep call
        Thread.Sleep(TimeSpan.FromSeconds(5));

        flag.ReleaseMutex();
        Console.WriteLine("Done!");
    }
    else
    {
        Console.WriteLine("Another instance is running.");
    }
    Console.WriteLine("Press RETURN to close...");
    Console.ReadLine();
}

请不要放弃你的互斥锁...;-)

HTH

What about allowing just one instance of your application to run at the same time?

I guess you are scheduling runs: have a look at this code as a way of preventing multiple instances of your application without changing the scheduling.

static void Main(string[] args)
{
    Mutex flag = new Mutex(false, "UNIQUE_NAME");
    bool acquired = flag.WaitOne(TimeSpan.FromMilliseconds(0));  // 0 or more milliseconds
    if (acquired)
    {
        Console.WriteLine("Ok, this is the only instance allowed to run.");

        // do your work, here simulated with a Sleep call
        Thread.Sleep(TimeSpan.FromSeconds(5));

        flag.ReleaseMutex();
        Console.WriteLine("Done!");
    }
    else
    {
        Console.WriteLine("Another instance is running.");
    }
    Console.WriteLine("Press RETURN to close...");
    Console.ReadLine();
}

Please, don't abandon your Mutex... ;-)

HTH

微暖i 2024-08-10 00:40:39

还有另一种选择吗?设置一个标志来指示进程当前是否正在运行。

如果该进程正在运行,则不要启动第二个或关闭第一个,或者可以设置一个计时器并在几分钟后再次检查(?)

What about another option? Set a flag which says whether the process is currently running or not.

If the process is running then dont start the second one or close the first one or may be be set a timer and check again after few minutes(?)

晚雾 2024-08-10 00:40:39

也许 DbConnection/DbCommand 对象的超时更合适(看看 MSDN)

Maybe a timeout for the DbConnection/DbCommand objects is more appropriate (have a look at MSDN)

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