Re: timer and threading help needed
- From: "Beemer Biker" <jstateson@xxxxxxxx>
- Date: Mon, 8 Jan 2007 08:39:45 -0600
"Rick Strahl [MVP]" <rickstrahl@xxxxxxxxxxx> wrote in message
news:6964F92E-0149-4726-83E5-11207FB233D1@xxxxxxxxxxxxxxxx
Hi Beemer,
This is a bit more tricky to do right in ASP.NET because there's no
concept of a stateful form. In Windows you had a form that was up and
running and you could tie your timer to that easily, but in ASP.NET pages
load and release immediately so there's no persistent page per say.
That said you can certainly create background threads though and it sounds
like that's what you'll want to do. The key question is what do you use to
fire off the background threads. You can use System.Threading.Timer to
fire events at regular intervals - but make sure you set up the timer
somewhere as a persistent instance (say on the HttpApplication object or
as a static property on some object).
I did something similar to this here:
http://west-wind.com/WebLog/posts/2028.aspx
which might give you an idea (make sure you read the comments for the
solution to the problem of the timer going away <s> - you have to hold on
to the timer reference).
+++ Rick ---
Thanks Rick! I looked at your example just now, but have already decided
that what I really need is a service running separately from the asp page.
My original C# app tuned a receiver over an RS232 port and the receiver
audio went into the line input of the XP system. I had taken that
SoundCapture example from the DX90SDK and added the capability of receiving
commands over a socket from a client and routing the audio notification
buffer out over another socket (UDP) back to the client. I have second C#
app that can talk to the server and receive data+audio. That is the one I
need to make into an asp page rather than my original program that
controlled the receiver.
FWIW, I have been unable to run my C# app on a win2003 server. I see a
general "socket error" show up in the event viewer. There is no firewall
and I have admin privileges so I am unsure why my app wont run on a win2003
server but runs fine on any number of XP systems. The server has the latest
dotnet 2.0 and all updates. It does not have VS2005 so I may install VS2005
and try running it in debug mode to see why it wont start up. Googleing
that error message shows complaints that dotnet 2 terminates threads that do
not have try-catch and one solution is going back to 1.1. I dont think that
applies in my case since my app works fine with dotnet 2 on xp system but
not on win2003. Maybe that is the problem you mentioned in your URL above?
If an exception has no try-catch a thread will be terminated in dotnet2 but
not dotnet1.
Rick Strahl
West Wind Technologies
www.west-wind.com/weblog
"Beemer Biker" <jstateson@xxxxxxxx> wrote in message
news:12pll605j0gju43@xxxxxxxxxxxxxxxxxxxxx
I wrote a "windows form" C# program that I am attempting to convert to
web. The app has a timer (from the vs2005 C# toolbox) and a couple of
threads "system.threading" type. One handles incoming UDP socket messages
the other receives data from a serial port and a 3rd is responsible for
sending commands to my serial device.. The timer callback has no problem
writing information to a control. I had to add a delegation method to the
threads so they could update a text box as the threads could not access
the text box directly unlike the timer.
OK, I started up a C# web project and found right away there is no
timer. I googled around and found a timer class "ScheduleTimer" that
actually works. http://www.dotnet247.com/247reference/msgs/18/92868.aspx
It also uses that delegation mechanism. I looked at the web designer
toolbox and I found a "Background Worker". So what is that? Should I be
using a "Background Worker" to handle intput from a socket instead of
using system.threading threads?
I dont want the client computer to run any of this code. All the threads
need to be executing on the server. For testing, it would be nice if the
thread would could print some info on one of the control text boxes.
Maybe I cant do that? I assume a thread running on the server can update
the page the user is looking at since ad's are updated the same way.
In the form_load i create my threads. Even when I tested to make sure it
was not started from a postback it seems the code that creates the
threads gets executed more than once when the page is brought up.
Can someone suggest some resources I can read up on to learn more?
Anyone using that "Background Worker" for anything?
At that same component designer I also see "SerialPort" and after
installing one on my form I see an event callback for data received. So
can I use that SerialPort component and do away with my C# thread that
receives serial data using a "read" command until it sees a carrage
return? I looked thru the toobox and didnt see a "socket". It would be
nice if I could drop a "socket" on my form and have received event
callbacks. Can life be that simple?
..thanks for any suggestions..
--
=======================================================================
Joseph "Beemer Biker" Stateson
http://ResearchRiders.org Ask about my 99'R1100RT
=======================================================================
.
- References:
- C#: timer and threading help needed
- From: Beemer Biker
- Re: timer and threading help needed
- From: Rick Strahl [MVP]
- C#: timer and threading help needed
- Prev by Date: RE: modify validator script to fire only if visible?
- Next by Date: Events firing without user interaction
- Previous by thread: Re: timer and threading help needed
- Next by thread: RE: What is syntax for Eval(User.IsInRole) to return True/False To
- Index(es):
Relevant Pages
|