One of my first questions starting programming in Visual Studio WPF was how to run processes without freezing the GUI.
After learning the basic concepts of backgroundworkers and tasks in c sharp programming I wanted to know how to report task progress while the task was not yet finished.
I found all kind of solutions to update a progress bar. I also found solutions to use the result for a specific task.
To implement this logic in a solution I started with a form on which I added a button and a listbox.
The button got the following implementation:
private async void ButtonAsyncFileProcessing_Click(object sender, RoutedEventArgs e)
{
var progressIndicator = new Progress<MyTaskProgress>(ReportProgress);
await MyMethodAsync(progressIndicator);
}
For the progress indicator I added the following class:
public class MyTaskProgress
{
//current progress
public int CurrentProgressAmount { get; set; }
//total progress
public int TotalProgressAmount { get; set; }
//some message to pass to the UI of current progress
public List<string> CurrentProgressLogging { get; set; }
}
The async method I used for testing was this:
async Task MyMethodAsync(IProgress<MyTaskProgress> progress){ int sleepTime = 1000; int totalAmount = 10000; List<string> log = new List<string>(); for (int i = 0; i <= totalAmount;){await Task.Delay(sleepTime); log.Add(string.Format("On {0} Message", i)); progress.Report(new MyTaskProgress{CurrentProgressAmount = i, TotalProgressAmount = totalAmount, CurrentProgressLogging = log });i = i + sleepTime; }}
The ReportProgress method used in the button click progressIndicator:
private void ReportProgress(MyTaskProgress progress) { UpdateScreen(progress.CurrentProgressLogging, string.Format("{0} out of {1}", progress.CurrentProgressAmount, progress.TotalProgressAmount)); }
Goto Pipelines in your Azure Devops project and click on new pipeline. My example uses a project named WPFDatasetWithSQL.
*Important note: This solution will only work when you do NOT have a .gitignore file in your repository*
Click continue and choose .Net Desktop and click Apply.
If you want
to build the solution using a hosted machine keep the “Agent pool” set on
“Hosted VS2017”. If you need local components to build you could choose to use
a local machine or set up the required components in this build script.
For this
example I have no need for extra components and I will keep the Agent pool on
Hosted VS2017.
We are going
to change a few setps in this script:
1 Set the
MSBuild Arguments to /target:publish. This changes the MSBuild to add a
app.publish to the build directory for click once deployment.
2 Change the step Copy Files to add the app.publish folder to the artifacts folder. Display name = Copy Files to: $(build.artifactstagingdirectory) Source Folder = $(Build.SourcesDirectory)\src\BLM\bin\$(BuildConfiguration)\app.publish Contents = **\**
3 Change the artifact name. Display name = Publish Artifact: $(System.TeamProject)-$(Build.BuildNumber) Artifact name = $(System.TeamProject)-$(Build.BuildNumber)
Click Save and keep the default name.
Set up a release pipeline
Now we will
set up a release pipeline in which we can control and manage releases for this
application.
Click on Releases in the menu and click New pipeline.
Choose a Empty job template. The release pipeline is going to contain not much more than a few copy tasks.
For
starters we will have to choose an artifact. Choice is simple, we are going to
use the artifacts from the build pipeline. Select the Source Build pipeline set
up in the previous step and finish this by clicking the Add button below.
Next step in this release pipeline is a deployment to “Test”. For this purpose we will rename the default “Stage 1” to “Test”. For this, clicking the Stage1 image (not on the link to job with task) will open a properties window. Rename Stage1 to Test and click save right top in the corner.
Now
click the link to job and task in the Test stage. Click the agent job and
change the agent pool to the pool where you added the local agent. In my
example I added the local agent to a pool named “local machine”.
Now we will
add a job to copy the publish folder to a local directory. Click on the puls
sign next to “Agent job” and search for “Copy Files”
Select The task added below Job agent and fill in the details:
Select The task added below Job agent and fill in the details:
Display name = Copy Files to: c:\drop\$(System.TeamProject)\$(Release.EnvironmentName)\$(Release.ReleaseName)\
Source Folder = $(system.defaultworkingdirectory)_WPFDatasetWithSQL-.NET Desktop-CI * This last directory name is the build pipeline name
Target Folder = c:\drop\$(System.TeamProject)\$(Release.EnvironmentName)\$(Release.ReleaseName)\
The source
folder will contain the pipeline name for the build pipeline preceded by an
underscore:
Click save
in top right hand corner.
Now we are
going to add the production stage and the required copy jobs for this stage.
Click on
releases in the left menu and click edit.
Click “Clone” in Test stage. And rename this new stage “Copy of Test” to “Production”. Click the task details and here I added System.TeamProject to the source folder name. This removes the build number from the destination name.
Next click the plus sign for the “Agent job” to add a command line script. With this command line we will first clean the install folder before we copy the new release in that location. The command line script is rd /S /Q c:\drop\$(System.TeamProject)\Install\
Last task for this job is to add a second “Copy Files” task. This task will copy the publish content in the install folder.
For the first run disable the Command line script because the folder will not yet exist. This will cause an error if the command is executed while the directory does not exist. After the first run the command can be enabled.
Last
option is to add an approval trigger on production. A test manager or a group
of testers can be allowed to approve the release after testing.
Another nice feature is to enable continuous integration and continuous deployment in Azure. For this go to the build pipeline and click the checkbox for “Enable continuous integration” in the tab “Triggers”.
Second, go to release pipeline click the continuous deployment trigger and enable continuous deployment every time a new build is available. Click save.
First two times the deployment failed. I checked the logging and fixed some typing errors.
After approving the release the install folder will be updated with the required binaries.
All done. Enjoy.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.