Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a stock dataFeed that has an event that fires approx 4 or 5 times per sec sending the stock data via a event arg. I parse the event arg and insert the results into a database via a database object that wrote. All of this activity seems to overwhelm the windows form and consumes most of system resources. I'd like to put the stock dataFeed event on a thread but I don't know how. I've tried creating a ThreadStart object and using an anonymous delegate to pass the event to but I dont know how to instantiate the event arg or what object to pass...

example:
C#
// dataFeed event
private void stockDataFeed(object sender, stockDataFeedArg arg)
{
   myDatabaseOject.Insert(arg.stockPrice);
}

private void Button_Click(object sender, EventArgs e)
{
   ThreadStart tS = delegate() {stockDataFeed(?,?);};
   Thread t = new Thread(tS);
   t.Start();
}


Can some one please explain/show me what I'm doing wrong and what's the correct way to go about this.

Thanks!

-Donald
Posted
Updated 26-Jan-11 15:44pm
v3

You don't create a new thread on every event. Creating a thread is a very expensive process.

Instead, I'd probably look into adding the data to a queue in your event handler. A second thread, that you start at the beginning of your code, NOT in the event handler, watches this queue for items to process and add to the database. When it's done processing items, it falls asleep for x number of seconds before waking up and checking the queue again.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 22:04pm    
Totally agree - my 5. I covered this issue in my answers to OP's previous question. I don't know why my advice was ignored. I also provided complete samples for inter-thread invocation. As you can see, I even posted a Tip as a permanent reference, as many Questions could be resolved using this technique.

Thank you.
--SA
d.allen101 26-Jan-11 22:18pm    
ok, so you're saying have a queue object inside the dataFeed event thats enqueuing the feed args and the queue is being "watched" by a thread which is dequeuing the results into my database object and it's (the thread watching the queue) sleeping for a second or so after each dequeue?
d.allen101 26-Jan-11 22:20pm    
hey SAKryukov, I'm not ignoring your advice, I'm just having trouble grasping this...I'm a novice dev and no experience with using delegates and threads.
Sergey Alexandrovich Kryukov 26-Jan-11 23:52pm    
Sorry Donald, I understand. The reason I felt you ignored was that you did not ask about inter-thread invocation in the comment to my second answer to your previous Question but created a new Question. In fact, I expected your feedback and checked that page from time to time. My guess was that you overlooked my words about that techniques. I did not see a big problem in it -- it happens to me all the time and later I fix myself. Same thing with you. Please don't get it personally or as a blame. You can see that I readily continued to answer to your problems -- on this new page. You know that I replied to your follow-up questions in detail.
Also, don't hesitate to ask if you have further questions. I can imagine how difficult is to grasp all that matter if you did not collect a lot of experience on more simple problems.
Best,
--SA
d.allen101 26-Jan-11 23:58pm    
NOOO! it's not that at all SAKryukov, you helped me out last time! it's just that I'm not familiar with threads and delegates...you have to be a little patient with me until I get up to speed. you've responded to EVERY single thread I've posted about multi-threading and you've helped me out alot but I need a little more explanation and simple examples...
Donald, it might surprise you, but the precise answer to this question is contained in my second answer to you previous question.

All you need it to read the last code sample from my Article from Tips/Tricks section:

Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

See also Alternative Tip to my article and related Microsoft sample (v.4.0 only, while I provide a solution good for all Framework versions 2.0 and later).
This last sample shows how to organize inter-thread invocation. You can use this code with very minimal changes: just instantiate generic with your event argument type.

Further questions are welcome.

Good luck!
—SA
 
Share this answer
 
v3
Comments
d.allen101 26-Jan-11 22:21pm    
Ok thanks, i'm about to read it RIGHT now!
Espen Harlinn 27-Jan-11 14:27pm    
5+ Good effort, it's turning into a series :)
Sergey Alexandrovich Kryukov 27-Jan-11 23:39pm    
Thank you, Espen,
I recently found yet another publication, very promising to turn into series as well, because it is even more universal :-)
Please see: "http://radio-weblogs.com/0001263/junk/Q209354 - HOWTO.html".
I already used it in comments and -- in particular -- in one of my today's answers.

Thank you,
--SA
Espen Harlinn 28-Jan-11 14:08pm    
Good one :) - and a few more:
RTBM ("read the bloody manual")
RTFA ("read the f***ing article")
RTFE ("read the f***ing e-mail")
RTFC ("read the f***ing code" [also "reboot the f***ing computer"])
RTFSC ("read the f***ing source code")
RTFQ ("read the f***ing question")
RTFFAQ ("read the f***ing frequently asked questions")
UTFH ("use the f***ing help")
UTSL ("use the source, Luke")
JFWI ("just f***ing Wiki it")
JGIYN ("just Google it, you noob")
STFG ("search the f***ing Google")
STFW ("search the f***ing Web")
WIDGI ("when in doubt, Google it")

Might come handy when answering questions :)
Sergey Alexandrovich Kryukov 28-Jan-11 14:18pm    
Yes, a good list, by the way.
You don' really need those ***stars***:
The canonical way of saying is this:
"RTFM -- Read The Following ;-) Manual".

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900