|
The service control manager (SCM) handles querying for dependent services and stopping them. What you see in the user interface is different than what happens. When you stop a service the SERVICE_CONTROL_STOP message is sent to the service's Handler or HandlerEx control handlers (this is true with the ServiceBase class, which encapsulates the native functionality). So, you're right, the code when the service is stopped is the same.
But the code that stops a service is different. The Services snap-in for MMC queries the service for dependent services first, then - if you click "Yes" - stops each one, querying those for dependent services. Finally, the select service is stopped.
You would need to do the same in your code. See http://msdn.microsoft.com/library/en-us/wmisdk/wmi/wmi_tasks__services.asp[^] for examples of how to determine the dependent services (at the bottom). You'll find some other useful examples of queries to use with the ObjectQuery class.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Awesome. That's exactly what I needed. Now I need to decide whether my code should also start any dependent services when it starts a service or if I should leave that to the user...
Maybe I'll just notify them.
Thanks very much!
|
|
|
|
|
It's often better to not do too much automatically that isn't required. Services that depend on yours would have to be stopped, while they don't necessarily have to be started.
As a compromise between you and your users, I recommend prompting them - as you should when shutting down your service - to start services that depend on your service. When shutting down a service, you can do a simple Yes/No dialog (using MessageBox.Show with MessageBoxButtons.YesNo ). If they choose No you can't stop your service. Simple.
When starting it back up, however, it's more difficult than that. You could develop your own Form derivative with, perhaps, a CheckBox that states something like "Also start services that depend on this service". Have it unchecked by default.
The tricky thing is that they might've set such a service to Manual and don't want it started. You should check for that, too. In such an event, though, how do you know they don't want it started? To get around this - if possible - record which services you stop and only re-start those if the user has opted to start such services.
Or just don't start them like the Services snap-in does.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Right. I implemented a message box that informs them that other dependent services stopped (and the names of those services), just so they know. The people who will be using this will know what services they want running, and it's simple enough for them to restart dependent services themselves if they so desire. They can handle doing that thinking on their own.
Interestingly enough, I've found that (in at least some cases) when you start a service that depends on another service that is stopped, it will start that other service first. The TCP/IP Print Server will start the Print Spooler if it's stopped when you pass it the "StartService" method, for example.
|
|
|
|
|
I have an object (not control) that belonges to a Control. Basically, the object (element) is a helper class to divide the control into sections in order to simplify buildup and rendering. Very close to the Avalon UIElement idea (in fact, I named my object also UIElement after that).
<br />
public class UIElement : IDisposable<br />
{<br />
___public Control mainControl{...};<br />
___public void DrawElement(bool childElements){...};<br />
___public Location location{...}<br />
}
Imagine that you have a custom-made toolbar. The base of this toolbar is a Control (or UserControl if you will). Now the grip of the toolbar is one element, the toolbar item is second element and so on ... All sections of the toolbar (grip, items etc) are drawn by UIElements.
I am wondering how could I add mouse panning functionality to the element, so that I could have different sections of the control having different mouse events (like in the former example, when you click on the grip element you start dragging or when you click on the item element you open up a popup menu).
Any ideas how is the mouse handling done in the Control class ?
P.S. When ready, I'll release the code to public ...
|
|
|
|
|
desmond5 wrote:
The base of this toolbar is a Control (or UserControl if you will).
That would be incredibly inefficient with too much overhead. A UserControl is a container control, used for hosting other controls like a Form hosts controls. This should not be used for single objects.
Even more advanced toolbars that seem to host other controls like a combo box are not container controls. They typically "site" (position) those controls at a particular location and move them when the control itself is moved.
desmond5 wrote:
Any ideas how is the mouse handling done in the Control class ?
Much of the mouse interaction is not handled by the Control class at all. Almost every Windows Forms control in the BCL encapsulates the related Windows Common Control - the very same native controls you see throughout Windows and Windows applications. This gives a consistent user interface whether your application is writtin in C/C++ using the Win32 APIs, MFC (C++), VB, or .NET (or a slew of others, excluding Java that wrote it's controls from the ground up for consistency across platforms, which is arguably less important that a single platform where most users spend their days).
If you want to see the implementation for yourself, learn to read Intermediate Language (IL) and use the ildasm.exe (IL Disassembler) application that ships with the .NET Framework SDK and is located in the Bin directory of the .NET Framework SDK installation. This is installed by default with VS.NET under the VS.NET installation root.
You could also use a decompiler like the .NET Reflector[^].
For your approach where you're defining everything in managed code, a more common approach is to have each UIElement implement a Click handler like so:
public class UIElement : IDisposable
{
public abstract void OnClick(MouseEventArgs e);
} Override OnMouseDown , for example, on your toolbar control and pass the MouseEventArgs passed to you to the UIElement at those client coordinates (i.e., 0,0 corresponds to the upper-left corner of the control that was clicked - not the screen). That will allow you to get any relative mouse click point for the element as well as any modifier keys.
A better design would be to encapsulate only the properties of the MouseEventArgs you want, which makes this easily callable for key events (like pressing the [Space] key when the UIElement has the focus, if you implement focus within the toolbar itself).
Regarding your naming conventions you should really read Naming Guidelines[^] in the .NET Framework SDK. Methods (not functions, mind you) should never be lower case, nor should properties. Fields may be, as it really doesn't matter how you name private fields (no one should see them anyway, but take into account how they're serialized, implementing ISerializable for custom serialization if needs be).
It's important to follow the guidelines as it makes developing applications using your library easier when it's consistent with the other libraries a developer would be using. Just think of all the classes, members, etc. that you use from the BCL? Have you ever seen a method that begins with a lower-case letter? A property, or a class? Not in the BCL (base class library) you won't.
It's good to run your assemblies through FxCop[^], which will not only catch naming violations (not typically bad violations, but following the guidelines is helpful for your clients) but many other errors as well.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
There is a method of the DataTable object that I require to access from various methods of my class basically to retrieve the column name as a specific index.
I have created a private property in my class to access this method and defined it similar to as follows:
<br />
private string ColumnName<br />
{<br />
get {return DataTable.Columns[ColumnNumber].ColumnName.ToString();}<br />
set {DataTable.Columns[ColumnNumber].ColumnName = value;}<br />
}<br />
Is this acceptable practice?
Regards
Wayne Phipps
____________
Time is the greatest teacher... unfortunately, it kills all of its students
LearnVisualStudio.Net
|
|
|
|
|
This is certainly "acceptable", however, you might ask yourself "what is the benefit?". Considering that your DataTable and ColumnNumber fields appear to be instance variables, there's no reason when you need that specific column name you can't just call it directly as in DataTable.Columns[ColumnNumber].ColumnName.
If what you want is a short cut method, create some methods like this (that aren't properties).
private string GetColumnNameAt( int index )
{
if( DataTable.Columns[index] != null )
return DataTable.Columns[index].ColumnName;
}
private void SetColumnNameAt( int index, string name )
{
if( DataTable.Columns[index] != null )
DataTable.Columns[index].ColumnName = name;
}
Let me know if you need clarification.
Best Regards.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
confused:Hi everyone,
I´m trying to serialize some objects in C# using BinaryFormatter. The problem is such that multiples objects being serialized make reference to a same object and I don´t know how to map these references so that I can have only one copy of the object being referenced.
Does anyone know a way to get through it? Is there some facility in C# that handles it?
Thanks.
|
|
|
|
|
If you put all the objects in a Hashtable or a set and serialize the whole thing, only one copy of each object will be serialized.
Additionally, I like to write my code so that I can easily change the serialization to use a BinaryFormatter or the SoapFormatter. That makes it easier to verify your assumptions about how things are being serialized (to some extent).
Matt Gerrans
|
|
|
|
|
I'm writing a program that will populate a TreeView with a list of all the "programs" that can be run from within my program. These "programs" will actually just be MDI children. The name of the MDI child form will be loaded into the TreeMenu.SelectedNode.Tag property.
My problem is to be able to launch a form dynamically based on which item they double-click on.
Any ideas?
--In a world without fences, who needs Gates?
|
|
|
|
|
SignMan359 wrote:
My problem is to be able to launch a form dynamically based on which item they double-click on.
You can handle the AfterSelect event from the TreeView . This will allow you to interogate the TreeNode that issued the event. Something like the following will do it:
tv = new TreeView();
tv.AfterSelect += new TreeViewEventHandler(tv_AfterSelect);
private void tv_AfterSelect(object sender, TreeViewEventArgs tvea)
{
TreeNode node = tvea.Node;
if(node != null)
{
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
I'm sorry, maybe I confused the issue. I'm not having a problem with the TreeView. Here is where my problem is:
Let's say you have a form named HelloWorld, you would get that for to show up by typing something like:
HelloWorld newForm = new HelloWorld();
newForm.Show();
My problem is how do I get the same result of it was more like this:
string formName = "HelloWorld";
formName newForm = new formName;
Yes, I know this is totally invalid syntax, but I think it explains what I am trying to do. does anyody have any idea how to do this correctly?
--In a world without fences, who needs Gates?
|
|
|
|
|
In this example I am storing the Mdi's type in the node's tag. You could set the text of ur node to be the mdi form's classname instead if it were a valid solution.
Type type=Type.GetType((string)selectedTreeNode.Tag);
Form mdiChild=(Form)Activator.CreateInstance(type);
P.D. Activator.CreateInstance() can pass arguments to the object to instantiate constructor if needed. Just check the msdn information about the different overloads
|
|
|
|
|
I've tried the above. I have the name of the class (and therefore the form) in the .tag property. However, when I try to run this code, it leaves the type variable null.
I'm at a loss. I'm pretty new to C# and .Net, so please forgive my ignorance if this is a simple problem.
--In a world without fences, who needs Gates?
|
|
|
|
|
Put the whole name of your type, for example: MyApplication.MyObjects.MyType (Check the namespace of ur type.)
|
|
|
|
|
I have two datagrid controls and I want to syncronize the scrolling between them. I can find the HScrollBar for both datagrids by searching through their Controls collection and I can intercept the scroll events of the datagrids. In the scroll event I try and set the HScrollBar.Value of one scroll bar to be equal to the other. The problem is that while this does, in fact, move the scroll bars, the datagrid (the one I'm trying to force to scroll) does not get redrawn. I tried using Refresh() and Invalidate() methods of the datagrid, but neither of them seem to work. In the application if you click on the scroll bar that was moved programatically the datagrid will then update to reflect the new position of the scroll bar.
Does anybody have any idea on how I can make this work?
<br />
private void grid_Scroll(object sender, EventArgs e)<br />
{<br />
this.hsbLower.Value = this.hsbGrid.Value;<br />
}<br />
Thanks
|
|
|
|
|
I've been reading about virtual functions and I keep seeing code like this in the examples:
Dimensions c = new Circle(r);
The Circle class is derived from Dimensions class.
I understand the benefits of inheritance but I don't understand why you would declare a circle as a Dimensions class. This seems to be a common practice, too. The following three articles from MSDN all do it but I don't understand why. Are they just trying to demonstrate which method would be called under these special declaration circumstances?
Why not do this:
Circle c = new Circle(r);
1) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfvirtualpg.asp
2) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkversioningtutorial.asp
3) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_10_5_3.asp
|
|
|
|
|
public Circle(double r): base(r, 0)
{
}
This is the reason why it initialize the circle...
Its constructor , construct a new object by sending values to base class..So there is no difference between the two way
public virtual double Area()
{
return x*y;
}
and by declaring the virtual method , all children classes will implement their own Area method.. by overriding..
public override double Area()
{
return 2*pi*x*x + 2*pi*x*y;
}
In the first article , if you be more careful , each children has its unique area method , but with same commmon members
|
|
|
|
|
I suspect this is just one of those practices that is common in book examples demonstrating some aspect of inheritance, but has very little practical application. I agree, just say Circle c = new Circle(r); .
my blog
|
|
|
|
|
I'll have to disagree with that. Through inheritance u can get polymorphism which comes handy in thousands of code situations.
Another simplified example:
Imagine ur making an inventory of all kind of diferent InventoryObjects (big mall with all kind of different stuff on sale). Ok lets create a base class IventoryObject and a public property named InventoryId.
Then we would create all the kind of different objects derived form the base InventoryObject that implement all the diferent details of our real objects which could be very diferent in nature and thus require very different abstractions (for example Books, Hardware, VacuumCleaners, etc)
Now we want to iterate through our whole collection of objects looking for certain InventoryIds. We would do that iterating through our stronglytyped InventoryObjectCollection and getting the common InventoryId property. (The collection would obviously be stronglytyped to InventoryObject and as all our real objects are derived from this class, all casts would be succesful)
Otherwise u would have to keep stronglytyped collections of all our different object types and iterating through each of them.
Of course this can also be done also through Interfaces and nobody discusses their usefulness:
public Interface IIdentifiableObject
{
int InventoryId{get;}
}
But inheritance is already giving u this feature for free.
|
|
|
|
|
Hi all,
I am writing a server program for multi-client access in C#,
the server will update, delete, add object onto the database.
I am not quite sure the proper method to handle multi-client access.
There are object layer and database layer in the program, when two clients want to update the same object A to the database via the object layer at the same time, should the synchronization of object be
implemented at the object layer or the database layer ?
What are the considerations to choose which layer to synchronize ?
Are there any reference for these kind of programming ?
Thanks
Patrick
|
|
|
|
|
If you're using Sql Server for the database and two people update at the same time, the second person's update will happily over write the first person's update.
If you have two people working on the same record at the same time in your user interface and you want to ensure when the first person updates the record, that the second person gets notified that there has been a change made since starting to work with the record themselves, then you would probably want to put that into your business logic (object layer). You should put a last update date column into your database table that is being updated and check it before calling the update. You should also save the date/time that the record was opened and check that against the last update on the record. If it has been updated since the current person first accessed it, then you should prompt the user to determine if they want to see what has changed and/or if they want to overwrite it.
Hope that helps.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
Hi and a happy new year!
I like to get the application path (startup path) of my console application (with a windows form it is no problem because I am having the Application object).
How can I do this?
Thank you for your help!
senfgurke.
life is very short and there is no time for fussing and fighting my friend.
|
|
|
|
|
Ah, ok, I found it:
using System.IO;
...
string AppPath = Directory.GetCurrentDirectory();
...
life is very short and there is no time for fussing and fighting my friend.
|
|
|
|