|
Dear EnkelIk,
i have the same broblem with dieing the timers so you should use the timer of threading try
System.Threading.Timer
this class is beter that the standered one you can create it by using call back function it is good one so try it
Mhmoud Rawas
------------
Software Eng.
|
|
|
|
|
Thanks.
Using System.Threading.Timer plus improved error handling (try-catch) my service seems much more stable than before.
Again, THANKS!
/EnkelIk
|
|
|
|
|
Several things: if you read the documentation for Start and Stop , they are exactly the same as passing true and false to the Timer.Enabled property, respectively. Don't do both because it's a waste of CPU time.
Second, it is recommended that you don't use GC.Collect . Instead, dispose of the objects in your "[Write to database]" block that implement IDisposable .
Finally: exception handling and stopping / starting timers. What's probably happening is that since you're stopping the timer, writing to the event log, and then performing database functions - which can be volatile - and then starting the timer again, if an exception were to be thrown your time would not be started again! Instead, put your database calls in a try-catch and gracefully catch all exceptions, or add this.vscTimer.Start() to a finally block of a try-catch-finally or try-finally (exceptions are still thrown in the latter block, but finally is always executed regardless of success.
You should also enable auto-logging on the Windows Service by setting ServiceBase.AutoLog to true (of course, your service inherits ServiceBase , so you can set this property on yourself, too).
One more option: don't stop the timer! If you don't stop it, even if the database code throws an exception, the timer will keep running. Besides, lets say all the database code takes 2 seconds and you run this code every 10 minutes. That means that you're shifting time about 4.8 minutes per day! Just let it run, man!
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath,
I haven't seen the code that causes this problme so I may not be making any sense here. Feel free to use your vast reserves of knowledge to correct me.
The .NET timer class uses the .NET built-in thread pool to execute the timer callback function. The .NET built-in thread pool has a maximum of 25 threads as the default limit. If the timer callback function got stuck (with some database operations, for example) for longer than the timer interval (5 minutes in this case), the timer callback function will be invoked by a new thread from the thread pool. If this new thread got stuck again, then another new thread from the thread pool will be used.
Anyway, under some conditions, the timer can exaust all 25 threads from the thread pool. I have posted a simple example in a message after my article Creating Your Own Thread Pool In .NET[^] to demonstrate this.
If there is no easy way to figure out what caused the timer callback to stuck, then the solution may be not using the .NET timer class at all or restarting the applcation when too many threads are being used to handle the timer event.
My articles and software tools
|
|
|
|
|
Thanks, but I don't think that was his problem. His Elapsed handler stopped the timer, ran some database code, then started the timer again after the database code completed. The database code was not wrapped in a try-catch block and if any exceptions where thrown, the timer would not be restarted.
Or am I missing your point?
BTW, I've read your article and found it very interesting. Good job!
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
Thanks, but I don't think that was his problem.
You are right again. I wasn't reading the code in his post carefully. Thanks.
My articles and software tools
|
|
|
|
|
Thanks.
Using System.Threading.Timer plus improved error handling (try-catch) my service seems much more stable than before. It's quite true that I have no need to stop and start the timer (even when not using the Threading timer), don't know why I got that idea.
I have also with interest read Xiangyang Liu's article but I don't think that will be any serious and frequent problem in my case.
Again, THANKS!
/EnkelIk
|
|
|
|
|
Hi Friends,
when i call a MSMAPI object from a standalone exe in VB .NET, i am able to send a mail. But when i spawn this exe as a process from a windows service, i get an error message while signing on using the MAPI session SignOn() method. I tried using windows service logon as both local system and using my NT logon too. The error message thrown is
System.Runtime.InteropServices.COMException (0x800A7D1A): Not supported
at MSMAPI.MAPISessionClass.SignOn()
at ProcTryMail.tryMail.Pr_SendMail() in D:\Projects\Ems\Backup_Nov17OutSeq\ProcTryMail\tryMail.vb:line 20
My feeling was that some security access is required.
The code i have used is:
Public Class tryMail
Private objMapiSession As MSMAPI.MAPISession
Private objMapiMsgs As MSMAPI.MAPIMessages
Private i As Boolean
Public Sub Pr_SendMail()
Try
objMapiSession = New MSMAPI.MAPISession()
objMapiMsgs = New MSMAPI.MAPIMessages()
objMapiSession.SignOn()
objMapiMsgs.SessionID = objMapiSession.SessionID
objMapiMsgs.Compose()
objMapiMsgs.RecipDisplayName = "--give some mail id here--"
objMapiMsgs.MsgSubject = "From .NET MAPI"
objMapiMsgs.Send()
objMapiSession.SignOff()
Catch
End Try
End Sub
End class
Will be grateful if anyone of u is able to find out the problem.
Thanks
Alex
|
|
|
|
|
Ack, VB.NET! You did notice this was the C# forum, right? Fortunately for you, VB.NET, C#, and all other language that target the CLR use the same BCL and all assemblies the same way, too (since they're all compiled down to IL)...and some actually understand / remember that!
There doesn't appear to be anything wrong with your code (other than the obvious - it's VB.NET ). I don't know anything about MSMAPI (is this a Microsoft wrapper assembly, or something else you found online), but I do know that with functions like MAPILogon you either have to provide a profile name or specify the MAPI_LOGON_UI flag to have the function prompt the user for a profile to pick. I see nothing about you setting up a profile. This is predicated on different assumptions, though, like that this hasn't already been done under the covers for you already.
About the only thing I can tell you is to read the documentation for MSMAPI and take a look at some of the relevant MAPI functions and interfaces, like MAPILogon . Perhaps more information about that error code will be provided (of course, you may have to search the C/C++ header files for the preproc definition to which that error code is defined).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath, problem here is of accessing my local mail client from a windows service. Guess that would remain the same for any language at all. This code works for me as a stand alone exe. It takes the default profile and password of my client (microsoft outlook). MSMAPI is Microsoft Mail API underlying outlook.
Problem comes when this exe is spawned from a windows service as a separate process. Its on the SignOn() method, that it says method not supported.
|
|
|
|
|
I know what MAPI is, I'm just wondering where you got this component. Is it just a RCW (COM interop assembly)?
In any case, is the "Interact with Desktop" option enabled for the service. Since it executes your MAPI client (Outlook in your case), it must be able to interact with the desktop.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Your service is probably using the default "Local System" account, which does not have access to your e-mail profile. What you have to do is reconfigure your service to run under your own account, or the account that owns the e-mail profile:
1. Open the "adminstrative tools" menu, click the services submenu to open a window displaying the list of all services.
2. Double click your service in the list and then click the "Logon" tab.
3. Enter the correct user name and password, click "OK" to close the window.
4. Restart your service.
My articles and software tools
|
|
|
|
|
Not sure where to post this. Is there a builtin function or easy way to convert upper-unicode characters to their escaped HTML equivalent? I.E. I have a string defined and written out to HTML as:
string testString = "[\u2026]";
writer.Write(testString);
What I would like in the resulting HTML output is "[…]", but what I get is the actual unicode byte. I have tried HtmlEncode and various Text.Encoding but they do not handle this... Is there any builtin functions to handle this, or is a funciton needed to loop through the bytes in the string and escape the characters?
FYI: This is just a simple test, the actual data is from an outside data-source and has these characters in it which I need to escape.
|
|
|
|
|
See the HttpUtility.HtmlEncode method. It's static and can be used outside the scope of ASP.NET.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Actually that was the first thing I tried and it doesn't work:
string testString = "[\u2026]";
HttpUtility.HtmlEncode(testString, writer);
doesn't encode it to be … - it just leaves it as the unicode character (which displays as garbage). It seems like it should work, or there should be a builtin function to do this...
|
|
|
|
|
I have a main form that uses AddOwnedForm to allow these forms to draw on top of the main form.
The problem I am having is with a control that I created by inheriting from System.Windows.Forms.Control.
When I display this control, which belongs to the main form, it will draw properly until it tries to write on top of an area occupied by an owned form.
Is there a way to allow a control to draw on top of anything.
I tried using SetTopLevel but that seems to turn it into a form and puts a forms border around it.
I have seen the build in ComboBox control pop up its listbox over anything so I figure it must be possible.
Does anyone have any ideas?
Thanks,
Jay
|
|
|
|
|
Try overridding the CreateParams property and doing something like so:
protected virtual System.Windows.Forms.CreateParams CreateParams
{
get
{
CreateParams parms = base.CreateParams;
parms.Style |= WS_POPUP;
return parms;
}
} I'm not totally positive this will work, but it's worth a try.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Unfortunately that did not work the way I needed. When you modify a control to have the WS_POPUP style it no longer acts as a child on the form and processing of other controls does not work.
Thanks for the suggestion.
|
|
|
|
|
I'm using NetworkStream to read from a TCPClient.
I'm testing DataAvailable before reading to keep by thread from blocking on read.
The docs say that if the remote end has closed the connection, a call to DataAvailable should throw a SocketException, which I canb catch.
Unfortunatly, the exception isn't getting thrown, so my code keeps spinning on DataAvailable forever, never knowing that the connection has been closed. There don't seem to be any other properties or methods in either TCPClient or NetworkStream that I can use to get the connection state without potentially blocking.
Am I missing something? Is there another way to determine if the connection is active without risking a block?
Thanks.
|
|
|
|
|
Most of the TCP code I've done runs in its own thread, so I never worry about blocking. That said, I'll take a few shots at it .
Have you considered the asynchronous version of Read (BeginRead)? It's an asynchronous call, so it's non-blocking.
I once did a simple test application (non-robust) and wanted a Read to timeout after a certain amount of time (this isn't exactly what you want, but keep reading). In order to do this, I needed to get at the Socket itself (TCPClient.Client). Since this property is protected, I ended up having to create my own class (TCPTimeoutClient or some such). Once you get to the actual Socket, you can do all sorts of extra things (take a look at Socket.Available and Socket.Select for instance). I'm not sure if these Socket versions would behave differently, but it might be worth a shot?
I hope some of my babbling might help a little.
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
Hi C#-people,
I have a ListView, that contains directories and files.
I was wondering how I can popup the context menu (which contains items such as 'open with', 'add to zip', 'Copy', 'Paste', etc...) if I right click on these files/directories.
Can anyone point me in the right direction?
Thanks!
Kind regards
Ludwig
|
|
|
|
|
This is really not as easy as you might hope and - to do so in C# - will require recreating countless interfaces, enum, structs, and consts already defined in various headers for Win32 shell programming.
See my answer to a somewhat similar problem to an early question today: http://www.codeproject.com/script/comments/forums.asp?msg=669971&forumid=1649#xx669971xx[^]
While this isn't exactly what you're looking for, you're now going to have to play the part of the Windows shell and pass an ITEMIDLIST - which is going to be hard enough in an application that knows little about the actual shell - to a default context menu while accessing the registry to query for other context menu handlers for the associated file type. I give several links in that previous post. You should read the documentation at the targets of those links about shell programming. There's a lot involved so you might consider just handling generic operations like copy, cut, and paste (if they represent real files, you can simply use the DragDropFormats.Files clipboard format and put a string[] array of filenames into the DataObject for the drag-n-drop or clipboard operation like cut and copy (using the appropriate DragDropEffects ). That clipboard format is one the shell recognizes and will cut or copy the files appropriately.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi all.
I need to get internet expl. selected text to process in my C# application.
Any clear help will be appreciated.
|
|
|
|
|
Get the IHTMLDocument2 interface from the WebBrowser control (include the Microsoft.mshtml assembly in your project and get it from the WebBrowser.Document property, which may be null), then get the selection property (which may be null). This gets an IHTMLSelectionObject from which you can use the type property to determine the selection type (text, insertion point, control, etc.) and the createRange method to create a suitable object to work with the actual selection (like a TextRange ). See the documentation for IHTMLSelectionObject in the MSDN Library for more detailed information).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Ughhh ugly and messy COM interfaces. Can't wait for Whidbey's managed web browser control.
The graveyards are filled with indispensible men.
|
|
|
|