Thursday, July 28, 2011

Using command line to control Windows Scheduled Tasks

You can control, monitor, change, etc Windows Scheduled Tasks using schtasks from the command line. You can use it on your local machine or a remote machine such as a server.

In the examples below let’s assume that the Windows Scheduled Task you are interested in is called EventLogImport.

IMPORTANT: For any of the commands or examples below, you can add a /S and then the servername to execute the command against a remote machine running Windows.

Monitoring

Basic Info on local machine

schtasks /QUERY /TN EventLogImport

 

Basic Info for remote machine

schtasks /QUERY /S serverNameHere /TN EventLogImport

 

Verbose Info

schtasks /QUERY /V /TN EventLogImport

 

Verbose Info with CSV output to screen

schtasks /QUERY /FO CSV /V /TN EventLogImport

NOTE: Other options are TABLE and LIST. The /V is for Verbose.

 

To remove the header row you can use the MORE command to start reading at the second line

schtasks /QUERY /FO CSV /V /TN EventLogImport | more +1

 

Verbose Info with CSV output to file

This will clear the status.csv if it exists, else it will create a new file.

schtasks /QUERY /FO CSV /V /TN EventLogImport > c:\temp\status.csv

 

To append to the file if you call two or more of these in a row, use the >> instead of >.

schtasks /QUERY /FO CSV /V /TN EventLogImport >> c:\temp\status.csv

 

To remove the header row you can use the MORE command to start reading at the second line

schtasks /QUERY /FO CSV /V /TN EventLogImport | more +1 >> EventLogImportScheduledJobStatus.csv

 

Controlling

Disable a Scheduled Task

schtasks /CHANGE /TN EventLogImport /DISABLE

 

Enable a Scheduled Task

schtasks /CHANGE /TN EventLogImport /ENABLE

 

Other Commands

You can also create, delete, run, or kill scheduled tasks as well. For information on how to do this, I recommend typing schtasks /? at any command prompt.

For help on a specific switch, try one of the following:

SCHTASKS
SCHTASKS /?
SCHTASKS /Run /?
SCHTASKS /End /?
SCHTASKS /Create /?
SCHTASKS /Delete /?
SCHTASKS /Query  /?
SCHTASKS /Change /?
SCHTASKS /ShowSid /?

Tuesday, July 26, 2011

Getting Started with Microsoft Charting

Microsoft has provided a very nice charting package that is available on the WinForms and ASP.NET and is included (built into) .NET 4. It is also available for download with .NET 3.5. It is a VERY robust charting package that is extremely customizable. In fact, you can even modify the images before they are sent to the user (in the case of ASP.NET). I am sure there is something this package can’t do, but I have not ran into it yet.

It is actually quite easy to use, but it is best if you read through some examples. The best way to get started in my opinion is to follow the first link below. If you go through the tutorial you will be in good shape. Once you have done that, you may want to check the second link out for additional references. Next check the official docs when you need more info on specific properties, etc. The final link is really just a cool snippet of code to get you started in the right direction if you need to generate your own gradients for your charts; for example in a bar or column chart.

Using Microsoft’s Chart Controls In An ASP.NET Application: Getting Started (by Scott Mitchell)

Built-in Charting Control (VS 2010 and .NET 4 Series) – ScottGu’s Blog

Official Documentation

Post that shows basic idea of how to create your own gradient image that can then be used as a background in a chart.

Friday, July 15, 2011

Easily Disable Row Selection for a Silverlight DataGrid

This technique actually prevents the row from being selected and prevents an unwanted cell from being selected. The visual effect is that the row or cell in that row never appears to be selected.

I assume you know how to load you DataGrid with data, etc. In my example, I only have one column of data. The trick to this solution is almost all in code behind. Also, this all assumes that you have set IsReadOnly="True" either in the XAML or the code behind.

 

In this example my DataGrid is named dgAnnouncements. I handle the SelectionChanged event as show in the method below.

private void dgAnnouncements_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    dgAnnouncements.SelectedItem = null;
}

The above gets us most of the way there, however you will notice that the cell itself can still be selected. To handle this we have to do a trick.

In the XAML for the DataGrid you will need to add an invisible column at position 0 so that we can select it whenever someone tries to select another cell that they can see.

Here is an example of the XAML for the DataGrid.

<sdk:DataGrid x:Name="dgAnnouncements"
ItemsSource="{Binding Data, ElementName=dsAnnouncements}"
AutoGenerateColumns="False"
IsReadOnly="True"
CurrentCellChanged="DataGrid_CurrentCellChanged"
SelectionChanged="dgAnnouncements_SelectionChanged">

    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn Binding="{Binding Path=Nothing}" MinWidth="0" MaxWidth="0" />
        <sdk:DataGridTextColumn Binding="{Binding Path=Title}"  />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

Here is the code behind to redirect the current cell to our invisible cell.

private void DataGrid_CurrentCellChanged(object sender, EventArgs e)
{
    if (dgAnnouncements.CurrentColumn != null)
    {
        dgAnnouncements.CurrentColumn = dgAnnouncements.Columns[0];
    }
}

Thursday, July 14, 2011

Creating your own MessageBox in Silverlight

The MessageBox built into Silverlight is nice enough to use for simple OK, Cancel scenarios, but sometimes I want to change the text of the buttons in the MessageBox. For example, I may want Yes, No instead of OK, Cancel or maybe completely different text like Finished Now, Finish Later. The syntax of the MessageBox is simple enough so I decided to mirror it as closely as I could. This makes it as painless as possible to change from a MessageBox to my message box. The biggest difference in usage that I couldn’t figure out how to get around is that the MessageBox is a blocking call, but the UI thread still continues to render. When I tried to do the same, the UI thread did not continue to render. I looked at the code for MessageBox and it appears to be calling a Windows native MessageBox that is most likely different for OS that Silverlight is implemented on. So, I decided that in the Silverlight manor I would use asynchronous calls (event handlers) instead of a blocking call. When I use delegates the code is similar to using MessageBox.

Okay, enough description I think. Here is source code for my version of MessageBox called MessageBox2

Download Source Code for Control

MessageBox2.xaml.cs   MessageBox2.xaml

Usage

You’ll notice I have 3 Show() methods instead of the standard 2 that MessageBox has. The reason is that I added one so that you can specify the text of OK and Cancel buttons. You’ll notice that I don’t return a MessageBoxResult and instead return the MessageBox itself.  Below is how you would use the MessageBox versus MessageBox2.

MessageBox

MessageBoxResult result = MessageBox.Show("This is a choice test", "Some caption", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
    DoSomethingUsefulHere();
}
else if (result == MessageBoxResult.Cancel)
{
    DoSomethingUsefulHere();
}

MessageBox2

var msgBox2 = MessageBox2.Show("This is a choice test", "Some caption", MessageBoxButton.OKCancel, "Yes", "No");
msgBox2.OKButton.Click += delegate(object sender2, RoutedEventArgs e2) { DoSomethingUsefulHere(); };
msgBox2.CancelButton.Click += delegate(object sender2, RoutedEventArgs e2) { DoSomethingUsefulHere(); };

You could also use the Closed event and check the DialogResult to see if it was accepted or cancelled. Also, notice I changed the OK button to Yes and the Cancel button to No.