Category Archives: C#

TechDay 2014: Azure Workshop – Demos and Slide Decks


After Microsoft TechDay finished yesterday I got several emails and questions about my demos and slides from the Azure Workshop.

So here is a link from which you can download all slides and demos you saw yesterday on my Workshop:

Slides about Microsoft Azure Training are general available at Git site: https://github.com/Azure-Readiness/DevCamp.

Once more thank you all for attending the workshop and see you next time. I am very glad I can see lot of people interested in Microsoft Azure and other cool technologies from Microsoft.

Remove items from a List while enumerating


Here is a simple way to remove items from currently enumerated list.
Put the list in while loop and make a manual index counter (var ind=0). Within while loop, every time items is removed decrease the index counter, otherwise increase the index.

private static void RemoveDirty(List<Element> lst)
{
    var ind = 0;
    while(ind<lst.Count)
    {
        var el= lst[ind];
        if (el.IsDirty)
        {
            lst.RemoveAt(ind);
            // in case item is removed decrease the index
            ind--;
        }
        // increase the index when nothing is happen
        ind++;
    }
}

Get resource string from .NET application regardless of the current culture


It is common practice that for the multilingual .NET application language specific strings are placed on the Resource of the .NET application. Each supported language has itc own resource file which holds the translated strings. Many year ago I wrote article about it, and you can find it on this location (on Bosnian language).

But what if you want to get the resource string regardless of you current culture. In fact when you call App.Properties.Resources.SomeString you always  the localized string based on the current culture. If you want to get string from other language than current culture, you need to call something else.

For example: Suppose your current culture is English, and you want to get string from the German resources. The picture below shows the resource string in both language specific resources (the first one is German, the second is English resource):

resources
First you need to create Culture info object for german language, and call the GetString method from ResourceManages class by passing the cultureInfor object. The code below show the procedure of getting the string in other that current application language.


System.Globalization.CultureInfo germanInfo = new CultureInfo("de-DE");
string germanWord= Properties.Resources.ResourceManager.GetString("CompatibleMode", germanInfo);

Happy programming

How to run code daily at specific time in C# Part 2


dailyruncode

Few months ago I wrote blog post about how to run code daily at specific time. I dint know that the post will be the most viewed post on my blog. Also there were several questions how to implement complete example. So today I have decided to write another post, and extend my previous post in order to answer thise question as well as to generalize this subject in to cool demo called Scheduler DEMO.

The post is presenting simple Windows Forms application which calls a method for every minute, day, week, month or year. Also demo shows how to cancel the scheduler at any time.

The picture above shows simple Windows Forms application with two  numeric control which you can set starting hour and minute for the scheduler. Next there is a button Start to activate timer for running code, as well as Cancel button to cancel the scheduler. When the time is come application writes the message on the Scheduler Log.

Implementation of the scheduler

Scheduler is started by clicking the Start button which is implemented with the following code:

/// <summary>
/// Setting up time for running the code
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void startBtn_Click(object sender, EventArgs e)
{

    //retrieve hour and minute from the form
    int hour = (int)numHours.Value;
    int minutes = (int)numMins.Value;

    //create next date which we need in order to run the code
    var dateNow = DateTime.Now;
    var date = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, hour, minutes, 0);

    listBox1.Items.Add("**********Scheduler has been started!*****");

    //get nex date the code need to run
    var nextDateValue=getNextDate(date,getScheduler());

    runCodeAt(nextDateValue, getScheduler());

}

When the time is defined then the runCodeAt method is called which implementation can be like the following;

/// <summary>
/// Determine the timeSpan Dalay must wait before run the code
/// </summary>
/// <param name="date"></param>
/// <param name="scheduler"></param>
private void runCodeAt(DateTime date,Scheduler scheduler )
{
    m_ctSource = new CancellationTokenSource();

    var dateNow = DateTime.Now;
    TimeSpan ts;
    if (date > dateNow)
        ts = date - dateNow;
    else
    {
        date = getNextDate(date, scheduler);
        ts = date - dateNow;
    }

    //enable the progressbar
    prepareControlForStart();

    //waits certan time and run the code, in meantime you can cancel the task at anty time
    Task.Delay(ts).ContinueWith((x) =>
        {
            //run the code at the time
                methodToCall(date);

                //setup call next day
                runCodeAt(getNextDate(date, scheduler), scheduler);

        },m_ctSource.Token);
}

The method above creates the cancelationToken needed for cancel the scheduler, calculate timeSpan – total waiting time, then when the time is come call the method methodToCall and calculate the next time for running the scheduler.

This demo also shows how to wait certain amount of time without blocking the UI thread.

The full demo code can be found on OneDrive.

Follow

Get every new post delivered to your Inbox.

Join 570 other followers