Jump to content

event is triggered from one method/function but not another

- - - - -

  • Please log in to reply
7 replies to this topic

#1
turc1656

turc1656

    Newbie

  • Members
  • Pip
  • 5 posts
Hi everyone. I am new to C# and not a professional programmer. I am having what is probably a simple problem to fix. I am trying to grab quotes from my brokerage for the purposes of trading and I am using VS2008 and this is a C# project.

The quotes grab just fine when I click a button that executes the associated code. The request for data is fine and the event is triggered that means I have just received data and that code then executes (parses the returned data, etc). However, when I built a timer to do this automatically it won't work for some reason. The timer functions perfectly fine and I stepped through debugging it and all code gets executed as I expect. It even does the lines right above, INCLUDING, and below the code to grab a quote. The problem is that it looks like the event is not triggered for some reason.

I even tried putting the code that does the actual request into a separate method/function to see if that solves it (or breaks it for both types of requests, manual and automatic). And yet it STILL only works for the button press and not for the timer. The event is still ignored (or doesn't occur) for the timer.

the method that is called is this:

private void GrabQuote()
{
mySym=currency_input.Text;
myMinBar=myHist.CreateHistMinBar();
myMinBar.Clear();
DateTime oDate=new DateTime(0);
myMinBar.SendRequest(mySym,2,3600,oDate,oDate,250,false);
}

and the ONLY code that exists in the button click method is:
private void button1_Click(object sender, EventArgs e)
{
GrabQuote();
}

and that somehow works fine but the same call to GrabQuote() does not work when called from the other method.

This is driving me nuts. Does anyone have an idea on how to fix this or at least what is wrong?



Thanks,

-Turc

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Where is your timer code? That's the code that isn't working, correct? So post that.

#3
turc1656

turc1656

    Newbie

  • Members
  • Pip
  • 5 posts

Momerath said:

Where is your timer code? That's the code that isn't working, correct? So post that.

well actually the timer appears to work fine. it counts down on a per second basis and updates the visual counter. when it reaches zero, the appropriate code is executed, and then the timer is reset.

the problem, from what i can tell, is that when the request for data is made the event is not raised that says i have received data - from which other code is executed - this is despite the fact that the exact same request command is given.

here is the code pieces that deal with the timer...

in my GlobalVars namespace i have the following class and declarations:

public class Timer

    {

        public static System.Timers.Timer myTimer = new System.Timers.Timer();

        public static int Previously_Defined;

        public static int minutes;

        public static int seconds;

        public static int UpdateInterval;

    }

then when i click the Start button to begin the timer this chunk of code is executed in relation to the timer:

if (GlobalVars.Timer.Previously_Defined != 1)

            {

                GlobalVars.Timer.myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);

                GlobalVars.Timer.Previously_Defined = 1;

            }


int update_interval;


            try

            {

                update_interval = Convert.ToInt32(update_interval_textbox.Text);

                GlobalVars.Timer.UpdateInterval = update_interval;

                RAdd("Using interval of "+update_interval.ToString()+" minutes.");

            }

            catch (FormatException)

            {

                RAdd("Unable to convert update interval to an integer.");

                stop_button.Enabled = false;

                start_button.Enabled = true;

                return;

            }

            catch (OverflowException)

            {

                RAdd("Update interval is outside the range of an integer.");

                stop_button.Enabled = false;

                start_button.Enabled = true;

                return;

            }


            if (update_interval >= 1 & update_interval <= 60)

            {

                GlobalVars.Timer.myTimer.Interval = 1000;

                GlobalVars.Timer.minutes = update_interval;

                GlobalVars.Timer.seconds = 0;

                string interval = "Next update in " + GlobalVars.Timer.minutes.ToString() + " minutes 0 seconds.";

                SetUpdateIntervalText(interval);

                GlobalVars.Timer.myTimer.Start();

                RAdd("STARTED.");

            }

            else

            {

                RAdd("Update interval must be between 1 and 60 minutes.");

                stop_button.Enabled = false;

                start_button.Enabled = true;

                return;

            }

here is the code for the DisplayTimeEvent event handler that gets executed every 1 second:

public void DisplayTimeEvent(object source, ElapsedEventArgs e)

        {


            if (GlobalVars.Timer.minutes == 0 & GlobalVars.Timer.seconds == 0)

            {

                //Timer has expired and quote for pending instrument needs to be updated.

                RAdd("Timer has expired and quote for pending instrument needs to be updated.");

                GlobalVars.Timer.myTimer.Stop();


                //Place code for grabbing the quotes and writing to file here.

                GrabQuote();


                //Resume counter for next currency update.

                GlobalVars.Timer.minutes = GlobalVars.Timer.UpdateInterval;

                GlobalVars.Timer.seconds = 0;

                string interval = "Next update in " + GlobalVars.Timer.minutes.ToString() + " minutes 0 seconds.";

                SetUpdateIntervalText(interval);

                GlobalVars.Timer.myTimer.Start();

            }

            else

            {

                if(GlobalVars.Timer.seconds==0)

                {

                    GlobalVars.Timer.minutes = GlobalVars.Timer.minutes - 1;

                    GlobalVars.Timer.seconds = 59;

                    string interval = "Next update in " + GlobalVars.Timer.minutes.ToString() + " minutes " + GlobalVars.Timer.seconds.ToString() + " seconds.";

                    SetUpdateIntervalText(interval);

                }

                else

                {

                    GlobalVars.Timer.seconds = GlobalVars.Timer.seconds - 1;

                    string interval =  "Next update in " + GlobalVars.Timer.minutes.ToString() + " minutes " + GlobalVars.Timer.seconds.ToString() + " seconds.";

                    SetUpdateIntervalText(interval);

                }

            }

        }

i think that about covers all the relevant code.

Edited by turc1656, 24 November 2010 - 11:12 AM.


#4
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
turc if you put your code in side code tags like so:

[CODE*]Code here.[*/CODE]

It will make it easy for everyone to read. :)
(just take out the "*" in the tags above, or click the "#" button at the top of your message to get the tags to work)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#5
turc1656

turc1656

    Newbie

  • Members
  • Pip
  • 5 posts
thanks for the heads up, gamemaker. i have edited the post accordingly.

#6
analysis_junky

analysis_junky

    Newbie

  • Members
  • Pip
  • 9 posts
It has been a little bit since I have worked with Windows timers, I now use either Windows Services or Scheduled Tasks.
If I were you, I would create a new project with only the necessary components (take out the global class, etc), with just a (non static) timer that starts on load. Essentially, you will be making it simpler to debug.

#7
turc1656

turc1656

    Newbie

  • Members
  • Pip
  • 5 posts
that's a good idea. i think i will attempt that this evening.

#8
turc1656

turc1656

    Newbie

  • Members
  • Pip
  • 5 posts
i built a barebones version as suggested and got rid of the globalvars class so everything is kept inside of the one main class. unfortunately that did not solve the problem. in this barebones version there is no connection to the brokerage so the OnError event should be raised...which it is when when the user clicks the button that equates to a manual data request. when the timer makes the request the OnError event is not raised.

i can upload this simplified version of the software (the VS2008 project directory as a zip) if you would like along with the 2 DLL's from the brokerage (free to distribute with their software) that i added as references if anyone wants to take a quick look for themselves.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users