BUY Declomycin ONLINE WITHOUT PRESCRIPTION

July 3rd, 2009

BUY Declomycin ONLINE WITHOUT PRESCRIPTION, I can’t count the number of times someone has asked me about running a time consuming task on a separate thread, but at the same time show a progress dialog with up-to-the-second percentage updates being displayed to the user. Multithreading can be confusing at first, but if you just take it one step at a time, it really isn’t all that bad. I am going to show you how to create a multithreaded application that shows a progress dialog which shows real time progress to the user.

First lets start with how to get started with multithreading in WPF. If you don’t want to read how this actually works and just want to get the source and start playing ,here is the Source Code.

Your WPF application may need to perform intensive tasks that consume large amounts of time. Executing these tasks on a separate thread allows you to maintain the responsiveness of the UI while the task is performed, BUY Declomycin ONLINE WITHOUT PRESCRIPTION. Enter the BackgroundWorker. The BackgroundWorker is the recommended way to run time consuming tasks on a separate, dedicated thread, leaving the UI responsive.

Running a Background Process

The RunWorkerAsync method starts the execution of the background process by raising the DoWork event. Australia, uk, us, usa, The code in the DoWork event handler is executed on a separate thread.

int maxRecords = 1000;

 

BackgroundWorker worker = new BackgroundWorker();

 

worker.DoWork += delegate(object s, DoWorkEventArgs args)

{

    for (int x = 1; x < maxRecords; x++)

    {

        System.Threading.Thread.Sleep(10);

    }

};

 

worker.RunWorkerAsync();

Providing Parameters to the Process

Your background process may required one or more parameters, such as the address of a file to download. You can provide a parameter in the RunWorkerAsync method that will be available as the Argument property in the DoWork event handler.

BUY Declomycin ONLINE WITHOUT PRESCRIPTION,
BackgroundWorker worker = new BackgroundWorker();

 

worker.DoWork += delegate(object s, DoWorkEventArgs args)

{

    string path = (string)args.Argument;

    //do something

};

 

worker.RunWorkerAsync("c:\\myFile.txt");

Returning a Value from the Process

You might want to return a value from a background process, such as a result from a calculation. You can return a value by setting the Result property of the DoWorkEventArgs in the DoWork event handler. Then, this value can be retrieved from the Result property of the RunWorkerCompletedEventArgs parameter int the RunWorkerCompleted event handler.

BackgroundWorker worker = new BackgroundWorker();

 

worker.DoWork += delegate(object s, order Declomycin online c.o.d, DoWorkEventArgs args)

{

    args.Result = CalculationMethod();

};

 

worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)

{

    object result = args.Result;

};

 

worker.RunWorkerAsync();

Cancelling the Background Process

You may want to allow the user to cancel a long running process. In order to support this you must first set the WorkerSupportsCancellation to true. You call the CancelAsync mtehod to attempt to cancel the process. When you call the CancelAsync method it sets the CancellationPending property of the BackgroundWorker to true, BUY Declomycin ONLINE WITHOUT PRESCRIPTION. Then you must check for the value of the CancellationPending property in the DoWork event handler, and if it is true, set the Cancel property of the DoWorkEventArgs parameter to true.

int maxRecords = 1000;

 

BackgroundWorker worker = new BackgroundWorker();

worker.WorkerSupportsCancellation = true;

 

worker.DoWork += delegate(object s, DoWorkEventArgs args)

{

    for (int x = 1; x < maxRecords; x++)

    {

        //check if there is a cancelat

        if (worker.CancellationPending)

        {

            args.Cancel = true;

            return;

        }

 

        System.Threading.Thread.Sleep(10);

    }

};

 

worker.RunWorkerAsync();

Reporting the Progress of a Background Process

You can report the progress of a background process back to the primary thread by calling the ReportProgress method. This method raises the ProgressChanged event and allows you to pass a parameter that indicated the percentage of progress that has been completed. Make sure you set the WorkerReportsProgess property of the BackgroundWorker to true.

int maxRecords = 1000;

 

BackgroundWorker worker = new BackgroundWorker();

worker.WorkerReportsProgress = true;

 

worker.DoWork += delegate(object s, DoWorkEventArgs args)

{

    for (int x = 1; x < maxRecords; x++)

    {

        System.Threading.Thread.Sleep(10);

        worker.ReportProgress(Convert.ToInt32(((decimal)x / (decimal)maxRecords) * 100));

    }

};

 

worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)

{

    int percentage = args.ProgressPercentage;

};

 

worker.RunWorkerAsync();

Using the Dispatcher to Access Controls on Another Thread

At times, you may want to change the user interface from a worker thread. For example, you may want to enable or disable buttons, or show a modal ProgressBar that provides more detailed progress information than is allowed by the ReportProgress method. BUY Declomycin ONLINE WITHOUT PRESCRIPTION, The WPF threading model provides the Dispatcher class for cross thread calls. By using the Dispatcher, where to buy Declomycin, you can safely update your user interface from background worker threads.

You can get a reference to the Dispacther object for a UI element from its Dispatcher property.

System.Windows.Threading.Dispatcher aDisp = Button1.Dispatcher;

Dispatcher provides two main methods that you will use; Invoke and BeginInvoke. Both methods allows you to call a method safely on the UI thread. The BeginInvoke method allows you to call a method asynchronously, and the Invoke method allows you to call a method synchronously.

Putting It All Together

Lets say I have a time consuming task in my application.  I want to execute this time consuming task on a background thread, but I also want to show a modal progress dialog that shows a message and the percent of the completed task. I also want to allow the user to cancel the process at anytime, BUY Declomycin ONLINE WITHOUT PRESCRIPTION. So the first thing I need to do is create my progress dialog window that has a label to display my percent completed, a progress bar to graphically show the progress, and a cancel button so the user can cancel the process.

<Window x:Class="MultiThreading.ProgressDialog"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="ProgressDialog" Height="115" Width="300" WindowStyle="ToolWindow" WindowStartupLocation="CenterOwner">

    <Grid>

        <StackPanel>

            <Label x:Name="lblProgress" Content="0%"/>

            <ProgressBar x:Name="progress" Height="25" IsIndeterminate="False"></ProgressBar>

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">

                <Button x:Name="btnCancel" Click="btnCancel_Click">Cancel</Button>

            </StackPanel>

        </StackPanel>

    </Grid>

</Window>

I need to expose two properties; ProgressText to set my label, and ProgressValue to set the progress bar value. I also need to expose an event so when the user hits the cancel button the process will stop.

   1: public string ProgressText

   2: {

   3:     set

   4:     {

   5:         this.lblProgress.Content = value;

   6:     }

   7: }

   8:  

   9: public int ProgressValue

  10: {

  11:     set

  12:     {

  13:         this.progress.Value = value;

  14:     }

  15: }

  16:  

  17: public event EventHandler Cancel = delegate { };

  18:  

  19: private void btnCancel_Click(object sender, RoutedEventArgs e)

  20: {

  21:     Cancel(sender, e);

  22: }

Now on my main application I need a button to execute this long running process and show my progress dialog.

<Button Height="23" Name="btnDispacther" Click="btnDispacther_Click">Using Dispatcher</Button>

Now lets code the background worker.

//our bg worker

BackgroundWorker worker;

//our progress dialog window

ProgressDialog pd;

 

private void btnDispacther_Click(object sender, RoutedEventArgs e)

{

    int maxRecords = 1000;

 

    pd = new ProgressDialog();

 

    //hook into the cancel event

    pd.Cancel += CancelProcess;

 

    //get our dispatcher

    System.Windows.Threading.Dispatcher pdDispatcher = pd.Dispatcher;

 

    //create our background worker and support cancellation

    worker = new BackgroundWorker();

    worker.WorkerSupportsCancellation = true;

 

    worker.DoWork += delegate(object s, DoWorkEventArgs args)

    {

        for (int x = 1; x < maxRecords; x++)

        {

            if (worker.CancellationPending)

            {

                args.Cancel = true;

                return;

            }

 

            System.Threading.Thread.Sleep(10);

 

            //create a new delegate for updating our progress text

            UpdateProgressDelegate update = new UpdateProgressDelegate(UpdateProgressText);

 

            //invoke the dispatcher and pass the percentage and max record count

            pdDispatcher.BeginInvoke(update, Convert.ToInt32(((decimal)x / (decimal)maxRecords) * 100), maxRecords);

        }

    };

 

    worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)

    {

        pd.Close();

    };

 

    //run the process then show the progress dialog

    worker.RunWorkerAsync();

    pd.ShowDialog();

}

 

//our delegate used for updating the UI

public delegate void UpdateProgressDelegate(int percentage, int recordCount);

 

//this is the method that the deleagte will execute

public void UpdateProgressText(int percentage, int recordCount)

{

    //set our progress dialog text and value

    pd.ProgressText = string.Format("{0}% of {1} Records", percentage.ToString(), recordCount);

    pd.ProgressValue = percentage;

}

 

void CancelProcess(object sender, EventArgs e)

{

    //cancel the process

    worker.CancelAsync();

}

That’s it. See, I told you it wasn’t that bad. Now that you have a basic understanding of multithreading, try to see how creative you can get on implementing it in your WPF application.

.

Similar posts: BUY Naltrexone ONLINE WITHOUT PRESCRIPTION. BUY Yaz (Crisanta LS) ONLINE WITHOUT PRESCRIPTION. Japan, craiglist, ebay, overseas, paypal. Where can i buy Pilex online.
Trackbacks from: BUY Declomycin ONLINE WITHOUT PRESCRIPTION. BUY Declomycin ONLINE WITHOUT PRESCRIPTION. Order Declomycin no prescription. Declomycin over the counter. Buy Declomycin without prescription.

  • Tracy

    The download does not appear to be available?

  • Blake

    Great article, this is exactly what I need for a bulk reporting tool i am creating. However I’ve having trouble converting this anonymous methods to vb.

    Any help on converting the below and relating function to vb would be much appreciated.

    worker.DoWork = delegate(object s, DoWorkEventArgs args){

    }

  • http://brianlagunas.com Brian Lagunas

    @Blake

    Since VB doesn’t have anonymous delegates just use regular event handlers

  • Blake

    Brian,
    I guessed that was the case but wasn’t 100% sure. So thanks for confirming that, I had a play with it last night and got it working.

    I declared a withevents private variable at page level and used the regular event handlers as you mentioned.

    Thanks for your reply.

  • Nigama

    Hi Brian,

    Very Nice article, very clearly explained!!!

  • Nigama

    Hi Brian,

    I need your ideas to proceed with BackGroundWorker threads for my requirement.

    I have a C# Windows Application that processes a directory/sub-directories containing huge number of .xml files, say 100,000 files and perfoms time-consuming I/O operation such as parsing xml files(I am currently querying those files using Linq) while updating the user of every file being processed. In fact, I have 3 such I/O operations to be performed on the same set of files(so all the 3 tasks will basically share the same resources/directory/files for their own independent requirements).

    Also, I currently have a Windows Form associated with 3 different class files, one for each I/O task. I need to implement multithreading so that I can run all the 3 tasks at the same time, while updating the UI controls such as Label- one per task/thread to keep the user informed about the background process.

    What is the best way of proceeding about such kind of task in a single application?(if possible..). Kindly suggest me.

    Thanks in advance

  • http://brianlagunas.com Brian Lagunas

    @Nigama

    Well one thing you will definitely have to watch out for is your files being locked by one thread while a different thread is trying to access it too. What I am trying to say is that if you have three I/O tasks to perform on a file, you can only do one I/O operation at a time. You cannot write to a file at the same time you try to read from a file. So I would recommend three seperate background workers. When the first I/O operation completes, in the worker.RunWorkerCompleted start your next operation and so forth.

  • http://www.dad.net.au Denis

    Fantastic article. It was exactly what I was looking for. Nice and concise. Thanks!

  • Garcia_juan_j

    Very nice example, it is simple to understand :) ! thanks brian!

  • JB