简体   繁体   中英

Selecting multiple months for a MonthlyTrigger using css

I have the need to create scheduled windows tasks using a C# app. I have a comma separated string that stores the months I'd like to run the task on. The string contains the short values for the type MonthsOfYear - eg. "1,2,4,16,128,1024".

The example I have shows that you can assign multiple months seperated by a pipe as follows:

MonthlyTrigger mt = new MonthlyTrigger();
mt.StartBoundary = Convert.ToDateTime(task.getStartDateTime());
mt.DaysOfMonth = new int[] { 10, 20 };
mt.MonthsOfYear = MonthsOfTheYear.July | MonthsOfTheYear.November;

My question is, how do I assign multiple months to the trigger dynamically, using the values from the comma seperated string.

I'm not quite sure, what your problem is. And you didn't post code of your Trigger or your enum. Because of this i'll provide a complete example with a List for comparesion:

public class MonthlyTrigger
{
    [Flags] // Important because we want to set multiple values to this type
    public enum MonthOfYear
    {
        Jan = 1, // 1st bit
        Feb = 2, // 2nd bit..
        Mar = 4,
        Apr = 8,
        May = 16,
        Jun = 32,
        Jul = 64,
        Aug = 128,
        Sep = 256,
        Oct = 512,
        Nov = 1024,
        Dec = 2048
    }

    public HashSet<int> Months { get; set; } = new HashSet<int>(); // classical list to check months
    public MonthOfYear MonthFlag { get; set; } // out new type
}

public static void Main(string[] args)
{
    MonthlyTrigger mt = new MonthlyTrigger();

    string monthsFromFileOrSomething = "1,3,5,7,9,11"; // fake some string..

    IEnumerable<int> splittedMonths = monthsFromFileOrSomething.Split(',').Select(s => Convert.ToInt32(s)); // split to values and convert to integers

    foreach (int month in splittedMonths)
    {
        mt.Months.Add(month); // adding to list (hashset)

        // Here we "add" another month to our Month-Flag => "Flag = Flag | Month"
        MonthlyTrigger.MonthOfYear m = (MonthlyTrigger.MonthOfYear)Convert.ToInt32(Math.Pow(2, month - 1));
        mt.MonthFlag |= m;
    }

    Console.WriteLine(String.Join(", ", mt.Months)); // let's see our list
    Console.WriteLine(mt.MonthFlag); // what is contained in our flag?
    Console.WriteLine(Convert.ToString((int)mt.MonthFlag, 2)); // how is it binarily-stored?


    // Or if you like it in one row:
    mt.MonthFlag = 0;
    foreach (MonthlyTrigger.MonthOfYear m in monthsFromFileOrSomething.Split(',').Select(s => (MonthlyTrigger.MonthOfYear)Convert.ToInt32(s)))
        mt.MonthFlag = m;

    return;
}

Adding or removing single Flags in an enum:

MyEnumType myEnum = 1; // enum with first flag set

myEnum |= 2; // Adding the second bit. Ofcouse you can use the enum-name here "MyEnumType.MyValueForBitTwo"
// Becuase:
//   0000 0001
// | 0000 0010   "or"
// = 0000 0011

myEnum &= (int.MaxValue - 2) // Deletes the second enum-bit.
// Because:
//   0000 0011
// & 1111 1101   "and"
// = 0000 0001

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM