0

I check a condition on my form_load event whether the form window should be closed or not. If so, a timer with an interval of 3000 will be started and after the tick, the form should be closed. When I check my debugger, even though the condition returns true, the debugger jumps over my timer.Start() method and my timer is never ticked.

I have defined the timer in my class as System.Windows.Forms.Timer timer; and this is how I am initiating it:

timer = new System.Windows.Forms.Timer();
this.timer.Enabled = true;
this.timer.Interval = 3000;
this.timer.Tick += new System.EventHandler(this.timer_Tick_1);

The tick event:

private void timer_Tick_1(object sender, EventArgs e)
{
   this.Close();
}

And the condition is as simple as:

if (closeWindow)
    timer.Start();

And trust me, the closeWindow returns true.

P.s. Strangely enough, this timer used to work. I know I have it messed up somewhere perhaps. But I do not know where.

Update: the form_load event

private void FormMain_Load(object sender, EventArgs e)
{
    metroLabelBuild.Text = args["name"] + " " + args["action"];
    if (CheckArguments(args, 18))
    {
        try
        {
            campaignRecipientsFileLocation = args["recipientsfile"].Trim();
            campaignName = args["name"].Trim();
            campaignDescription = args["description"].Trim();
            campaignAction = args["action"].Trim();
            campaignOutputFormat = args["outputformat"].Trim();
            campaignType = args["type"].Trim().ToLower();
            campaignDelimiter = args["delimeter"].Trim();
            campaignOutputPath = args["outputpath"].Trim();
            campaignFileName = args["filename"].Trim();
            campaignId = Convert.ToInt64(args["id"].Trim());
            campaignFixedCost = Convert.ToInt32(args["fixedcost"]);
            campaignVariableCost = Convert.ToInt32(args["variablecost"]);
            campaignControlGroup = Convert.ToInt32(args["controlgroup"]);
            campaignFtpId = Convert.ToInt64(args["ftpid"].Trim());
            campaignHasSpyList = (args["hasspylist"].ToLower().Equals("true") || args["hasspylist"].Equals("1")) ? true : false;
            closeWindow = (args["closewindow"].ToLower().Equals("true") || args["closewindow"].Equals("1")) ? true : false;
            campaignShouldUploadToFTP = (args["shoulduploadtoftp"].ToLower().Equals("true") || args["shoulduploadtoftp"].Equals("1")) ? true : false;
        }
        catch
        {
            listBoxMessages.Items.Add("Error preparing the arguments.");
            continueProcess = false;
            this.Close();
        }
        finally
        {
            logger = new Logger(loggerEnabled, cs);
            fh = new FileHelper(logger);
            rh = new RecipientHelper(logger);
            dbh = new DatabaseHelper(logger, cs);
            if (continueProcess)
            {
                InsertCampaignRun("Running");
                RunCampaign();
                if (closeWindow)
                    timer.Start();
            }
        }
    }
    else
    {
        if (closeWindow)
            timer.Start();
    }
}
disasterkid
  • 6,948
  • 25
  • 94
  • 179
  • Have you considered - ah - not enabling the timer until it is configured? You do not start it with 3000 interval, you start it with default interval and without event. – TomTom Nov 24 '14 at 08:53
  • Show your form load event. You have shown random snippets, and only the event handler is a complete sample we can work with. – Rufus L Nov 24 '14 at 08:54
  • @RufusL Updated the question with my `form_load`. Hope it helps. – disasterkid Nov 24 '14 at 08:57
  • @TomTom can you explain with a fix to the code? – disasterkid Nov 24 '14 at 08:57
  • @Pedram Yes. Fix your code. Simple. Change the order. Are you able to copy/paste lines? Do not enable an non configured timer. – TomTom Nov 24 '14 at 08:59
  • @TomTom yeah I have hear about copy/paste, lol. Should I put `Enabled` at the end? – disasterkid Nov 24 '14 at 09:00
  • Sorry, what I meant was, show us the relevant parts of the code, not the entire code. Your snippets above are completely out of context...we cannot see the flow. In your Form_Load event, you are calling timer.Start(). Where is the timer initialized? When do you hook up the Tick event? – Rufus L Nov 24 '14 at 09:03
  • @RufusL you are right. My timer is initialized in the constructor and the `timer.Start()` method is called on the `form_load` as seen in above. The timer tick method is located at the bottom of my class. – disasterkid Nov 24 '14 at 09:07
  • My crystal ball says that you are running this code on the 64-bit version of Windows 7. And you see a "first chance exception" notification in the Output window. [More here](http://stackoverflow.com/a/25135685/17034). – Hans Passant Nov 24 '14 at 10:04
  • @HansPassant your crystal ball is right. But now I have replaced the timer with a new Task that uses `Thread.Sleep()` instead and now it – disasterkid Nov 24 '14 at 12:40
  • @HansPassant but it would still help if I knew the reason why it occurs. – disasterkid Nov 24 '14 at 12:42
  • try calling the `timer.Start()` from the constructor just to check if it works, if it does then nothing wrong with the timer code. – Rohit Nov 27 '14 at 04:02

0 Answers0