This project has moved and is read-only. For the latest updates, please go here.

OutOfMemoryException when creating a TaskService with wrong password

Topics: Errors
Apr 11, 2014 at 2:47 PM
Edited Apr 11, 2014 at 2:58 PM

recently I get an OutOfMemoryException when I instantiate an object of TaskService (giving a wrong password) and my windows account got blocked (I cannot login anymore) :/

In a first call I get "Access Denied" .. in a subsequent call I get "OutOfMemoryException"

            TaskService ts = new TaskService(

bei Microsoft.Win32.TaskScheduler.V2Interop.TaskSchedulerClass.Connect(Object serverName, Object user, Object domain, Object password)
bei Microsoft.Win32.TaskScheduler.TaskService.Connect()
bei Microsoft.Win32.TaskScheduler.TaskService.EndInit()
bei Microsoft.Win32.TaskScheduler.TaskService..ctor(String targetServer, String userName, String accountDomain, String password, Boolean forceV1)

Any idea?


Apr 14, 2014 at 8:13 AM
I see that someone else had the same problem long time ago ( But I don't find a solution for it? Any help please?

I noticed that this error happens when "Credentials.ServerName" is the local machine. When I test the case (i.e., providing wrong password) on a remote server I got always "Access Denied" (which is what I expect also on the local machine). The question is: why there is a difference between executing it on local vs. remote machine?
Apr 14, 2014 at 5:22 PM
What values are you providing as parameters and what version of Windows is the local machine? OutOfMemoryException usually means there is an infinite loop somewhere. I don't see any in this section of my code so I'm wondering if this may be a Microsoft issue. If it is, then maybe I can prevent the scenario in my code.
Apr 15, 2014 at 6:55 AM
It happens on Window 8.1 Pro.

I wrote a small test WPF application. Here is the code (used v4.0 of Task Scheduler Managed Wrapper):


public partial class MainWindow : Window
    public MainWindow()
        this.TxtServer.Text = Environment.MachineName;
        this.TxtUsername.Text = string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName);

    private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        string server = this.TxtServer.Text;

        string[] domainUsername = this.TxtUsername.Text.Split('\\');
        string domain = string.Empty, username = string.Empty;
        if (domainUsername.Any())
            if (domainUsername.Length == 2)
                domain = domainUsername[0];
                username = domainUsername[1];

        string password = this.TxtPassword.Password;
            TaskService ts = new TaskService(server, username, domain, password);
            TaskDefinition td = ts.NewTask();
        catch (Exception ex)
            MessageBox.Show(string.Format("{0}\n{1}", ex.Message, ex.StackTrace));

    private void BtnCancel_Click(object sender, RoutedEventArgs e)


<Window x:Class="TaskSchedulerDemo.MainWindow"

    mc:Ignorable="d ignore"
    Title="Task Scheduler Demo"
<StackPanel Margin="10">
    <StackPanel Margin="5,0,0,5">
        <Label Content="Enter username and password"/>
    <StackPanel Margin="10, 0, 0, 0" Orientation="Horizontal">
        <StackPanel  Width="100">
            <Label Content="Server:" Width="170" Height="30" Margin="-5, 0, 0, 0"/>
            <Label Content="Username:" Width="170" Height="30" Margin="-5, 5, 0, 0"/>
            <Label Content="Password:"  Width="170" Height="30" Margin="-5, 5, 0, 0"/>
        <StackPanel Width="250" HorizontalAlignment="Center">
            <TextBox   x:Name="TxtServer" TextWrapping="Wrap" Width="250" Height="23" Margin="0, 5, 0, 0" />
            <TextBox   x:Name="TxtUsername" TextWrapping="Wrap" Width="250" Height="23" Margin="0, 10, 0, 0"/>
            <PasswordBox   x:Name="TxtPassword" Width="250" Height="23" Margin="0, 10, 0, 0"/>
    <Grid  Margin="0, 10, 0, 0">
            <ColumnDefinition Width="110"/>
            <ColumnDefinition SharedSizeGroup="Buttons"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition SharedSizeGroup="Buttons"/>
        <Button Grid.Column="1" x:Name="BtnOk" Content="OK" Click="BtnOk_OnClick" IsDefault="True"/>
        <Button Grid.Column="3" x:Name="BtnCancel" Content="Cancel" Click="BtnCancel_Click" IsCancel="True"/>
Apr 16, 2014 at 8:10 AM
Have you tried it? Did you find anything that explains the problem?
Apr 16, 2014 at 3:03 PM
I believe that since you are pulling the server name from a TextBox, if blank the value will be String.Empty. This may be causing the problem. I have updated the code so that String.Empty values are changed to null. However, until that code is released, try this check:
string server = this.TxtServer.Text;
if (String.Empty == server)
   server = null;
Apr 17, 2014 at 7:52 AM
Well giving "server = null" has solved the problem, but not because it was empty. I have always provided a server name even if I am working with my local computer. Apparently I had to give "null" if the "server == computer_name". Maybe you should fix this in your code in this way:

if (server.equals(Environment.MachineName)){
server = null;

I must admit that I find the Execption I got because of this issue a weird one :/

Thanks anyway for your help! :)
Apr 17, 2014 at 5:06 PM
I have updated the code in 2.1.1 to avoid this problem. The oddly termed exception is actually coming from the Microsoft system library. It must be a bug there. Anyway, thanks to your help, users of this library should never encounter it again.
Marked as answer by dahall on 9/1/2015 at 10:08 AM