|
It's impossible to advise you which is the one you should use. Take a look at the documentation for each control and decide which one best fits your requirements.
|
|
|
|
|
Please forgive my desperate plea but I am trying to find out why the following code only half works.
My code
namespace WindowsService1
{
public partial class ServiceName : ServiceBase
{
public ServiceName()
{
InitializeComponent();
string sSource;
string sLog;
string sEvent;
sSource = "MyServiceName";
sLog = "Application";
sEvent = "MyServiceName starting";
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent);
EventLog sysLog = new EventLog("System", ".");
sysLog.EntryWritten += new EntryWrittenEventHandler(OnEntryWritten);
EventLog.WriteEntry(sSource, "logger started");
Console.WriteLine("Logger started");
}
public void OnEntryWritten(object source, EntryWrittenEventArgs e)
{
string sSource;
string sLog;
sSource = "MyServiceName";
sLog = "Application";
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, "sqllogger started");
SqlConnection Conn = new SqlConnection("User id = x; password = y; server=z;database=dbname;connection timeout=30");
SqlCommand ins = new SqlCommand("INSERT INTO dbo.TableName(TimeWritten, Source)VALUES (@TimeWritten, @Source)", Conn);
ins.Parameters.Add("@TimeWritten", SqlDbType.DateTime);
ins.Parameters.Add("@Source", SqlDbType.NVarChar, 20);
ins.Parameters["@TimeWritten"].Value = e.Entry.TimeWritten;
ins.Parameters["@Source"].Value = e.Entry.Source;
TableName.Open();
ins.ExecuteNonQuery();
TableName.Close();
}
The account the service runs under is a local admin on the Windows XP host.
Please could one of you Gurus tell me what I am doing wrong?
|
|
|
|
|
why don't you have proper exception handling in your code, showing the occasional exception in all its glory, i.e. by calling its ToString() method?
why are you just ignoring return values of those methods that are eager to tell you how bad or well they did, such as SqlCommand.InsertExecuteNonQuery()?
why should we have a guess when you probably have all the answers within reach?
PS: there are ways to more easily debug a service, not making the code into a service until it all works obviously is one of them.
[EDIT] Method name fixed [/EDIT]
modified 21-Oct-11 7:56am.
|
|
|
|
|
Hi Luc,
I do have exception handling. Every segment has a "try" preceeding it and a "catch > ToString" and the end of it. I didn't include it in the code I posted because it wasn't relevant. No errors are reported during the coding phase, the building or the service installation. The code "should" work despite the "try/catch" phrasing.
Is there a difference between the "SqlCommand ins" in my code and the SqlCommand.Insert()you mention? If so could you tell me where to find it as I have to come across an example of a working difference?
If I had the answers this conversation wouldn't be taking place
PS. The code works perfectly in a console application. I am looking for help in understanding why it doesn't work as a service.
Thank you for your time though.
|
|
|
|
|
CCodeNewbie wrote: I didn't include it in the code I posted because it wasn't relevant.
Please don't show modified code snippets when the code does not behave as you expect; show the most relevant methods unmodofied, and wgen that is not possible and you must change them (way too long, or NDA-like problems), tell us. When you don't understand why it does what it does, you're probably not in a position to make correct judgement calls either.
Having said that, Bob's remark on static vs instance seems very much to the point.
|
|
|
|
|
You're writing via static methods on EventLog, but you're attaching the event handler to the new instance syslog that you create and then do nothing with. Unless the EventLog class is doing some really weird stuff, that handler will never get fired.
|
|
|
|
|
I would try
sysLog.WriteEntry(sSource, "logger started");
instead of
EventLog.WriteEntry(sSource, "logger started");
This way your event handler should be called.
|
|
|
|
|
The correct syntax is to monitor event log entries in a Windows service is
protected override void OnStart(string[] args)
{
EventLog AppLog = new EventLog("Application", ".");
AppLog.EntryWritten += new EntryWrittenEventHandler(AppLogEntryWritten);
AppLog.EnableRaisingEvents = true;
}
Thanks to all for their input
|
|
|
|
|
I am having some basic compile issues defining a BorderBrush.
http://msdn.microsoft.com/en-us/library/system.windows.controls.border.borderbrush.aspx
states that you define a borderbrush like this:
myBorder1 = new Border();
myBorder1.BorderBrush = Brushes.SlateBlue;
but in my code, when I try
border1.BorderBrush = Brushes.SlateBlue;
I get
Error 1 The name 'Brushes' does not exist in the current context
and I have included the namespace
System.Windows.Controls
as a "using" statement at the top of the file
as well as a Reference to the project.
What am I missing?
|
|
|
|
|
As you can see here[^] the Brushes class lives in the System.Windows.Media namespace. So you need to include that via using also.
Cheers!
—MRB
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
|
|
|
|
|
using System.Windows.Media;
is there. It still does not work.
Here is all I am using:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
|
|
|
|
|
On the very link you list it shows you need to have:
using System.Windows.Media.Brush;
So do you have your project set for .Net Framework 4.0? and have you included a reference to PresentationFramework.dll?
The following code works fine for me, I'm assuming your project is a C# WPF project:
Border myBorder1 = new Border();
myBorder1.BorderBrush = Brushes.SlateBlue;
myBorder1.BorderThickness = new Thickness(5, 10, 15, 20);
myBorder1.Background = Brushes.AliceBlue;
myBorder1.Padding = new Thickness(5);
myBorder1.CornerRadius = new CornerRadius(15);
If you'll notice I added Border to the beginning of defining myBorder1 as this is the proper way to declare the variable.
modified 21-Oct-11 0:40am.
|
|
|
|
|
It's only in .Net 4, if using 3.5 (this is a WPF question so it must be at least that) use new SolidBrush(col).
|
|
|
|
|
I want to encapsulate a large number of older DOS tools within a gui and I have chosen to do this with Visual Studio 2008 C#, but I am unhappy with my current direction.
What I have done, is using a file menu system and panels my users can click on any tool or option within the menu and all panels get set to invisible and then the one that the user wants is set to visible. This works, but it is a pain for a few reasons, probably more than I know...
The panels are all part of the same form.cs which means functions and variables all live within the same file and namespace. I would like a solution where each panel would reside within it's own file\namespace for easy code reuse and organization.
When in design view, having to move panels around to get to the panel you want to use seems unecessarily frustratring. I feel like I should be able to do this in some very simple way that I just must be missing in my "new to gui" mind.
I am not so worried about making it easier, as I am about doing it right. Any suggestions or thoughts that I could employ to best implement a solution?
Thanks for any help given,
Tim
|
|
|
|
|
Maybe what you need to look at is a tabbed solution. Have a main TabControl in your form. Implement each of your present panels as a separate UserControl, which inherits from a TabPage, and then show the correct page for each menu item. This would mean you could separate each panel in the design and coding process, making your life a lot easier.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
How do I force the code for tab2 to be in a different file then the code on tab1? It looks like they both default into Form1.cs? The design view is easier to work with than what I had though in tabbed view, so it is a step in the right direction.
Tim
|
|
|
|
|
You should create separate UserControls for each 'panel'. Design and code each one separately. Then add the custom control to your tabcontrol, showing and closing the tab with the menu.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Ok, I'll read up on the userControls. Thanks for the pointer.
|
|
|
|
|
Yes, UserControls were going to be my recommendation. A user control is a class (that inherits from UserControl) so all its internal code goes in its own file. You should then expose events, properties and methods to allow UI interaction with other parts of the application, and some way of hooking up a data model for interaction with the business tier (might be very minimal if your app is basically just a wrapper for several existing ones, as that's essentially a UI only problem).
|
|
|
|
|
Or possibly as some sort of plug-in system where each utility has its own Panel or TabPage in its DLL.
|
|
|
|
|
Hi,
I have a function that converts r(red), g(green) and b(blue) values to a uint (same as RGB macro in C++).
If I implement as follows I get no errors or warnings:
private uint rgb(byte r, byte g, byte b)
{
return ((uint)((r|((uint)(g)<<8))|(((uint)b)<<16)));
}
However, if I implement like this:
private uint rgb(byte r, byte g, byte b)
{
return ((uint)((r|((ushort)(g)<<8))|(((uint)b)<<16)));
}
I get a warning "Bitwise or operator used on a sign extended operand".
But ushort is un-signed, so why am I getting the warning?
Thanks - John.
|
|
|
|
|
ushort will be being expanded to uint because b is still being shifted as a uint (as it must be). That will cause the ushort to be 'sign extended' i.e. the new bits to be filled with the top bit, I suppose, though that doesn't really make sense (an unsigned type should always be zero extended).
|
|
|
|
|
It would appear that the logical expression is yielding an int value which is,by definition, signed. You should cast each part of the expression to uint as in the first case. you should also review your use of parentheses around your expressions, a better expression would be:
return (uint)(r | (((uint)g) << 8) | (((uint)b) << 16));
return (uint)(r | (g << 8) | (b << 16));
|
|
|
|
|
Thanks Guys,
I would have expected an expression containing unsigned variables to yield an unsigned, but I must be wrong and by default it yields an int.
return (uint)(r | (g << 8) | (b << 16));
...Seems like the best option.
Thanks again.
|
|
|
|
|
The documentation[^] is a little bit ambiguous, but the implication is that a left shift will return int (Int32 ) by default, hence the problems you encountered. I am sure there is a more explicit rule somewhere in the depths of the language specification if you care to look (but hey, life is short!).
|
|
|
|