System.Timers dasturidan foydalanib, konsol ilovasi va chiqish turi "Windows Application"

Taymerni ishlatadigan odatiy konsol ilovasi yordamida biz ilovaning yopilishiga yo'l qo'ymaslik uchun "Console.ReadLine ()" dan foydalanishimiz mumkin. Konsol ilovasining chiqish turi "Windows Application" bo'lsa, mos keladigan echim bormi?

Yodingizda tuting: Windows xizmati bu holatda emas, chunki jarayonlarni boshlayman.

class Program
{        
    private static Timer _Timer;       

    static void Main(string[] args)
    {   
        SetupTimer();                        
    }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {           
        Process.Start(@"C:\WINDOWS\system32\notepad.exe");
    }

    private static void SetupTimer()
    {
        _Timer = new Timer();
        _Timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        _Timer.Interval = 3000;
        _Timer.Enabled = true;
    }
}

enter image description here

1
@CodingYoshi Bu yerga qarang. Mening ilovam yopish uchun hech qanday oyna yo'q. stackoverflow.com/questions/2686289/…
qo'shib qo'ydi muallif ManxJason, manba
@HansPassant Nima uchun taymerni ishlatmoqchi bo'lmoqchi bo'lsam, savolimga aloqador emas, lekin boshqa jarayonni kuzatmoqchiman va tugatgandan keyin boshqa dasturni ishga tushirishni istayman. Thread.Sleep() ishni bajaradi. Rahmat
qo'shib qo'ydi muallif ManxJason, manba
Process.Start() tomonidan qaytarilgan Process misolini saqlang va keyin WaitForExit() usuli?
qo'shib qo'ydi muallif Idle_Mind, manba
Nima uchun siz Taymerni ishlatmoqchi ekanligingiz juda aniq emas. Thread.Sleep() buni ham bajarishi mumkinligi uchun baxtli bo'lish uchun foydalanuvchi interfeysi yo'q.
qo'shib qo'ydi muallif Hans Passant, manba
Windows ilovasi deraza mavjud, shunda oyna yopilmasa, taymer ishga tushadi. Oynani yopish oynani yopish uchun foydalanuvchi buyrug'i bo'lib, konsolda kalitni kiritish bilan bir xil konsolni yopish buyrug'i. Bu erda nima so'rayotganingizga amin emasman.
qo'shib qo'ydi muallif CodingYoshi, manba

7 javoblar

Istagan narsaga erishish uchun:

using System;
using System.Diagnostics;
using System.Timers;

public class Program
{
    private static Timer _Timer;
    private static bool Launched = false;
    static void Main(string[] args)
    {
        SetupTimer();
        WaitUntilItIsLaunched:
        if (!Launched)
        {
            System.Threading.Thread.Sleep(100);
            goto WaitUntilItIsLaunched;
        }
    }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Process.Start(@"C:\WINDOWS\system32\notepad.exe");
        Launched = true;
    }

    private static void SetupTimer()
    {
        _Timer = new Timer();
        _Timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        _Timer.Interval = 3000;
        _Timer.Enabled = true;
    }
}
1
qo'shib qo'ydi
OK, shuning uchun biz faqat mening ahvolimga qadar ipni doimo uxlaymiz. Juda ham oqlangan, lekin rahmat.
qo'shib qo'ydi muallif ManxJason, manba
Vazifalar va teglar bilan ishlashni ishlatishingiz mumkin, ammo bu sizni tushunish qiyin kechadi. Biroq, bu yomon yechim emas.
qo'shib qo'ydi muallif user7358693, manba

Istagan narsaga erishish uchun:

using System;
using System.Diagnostics;
using System.Timers;

public class Program
{
    private static Timer _Timer;
    private static bool Launched = false;
    static void Main(string[] args)
    {
        SetupTimer();
        WaitUntilItIsLaunched:
        if (!Launched)
        {
            System.Threading.Thread.Sleep(100);
            goto WaitUntilItIsLaunched;
        }
    }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Process.Start(@"C:\WINDOWS\system32\notepad.exe");
        Launched = true;
    }

    private static void SetupTimer()
    {
        _Timer = new Timer();
        _Timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        _Timer.Interval = 3000;
        _Timer.Enabled = true;
    }
}
1
qo'shib qo'ydi
OK, shuning uchun biz faqat mening ahvolimga qadar ipni doimo uxlaymiz. Juda ham oqlangan, lekin rahmat.
qo'shib qo'ydi muallif ManxJason, manba
Vazifalar va teglar bilan ishlashni ishlatishingiz mumkin, ammo bu sizni tushunish qiyin kechadi. Biroq, bu yomon yechim emas.
qo'shib qo'ydi muallif user7358693, manba

Kamroq kodlar qatori:

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Timers;
public class Program
{
    static void Main(string[] args)
    {
        int MsToWait = 3000;
        int MsElapsed = 0;
        WaitLaunch:
        System.Threading.Thread.Sleep(100);
        MsElapsed += 100;
        if (MsElapsed >= MsToWait)
        {
            Process.Start(@"C:\WINDOWS\system32\notepad.exe");
            return;
        }
        else
            goto WaitLaunch;
    }
}
1
qo'shib qo'ydi
Foydalanishda foydalanganingizda goto dan foydalanmang.
qo'shib qo'ydi muallif Phil1970, manba
Shuningdek, bir xil savolga ko'p javob bermang.
qo'shib qo'ydi muallif Phil1970, manba
To'g'ri bo'lgan ekanman, juda ko'p javob beraman va ular sezilarli darajada farq qiladi. Agar sizda goto in'ikosiz deb hisoblasangiz, to'liq javob berishingiz kerak, agar yo'q bo'lsa, bu erda hech kimga yordam bermaysiz. Ushbu holatdagi goto, taymerdan ko'ra, ancha ko'paytiriladi.
qo'shib qo'ydi muallif user7358693, manba

Yoki undan kichikroq kod:

using System;
using System.Diagnostics;
using System.Threading;
public class Program
{
    static void Main(string[] args)
    {
        DateTime LaunchAt = DateTime.Now.AddSeconds(3);
        WaitLaunch:
        Thread.Sleep(100);
        if (DateTime.Now >= LaunchAt)
            Process.Start(@"C:\WINDOWS\system32\notepad.exe");
        else
            goto WaitLaunch;
    }
}
0
qo'shib qo'ydi
The Thread.Sleep (100); kodning muhim qismidir, chunki u holda, protsessor protsessorning o'z imkoniyatlarining 100% ishlatilishiga sababchi bo'lib, soniyada millionlab vaqtni o'z ichiga oladi.
qo'shib qo'ydi muallif user7358693, manba

Yoki undan kichikroq kod:

using System;
using System.Diagnostics;
using System.Threading;
public class Program
{
    static void Main(string[] args)
    {
        DateTime LaunchAt = DateTime.Now.AddSeconds(3);
        WaitLaunch:
        Thread.Sleep(100);
        if (DateTime.Now >= LaunchAt)
            Process.Start(@"C:\WINDOWS\system32\notepad.exe");
        else
            goto WaitLaunch;
    }
}
0
qo'shib qo'ydi
The Thread.Sleep (100); kodning muhim qismidir, chunki u holda, protsessor protsessorning o'z imkoniyatlarining 100% ishlatilishiga sababchi bo'lib, soniyada millionlab vaqtni o'z ichiga oladi.
qo'shib qo'ydi muallif user7358693, manba

Agar siz bir necha dasturlarni turli vaqtlarda ishga tushirishni va ularni ishga tushirishni kutmoqchi bo'lsangiz:

using System;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
public class Program
{
    static Dictionary ExecutablesToLaunch = new Dictionary();
    static List ExecutablesLaunched = new List();
    static void Main(string[] args)
    {
        ExecutablesToLaunch.Add(@"C:\WINDOWS\system32\notepad.exe", DateTime.Now.AddSeconds(3));
        ExecutablesToLaunch.Add(@"C:\WINDOWS\system32\control.exe", DateTime.Now.AddSeconds(5));
        ExecutablesToLaunch.Add(@"C:\WINDOWS\system32\calc.exe", DateTime.Now.AddSeconds(10));

        WaitAllToLaunch:
        if (ExecutablesToLaunch.Count == ExecutablesLaunched.Count)
            return;
        Thread.Sleep(100);
        foreach (var Executable in ExecutablesToLaunch)
        {
            if (ExecutablesLaunched.Contains(Executable.Key))
                continue;
            if (DateTime.Now >= Executable.Value)
            {
                Process.Start(Executable.Key);
                ExecutablesLaunched.Add(Executable.Key);
            }
            else
                goto WaitAllToLaunch;
        }
    }

}
0
qo'shib qo'ydi

Agar siz bir necha dasturlarni turli vaqtlarda ishga tushirishni va ularni ishga tushirishni kutmoqchi bo'lsangiz:

using System;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
public class Program
{
    static Dictionary ExecutablesToLaunch = new Dictionary();
    static List ExecutablesLaunched = new List();
    static void Main(string[] args)
    {
        ExecutablesToLaunch.Add(@"C:\WINDOWS\system32\notepad.exe", DateTime.Now.AddSeconds(3));
        ExecutablesToLaunch.Add(@"C:\WINDOWS\system32\control.exe", DateTime.Now.AddSeconds(5));
        ExecutablesToLaunch.Add(@"C:\WINDOWS\system32\calc.exe", DateTime.Now.AddSeconds(10));

        WaitAllToLaunch:
        if (ExecutablesToLaunch.Count == ExecutablesLaunched.Count)
            return;
        Thread.Sleep(100);
        foreach (var Executable in ExecutablesToLaunch)
        {
            if (ExecutablesLaunched.Contains(Executable.Key))
                continue;
            if (DateTime.Now >= Executable.Value)
            {
                Process.Start(Executable.Key);
                ExecutablesLaunched.Add(Executable.Key);
            }
            else
                goto WaitAllToLaunch;
        }
    }

}
0
qo'shib qo'ydi