473,473 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Questions about delegate and event handler

Hi,

I have a program that gets the prices of stocks in real-time. The
program consists of the following key components:
// Constructor
public GetRealTimeStockPrice()
{
// Step 1
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
portfolio.OpenStocks(new
ListDataReadyDelegate(RegisterExistingStocks));

}
// Step 2
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{

portfolio.OpenStocks(new
ListDataReadyDelegate(OnStocksDownloaded));
}
}

void RegisterExistingStocks(IWatchList portfolio)
{
PriceForStock stockPrice;
string CurrentTicker;

foreach (IWatchEntry stock in portfolio.Stocks)
{
CurrentTicker = stock.Ticker;

if (!UniqueTickerRegistered(CurrentTicker))
{
stockPrice = new PriceForEntry(CurrentTicker);
mPriceCheckList.Add(stockPrice);
}
}
}

void OnStocksDownloaded(IWatchList portfolio)
{
string ticker;

foreach (IWatchEntry stock in portfolio.Stocks)
{
ticker = stock.Ticker;

if (!PriceObtained(ticker))
{
stock.SymbolData.Trade += new
TradeDelegate(SymbolData_Trade);
}
else // un-subscribe to the event once it's done
{
stock.SymbolData.Trade -= new
TradeDelegate(SymbolData_Trade);
}
}
}
Question 1:

I want to make sure that Step 1 happens before Step 2 in
"GetRealTimeStockPrice". Can anyone tell me if that is the case? (I
understand that with delegates, when it actually gets executed is out
of control.)
Question 2:

I was advised to use the structure below to get all of the existing
stocks in the porfolios:

foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
portfolio.OpenStocks(new
ListDataReadyDelegate(......));

}

Alternatively, I could use the following approach instead:

foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{

foreach (IWatchEntry stock in portfolio.Stocks)
{
// blah blah blah

}

}

However, I was told by using "OpenStocks" with a delegate as its
parameter, it guarantees to get all of the stocks while the other
approach of looping through each stock may not work if the data for
the stock(s) are not immediately available.

Can anyone explain why using the delegate guarantees that the data are
available?
Question 3:

Is the way I un-subscribe to the "SymbolData_Trade" event correct?
if (!PriceObtained(ticker))
{
stock.SymbolData.Trade += new
TradeDelegate(SymbolData_Trade);
}
else // un-subscribe to the event once it's done
{
stock.SymbolData.Trade -= new
TradeDelegate(SymbolData_Trade);
}

I want to make sure that this removes the stock for which
"PriceObtained(ticker) == true" from the subscription. In other words,
once the price of a stock is obtained, it should be removed from the
watch list so that it won't react to the "SymbolData_Trade" event
anymore.

Would appreciate knowledgable people's advice!
Jun 27 '08 #1
3 1045
On 21 apr, 03:44, Curious <fir5tsi...@yahoo.comwrote:
Hi,

I have a program that gets the prices of stocks in real-time. The
program consists of the following key components:

* * * * // Constructor
* * * * public GetRealTimeStockPrice()
* * * * {
* * * * * * // Step 1
* * * * * * foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
* * * * * * {
* * * * * * * * portfolio.OpenStocks(new
ListDataReadyDelegate(RegisterExistingStocks));

* * * * * * }

* * * * * * // Step 2
* * * * * * foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
* * * * * * {

* * * * * * * * portfolio.OpenStocks(new
ListDataReadyDelegate(OnStocksDownloaded));
* * * * * * }
* * * * }

* * * * void RegisterExistingStocks(IWatchList portfolio)
* * * * {
* * * * * * PriceForStock stockPrice;
* * * * * * string CurrentTicker;

* * * * * * foreach (IWatchEntry stock in portfolio.Stocks)
* * * * * * {
* * * * * * * * CurrentTicker = stock.Ticker;

* * * * * * * * if (!UniqueTickerRegistered(CurrentTicker))
* * * * * * * * {
* * * * * * * * * * stockPrice = new PriceForEntry(CurrentTicker);
* * * * * * * * * * mPriceCheckList.Add(stockPrice);
* * * * * * * * }
* * * * * * }
* * * * }

* * * *void OnStocksDownloaded(IWatchList portfolio)
* * * * {
* * * * * * string ticker;

* * * * * * foreach (IWatchEntry stock in portfolio.Stocks)
* * * * * * {
* * * * * * * * ticker = stock.Ticker;

* * * * * * * * if (!PriceObtained(ticker))
* * * * * * * * {
* * * * * * * * * * stock.SymbolData.Trade += new
TradeDelegate(SymbolData_Trade);
* * * * * * * * }
* * * * * * * * else // un-subscribe to the event once it's done
* * * * * * * * {
* * * * * * * * * * stock.SymbolData.Trade -= new
TradeDelegate(SymbolData_Trade);
* * * * * * * * }
* * * * * * }
* * * * }

Question 1:

I want to make sure that Step 1 happens before Step 2 in
"GetRealTimeStockPrice". Can anyone tell me if that is the case? (I
understand that with delegates, when it actually gets executed is out
of control.)
You can't. I don't know how the code is written of your custom
classes, but with the information given here, I can't tell. You have
to make sure in your custom class that it doesn't invoke the second
delegate before the first.
>
Question 2:

I was advised to use the structure below to get all of the existing
stocks in the porfolios:

* * * * * * foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
* * * * * * {
* * * * * * * * portfolio.OpenStocks(new
ListDataReadyDelegate(......));

* * * * * * }

Alternatively, I could use the following approach instead:

* * * * * * foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
* * * * * * {

* * * * * * * * *foreach (IWatchEntry stock in portfolio..Stocks)
* * * * * * * * *{
* * * * * * * * * * *// blah blah blah

* * * * * * * * *}

* * * * * * }

However, I was told by using "OpenStocks" with a delegate as its
parameter, it guarantees to get all of the stocks while the other
approach of looping through each stock may not work if the data for
the stock(s) are not immediately available.

Can anyone explain why using the delegate guarantees that the data are
available?
This is probably because in your custom class, the delegate is only
invoked when the data is available. Thus, when using the second you
immediatley invoke your method. Using the first you tell the portfolio
to invoke your method whenever the data is there.
Question 3:

Is the way I un-subscribe to the "SymbolData_Trade" event correct?

* * * * * * * * if (!PriceObtained(ticker))
* * * * * * * * {
* * * * * * * * * * stock.SymbolData.Trade += new
TradeDelegate(SymbolData_Trade);
* * * * * * * * }
* * * * * * * * else // un-subscribe to the event once it's done
* * * * * * * * {
* * * * * * * * * * stock.SymbolData.Trade -= new
TradeDelegate(SymbolData_Trade);
* * * * * * * * }

I want to make sure that this removes the stock for which
"PriceObtained(ticker) == true" from the subscription. In other words,
once the price of a stock is obtained, it should be removed from the
watch list so that it won't react to the "SymbolData_Trade" event
anymore.

Would appreciate knowledgable people's advice!
stock.SymbolData.Trade = null.

Jun 27 '08 #2
Thanks for the input!
Jun 27 '08 #3
Hi NattyDreadLock,

You're obviously knowledgable about delegates. May I get your input
about a threading issue?

I user System.Threading.Timer to check something every 15 seconds:

void OnTrade ()
{
// Check after the trade market first opens
if (!mIsTradeOpen)
{
mTimer = new Timer(new
TimerCallback(CheckHistoricalData), null, 15000, 15000);
mIsTradeOpen = true;

}
}

I have to use timer in the event handler "OnTrade" because the timer
only starts after there's a trading activity. However, it should not
start again when there's another trading activity - That's why I use a
global variable, "mIsTradeOpen", to make sure that the timer only
starts once.

Question 1: How can I find out how many instances of my timer is
running in my Task Manager? I want to make sure that there's only one
timer running.

Question 2: When shall I call mTimer.Dispose() to kill the timer? I'll
need to kill the timer after 5 PM when the trade market closes. But I
don't know where in my code I should kill the timer. Difficult
situation is that I start the timer in an event handler which is
initiated from the constructor.

Any input?

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: 0to60 | last post by:
More importantly, WHY do I wanna do this? Well, I have events that I raise asynchronously like this: foreach(eventhandler handler in eventname.getinvocationlist()) handler.begininvoke(); ...
4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
by: Kerry Jenkins | last post by:
I am having problems passing an Event Delegate as an argument to a method that accepts a delegate argument. I get the following error message: 'Public Event ProgressChanged(sender As Object, e...
5
by: Mats Larsson | last post by:
Hi, I have an example in C# for testing if a handler has been assigned to the delegate, I would like to do the same in VB.NET but I don't understand how. The C# code is copied from the Northwind...
1
by: Greg Wilkerson | last post by:
I hope I can explain this in a way that can be understood. I have a custom control, I'll call it navigator, used to navigate "things". I has, among other controls, two standard buttons, previous...
5
by: Doug Handler | last post by:
Hi, I have a form (Form1) that contains a tab control which one tab has a customer user control (UserControl1). When the user double-clicks on the grid hosted there a new user control is...
6
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
7
by: sam.m.gardiner | last post by:
I'm working with VB.NET events and I want a way to disconnect all the handlers of an event. I want to do this in the object that is the source of the event. This is slightly tricky in VB.Net as the...
3
by: lothar.behrens | last post by:
Hi, I am thinking about the delegate mechanism and try to understand it. I am coming from C++ and know about callbacks or member callbacks. In C++ I have this typedef for every class that...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.