Domanda

I having problem with my timer, it doesn't reach its tick and I don't see the problem.

class Class1
{
    bool check;
    Timer net;
    private bool timerComplete = false;
    private int timerIndex = 0;

    public Class1()
    {
        check = true;
        net = new Timer();
    }
    public void Do()
    {
        System.Threading.Thread.Sleep(4*1*1000);
        net.Interval = 1 * 1000;
        net.Tick+=new EventHandler(net_Tick);
        net.Start();
        while (!timerComplete)
            System.Threading.Thread.Sleep(1000);

    }

    void net_Tick(object sender, EventArgs e)
    {
        timerIndex++;
        if (timerIndex >= 10)
        {
            timerComplete = true;
            return;
        }
       //my code
    }
}

Main

This is where I call the Do() function

class Program
{
    const int SW_HIDE = 0;

    static void Main(string[] args)
    {
        Hide();
        Class1 c = new Class1();
        c.Do();
    }

    public static void Hide()
    {
        var handle = GetConsoleWindow();
        // Hide
        ShowWindow(handle, SW_HIDE);
    }

    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
È stato utile?

Soluzione 2

Your code is a bit confusing but basically you are looking for the following

static void Main(string[] args) {
    var myObject = new Class1();
    myObject.Do();

    // give the program 10 minutes to run. This is arbitrary so you will want to change
    //     to something realistic or have a callback to signal "done".
    System.Threading.Thread.Sleep(10 * 60 * 1000);
}

That should do it or at least get you started!

Other Note - One other note is the second timer calls a method named net_Tick. I don't see this in the code presented so I assume there are no issues with that...

Alternative Approach

One other approach instead of making the main thread pause is to block the Do method until the timer is done (or you want to stop). To do this I have added just the pieces of code that need to change in Class1 below:

public class Class1 {

    // add a variable for timer complete
    private bool timerComplete = false;
    private int timerIndex = 0;


    public void Do()
    {
        t.Enabled = true;

        // start timer same as normal:
        t.Interval = 10 * 1000;
        t.Tick += new EventHandler(t_Tick);
        t.Start();

        // busy wait to block the thread
        while (!timerComplete) {
           System.Threading.Thread.Sleep(1000);
        }
    }

    void t_Tick(object sender, EventArgs e)
    {
        // give this a stopping point
        timerIndex++;
        if (timerIndex >= 10) {
            timerComplete = true;
            return;
        }

        // leave the rest of the tick as it was before...
    }
}

Once you do this you can remove the call to Sleep from the Main routine and everything should still work. Best of luck!

Altri suggerimenti

You forgot to call Do(), which is the method that sets the timer's properties and starts it.

EDIT:

If it's your second timer the one that doesn't work (you still don't specify which timer is not working), it's because you are creating it and not saving any reference to it, therefore you lose control over it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top