Click here to Skip to main content
15,878,970 members
Home / Discussions / C#
   

C#

 
GeneralRe: How do you protect your app hosting third party dll ...? Pin
Eddy Vluggen5-Mar-11 3:02
professionalEddy Vluggen5-Mar-11 3:02 
GeneralRe: How do you protect your app hosting third party dll ...? Pin
devvvy5-Mar-11 14:24
devvvy5-Mar-11 14:24 
GeneralRe: How do you protect your app hosting third party dll ...? Pin
Eddy Vluggen7-Mar-11 0:41
professionalEddy Vluggen7-Mar-11 0:41 
GeneralRe: How do you protect your app hosting third party dll ...? Pin
devvvy7-Mar-11 14:27
devvvy7-Mar-11 14:27 
QuestionUpdate progress bar on another form Pin
Etienne_1232-Mar-11 21:28
Etienne_1232-Mar-11 21:28 
AnswerRe: Update progress bar on another form Pin
musefan2-Mar-11 22:29
musefan2-Mar-11 22:29 
GeneralRe: Update progress bar on another form Pin
Etienne_1233-Mar-11 21:17
Etienne_1233-Mar-11 21:17 
AnswerRe: Update progress bar on another form Pin
DaveyM693-Mar-11 1:13
professionalDaveyM693-Mar-11 1:13 
This can be done by creating custom events on your form that has the ProgressBar and wraps a BackgroundWorker.

I have my own custom class that I use for this which I am intending to post in an article real soon but here is the code if it helps - it is using a custom progress bar that has a Marquee property rather than Style, but the calls to this can easily be altered to use the standard one. The worker part of this code is essentially the same as the .NET BackgroundWorker and I have extended the ProgressChangedEventArgs so I can pass two lots of text in addition to the usual.

C#
using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace DaveyM69.Windows.Forms
{
    [ToolboxBitmap(typeof(Form))]
    public class ProgressForm : Form
    {
        public const string NoMessageChange = null;
        public const int NoProgressChange = -1;

        public event DoWorkEventHandler DoWork;
        public event ProgressChangedEventHandler ProgressChanged;
        public event RunWorkerCompletedEventHandler RunWorkerCompleted;

        private object argument;
        private AsyncOperation asyncOperation;
        private Button button;
        private bool cancellationPending;
        private readonly SendOrPostCallback completed;
        private bool isBusy;
        private Label label;
        private readonly SendOrPostCallback progress;
        private ProgressBar progressBar;
        private readonly ParameterizedThreadStart work;
        private bool workerReportsProgress;
        private bool workerSupportsCancellation;

        public ProgressForm()
        {
            InitializeComponent();
            argument = null;
            asyncOperation = null;
            completed = new SendOrPostCallback(Completed);
            isBusy = false;
            progress = new SendOrPostCallback(Progress);
            work = new ParameterizedThreadStart(Work);
            workerReportsProgress = true;
            workerSupportsCancellation = true;
        }

        [Browsable(false)]
        public object Argument
        {
            get { return argument; }
            set
            {
                if (argument != value)
                {
                    if (!isBusy)
                        argument = value;
                }
            }
        }
        [Browsable(false)]
        public bool CancellationPending
        {
            get { return cancellationPending; }
        }
        [Browsable(false)]
        public bool IsBusy
        {
            get { return isBusy; }
        }
        [DefaultValue(true)]
        public bool WorkerReportsProgress
        {
            get { return workerReportsProgress; }
            set
            {
                if (workerReportsProgress != value)
                {
                    workerReportsProgress = value;
                    SetWorkerReportsProgress();
                }
            }
        }
        [DefaultValue(true)]
        public bool WorkerSupportsCancellation
        {
            get { return workerSupportsCancellation; }
            set
            {
                if (workerSupportsCancellation != value)
                {
                    workerSupportsCancellation = value;
                    SetWorkerSupportsCancellation();
                }
            }
        }

        public void CancelAsync()
        {
            if (isBusy)
            {
                button.Enabled = false;
                cancellationPending = true;
            }
        }
        private void Completed(object state)
        {
            asyncOperation = null;
            cancellationPending = false;
            isBusy = false;
            button.Enabled = false;
            RunWorkerCompletedEventArgs e = (RunWorkerCompletedEventArgs)state;
            OnRunWorkerCompleted(e);
            if (Modal)
                DialogResult = e.Cancelled ?
                    DialogResult.Cancel : e.Error == null ?
                        DialogResult.OK : DialogResult.Abort;
            else
                Close();
        }
        private void InitializeComponent()
        {
            this.label = new System.Windows.Forms.Label();
            this.progressBar = new DaveyM69.Windows.Forms.ProgressBar();
            this.button = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label
            // 
            this.label.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.label.AutoEllipsis = true;
            this.label.Location = new System.Drawing.Point(12, 9);
            this.label.Name = "label";
            this.label.Size = new System.Drawing.Size(200, 13);
            this.label.TabIndex = 0;
            this.label.Text = "Working...";
            // 
            // progressBar
            // 
            this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.progressBar.Location = new System.Drawing.Point(12, 25);
            this.progressBar.Marquee = false;
            this.progressBar.Name = "progressBar";
            this.progressBar.Size = new System.Drawing.Size(200, 23);
            this.progressBar.TabIndex = 1;
            this.progressBar.Text = "0%";
            this.progressBar.Value = 0;
            // 
            // button
            // 
            this.button.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.button.Location = new System.Drawing.Point(137, 54);
            this.button.Name = "button";
            this.button.Size = new System.Drawing.Size(75, 23);
            this.button.TabIndex = 2;
            this.button.Text = "&Cancel";
            this.button.UseVisualStyleBackColor = true;
            // 
            // ProgressForm
            // 
            this.AcceptButton = this.button;
            this.ClientSize = new System.Drawing.Size(224, 89);
            this.ControlBox = false;
            this.Controls.Add(this.button);
            this.Controls.Add(this.progressBar);
            this.Controls.Add(this.label);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "ProgressForm";
            this.ShowInTaskbar = false;
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Progress";
            this.ResumeLayout(false);

        }
        protected virtual void OnDoWork(DoWorkEventArgs e)
        {
            DoWorkEventHandler eh = DoWork;
            if (eh != null)
                eh(this, e);
        }
        protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
        {
            ProgressChangedEventHandler eh = ProgressChanged;
            if (eh != null)
                eh(this, e);
        }
        protected virtual void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        {
            RunWorkerCompletedEventHandler eh = RunWorkerCompleted;
            if (eh != null)
                eh(this, e);
        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            RunWorkerAsync();
        }
        private void Progress(object state)
        {
            DaveyM69.ComponentModel.ProgressChangedEventArgs e =
                (DaveyM69.ComponentModel.ProgressChangedEventArgs)state;
            if (e.TaskName != NoMessageChange)
                Text = e.TaskName;
            if (e.OperationName != NoMessageChange)
                label.Text = e.OperationName;
            if (workerReportsProgress && e.ProgressPercentage != NoProgressChange)
            {
                progressBar.Value = e.ProgressPercentage;
                OnProgressChanged(e);
            }
        }
        public void ReportProgress(int progressPercentage)
        {
            ReportProgress(
                NoMessageChange, NoMessageChange, progressPercentage, null);
        }
        public void ReportProgress(string taskName, string operationName)
        {
            ReportProgress(taskName, operationName, NoProgressChange, null);
        }
        public void ReportProgress(string taskName, string operationName,
            int progressPercentage, object userState)
        {
            DaveyM69.ComponentModel.ProgressChangedEventArgs state =
                new ComponentModel.ProgressChangedEventArgs(
                taskName, operationName, progressPercentage, userState);
            if (asyncOperation == null)
                progress(state);
            else
                asyncOperation.Post(progress, state);
        }
        private void RunWorkerAsync()
        {
            if (!isBusy)
            {
                isBusy = true;
                button.Enabled = workerSupportsCancellation;
                asyncOperation = AsyncOperationManager.CreateOperation(null);
                work.BeginInvoke(argument, null, null);
            }
        }
        private void SetWorkerReportsProgress()
        {
            if (InvokeRequired)
                new MethodInvoker(SetWorkerReportsProgress).Invoke();
            else
            {
                progressBar.Marquee = !workerReportsProgress;
            }
        }
        private void SetWorkerSupportsCancellation()
        {
            if (InvokeRequired)
                new MethodInvoker(SetWorkerSupportsCancellation).Invoke();
            else
            {
                if (isBusy && !cancellationPending)
                    button.Enabled = workerSupportsCancellation;
                else
                    button.Enabled = false;
            }
        }
        private void Work(object obj)
        {
            bool cancelled = false;
            Exception error = null;
            object result = null;
            DoWorkEventArgs doWorkEventArgs = new DoWorkEventArgs(obj);
            try
            {
                OnDoWork(doWorkEventArgs);
                if (doWorkEventArgs.Cancel)
                    cancelled = true;
                else
                    result = doWorkEventArgs.Result;
            }
            catch (Exception ex)
            {
                error = ex;
            }
            RunWorkerCompletedEventArgs state = new RunWorkerCompletedEventArgs(
                result, error, cancelled);
            asyncOperation.PostOperationCompleted(completed, state);
        }
    }
}

C#
namespace DaveyM69.ComponentModel
{
    public class ProgressChangedEventArgs : System.ComponentModel.ProgressChangedEventArgs
    {
        private string operationName;
        private string taskName;

        public ProgressChangedEventArgs(
            string taskName, string operationName,
            int progressPercentage, object userState)
            : base(progressPercentage, userState)
        {
            this.taskName = taskName;
            this.operationName = operationName;
        }

        public string OperationName
        {
            get { return operationName; }
        }
        public string TaskName
        {
            get { return taskName; }
        }
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: Update progress bar on another form Pin
Luc Pattyn3-Mar-11 1:36
sitebuilderLuc Pattyn3-Mar-11 1:36 
GeneralRe: Update progress bar on another form Pin
DaveyM693-Mar-11 3:58
professionalDaveyM693-Mar-11 3:58 
GeneralRe: Update progress bar on another form Pin
Etienne_1233-Mar-11 20:00
Etienne_1233-Mar-11 20:00 
QuestionPausing execution for certain amount of time without freezing the form. Pin
john1234512-Mar-11 14:48
john1234512-Mar-11 14:48 
AnswerRe: Pausing execution for certain amount of time without freezing the form. Pin
Luc Pattyn2-Mar-11 15:31
sitebuilderLuc Pattyn2-Mar-11 15:31 
AnswerRe: Pausing execution for certain amount of time without freezing the form. Pin
OriginalGriff2-Mar-11 21:19
mveOriginalGriff2-Mar-11 21:19 
GeneralRe: Pausing execution for certain amount of time without freezing the form. Pin
Luc Pattyn3-Mar-11 0:53
sitebuilderLuc Pattyn3-Mar-11 0:53 
AnswerRe: Pausing execution for certain amount of time without freezing the form. Pin
musefan2-Mar-11 22:20
musefan2-Mar-11 22:20 
QuestionConsole in c# 2008 Express Pin
Bob Pawley2-Mar-11 12:47
Bob Pawley2-Mar-11 12:47 
AnswerRe: Console in c# 2008 Express Pin
Dave Kreskowiak2-Mar-11 12:59
mveDave Kreskowiak2-Mar-11 12:59 
AnswerRe: Console in c# 2008 Express Pin
DaveyM692-Mar-11 13:32
professionalDaveyM692-Mar-11 13:32 
AnswerRe: Console in c# 2008 Express Pin
I Believe In GOD2-Mar-11 21:16
I Believe In GOD2-Mar-11 21:16 
QuestionHow to select a radio button in internet explorer. Pin
sososm2-Mar-11 11:46
sososm2-Mar-11 11:46 
QuestionLastinputinfo() not working on my machine ... Pin
turbosupramk32-Mar-11 10:51
turbosupramk32-Mar-11 10:51 
AnswerRe: Lastinputinfo() not working on my machine ... Pin
Eddy Vluggen2-Mar-11 11:08
professionalEddy Vluggen2-Mar-11 11:08 
AnswerRe: Lastinputinfo() not working on my machine ... [modified] Pin
DaveyM692-Mar-11 12:11
professionalDaveyM692-Mar-11 12:11 
GeneralRe: Lastinputinfo() not working on my machine ... Pin
turbosupramk33-Mar-11 6:08
turbosupramk33-Mar-11 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.