![](/img/trans.png)
[英]c# program closes and restarts if I run the .exe by double click or cmd
[英]Progressbar in C# on double click of .exe
我有一個捆綁在ISO映像中的Java應用程序,其中有一個用c#編寫的啟動器。 當我通過CD啟動應用程序時,等待時間很長,這使用戶錯誤地認為應用程序沒有啟動。 我試圖在Java應用程序中放置一個progressbar,並在程序開始時調用它,但是失敗了。 所以我試圖在啟動器中啟動進度條。
下面的啟動器代碼
Program.cs
using System.Security.Principal;
using System.Diagnostics;
namespace RunMyprogram
{
static class Program
{
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + @"/myBatFile.bat";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Verb = "runas";
Process.Start(startInfo);
}
}
}
請讓我知道如何在此代碼中添加進度條。
啟動一個新線程,在該線程上以添加點的形式顯示進度。 由於應用程序無法了解當前執行的狀態,因此我們無法顯示進度條說明已完成的百分比。
您可以做的是顯示一個無休止的進度選項,並顯示一條消息,例如“啟動應用程序,這可能最多需要10分鍾.....感謝您的耐心等待。”
此代碼如下所示:
using System.Security.Principal;
using System.Diagnostics;
using System.Threading; // for ThreadStart delegate and Thread class
using System; // for Console class
namespace RunMyprogram
{
static class Program
{
static void Main(string[] args)
{
ThreadStart ts = new ThreadStart(ShowProgress);
Thread t = new Thread(ts);
t.Start();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + @"/myBatFile.bat";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Verb = "runas";
Process.Start(startInfo);
t.Join();
}
static void ShowProgress()
{
// This function will only show user that the program is running, like aspnet_regiis -i shows increasing dots.
Console.WriteLine(""); //move the cursor to next line
Console.WriteLine("Launching the application, this may take up to 10 minutes..... Thanks for your patience.");
// 10 minutes have 600 seconds, I will display 'one' dot every 2 seconds, hence the counter till 300
for(int i = 0; i < 300; i++)
{
Console.Write(". ");
Thread.Sleep(2000);
}
}
}
}
除了for(int i = 0; i < 300; i++)
您還可以使用while(true)
(無盡)循環,但是為此,您必須能夠知道第二個應用程序是否已啟動,以便您可能有一個條件可以擺脫無限循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.