Click here to Skip to main content
15,891,473 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to change the button's value in Datagrid Pin
Henry Minute6-Oct-09 15:04
Henry Minute6-Oct-09 15:04 
QuestionHow do I pin an object in C#? [modified] - FIXED Pin
Richard Andrew x646-Oct-09 11:07
professionalRichard Andrew x646-Oct-09 11:07 
AnswerRe: How do I pin an object in C#? Pin
DaveyM696-Oct-09 11:35
professionalDaveyM696-Oct-09 11:35 
QuestionBackgroundworker disposal Pin
vsaratkar6-Oct-09 10:49
vsaratkar6-Oct-09 10:49 
AnswerRe: Backgroundworker disposal Pin
Christian Graus6-Oct-09 10:52
protectorChristian Graus6-Oct-09 10:52 
AnswerRe: Backgroundworker disposal Pin
DaveyM696-Oct-09 11:42
professionalDaveyM696-Oct-09 11:42 
GeneralRe: Backgroundworker disposal Pin
vsaratkar7-Oct-09 3:36
vsaratkar7-Oct-09 3:36 
GeneralRe: Backgroundworker disposal Pin
DaveyM697-Oct-09 8:57
professionalDaveyM697-Oct-09 8:57 
Personally, I would use three background workers here, one for each upload type and wrap them all in one 'super' background worker class that exposes the required events and methods.

This is a very rough mock up with a lot of gaps to fill but the theory should be OK.
C#
public class Uploader
{
    public event EventHandler<EventArgs<UploadProgress>> ProgressChanged;
    public event EventHandler<EventArgs<RunWorkerCompletedEventArgs[]>> Completed;

    private BackgroundWorker backGroundWorkerData;
    private BackgroundWorker backGroundWorkerFile;
    private BackgroundWorker backGroundWorkerPhoto;

    private RunWorkerCompletedEventArgs[] result;

    public void RunAsync()
    {
        result = new RunWorkerCompletedEventArgs[3];

        backGroundWorkerData = new BackgroundWorker();
        backGroundWorkerData.WorkerReportsProgress = true;
        backGroundWorkerData.WorkerSupportsCancellation = true;
        backGroundWorkerData.DoWork += new DoWorkEventHandler(backGroundWorkerData_DoWork);
        backGroundWorkerData.ProgressChanged += new ProgressChangedEventHandler(backGroundWorkerData_ProgressChanged);
        backGroundWorkerData.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backGroundWorkerData_RunWorkerCompleted);
        backGroundWorkerData.RunWorkerAsync();
        // and the same for the other workers
    }

    // These three methods will need implementing for all workers.
    void backGroundWorkerData_DoWork(object sender, DoWorkEventArgs e)
    {
        // Do upload stuff here plus check backGroundWorkerData.CancellationPending
    }
    void backGroundWorkerData_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        OnProgressChanged(
            new EventArgs<UploadProgress>(
                new UploadProgress(DataType.Data, e.ProgressPercentage)));
    }
    void backGroundWorkerData_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        result[0] = e;
        backGroundWorkerData = null;
        CheckCompleted();
    }

    public void CancelAsync()
    {
        backGroundWorkerData.CancelAsync();
        backGroundWorkerFile.CancelAsync();
        backGroundWorkerPhoto.CancelAsync();
    }
    private void CheckCompleted()
    {
        if (backGroundWorkerData == null &&
            backGroundWorkerFile == null &&
            backGroundWorkerPhoto == null)
            OnCompleted(new EventArgs<RunWorkerCompletedEventArgs[]>(result));
    }
    protected virtual void OnProgressChanged(EventArgs<UploadProgress> e)
    {
        EventHandler<EventArgs<UploadProgress>> eh = ProgressChanged;
        if (eh != null)
            eh(this, e);
    }
    protected virtual void OnCompleted(EventArgs<RunWorkerCompletedEventArgs[]> e)
    {
        EventHandler<EventArgs<RunWorkerCompletedEventArgs[]>> eh = Completed;
        if (eh != null)
            eh(this, e);
    }
}
public class UploadProgress
{
    public UploadProgress(DataType dataType, int progress)
    {
        DataType = dataType;
        Progress = progress;
    }

    public DataType DataType { get; private set; }
    public int Progress { get; private set; }
}
public enum DataType
{
    Data = 0,
    File = 1,
    Photo = 2
}
public class EventArgs<T> : EventArgs
{
    public EventArgs(T value)
    {
        Value = value;
    }

    public T Value { get; private set; }
}


Dave

Generic BackgroundWorker - My latest article!
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

GeneralRe: Backgroundworker disposal Pin
vsaratkar8-Oct-09 2:54
vsaratkar8-Oct-09 2:54 
GeneralRe: Backgroundworker disposal Pin
vsaratkar9-Oct-09 10:17
vsaratkar9-Oct-09 10:17 
GeneralRe: Backgroundworker disposal Pin
DaveyM6910-Oct-09 3:25
professionalDaveyM6910-Oct-09 3:25 
Question.NET Runtime 2.0 Error Event ID: 5000 Pin
PrasannaKulkarni6-Oct-09 10:47
PrasannaKulkarni6-Oct-09 10:47 
AnswerRe: .NET Runtime 2.0 Error Event ID: 5000 Pin
Christian Graus6-Oct-09 10:50
protectorChristian Graus6-Oct-09 10:50 
QuestionC# grapihcs Pin
nosheen awan6-Oct-09 7:26
nosheen awan6-Oct-09 7:26 
AnswerRe: C# grapihcs Pin
Eddy Vluggen6-Oct-09 8:05
professionalEddy Vluggen6-Oct-09 8:05 
QuestionMedia Player -XML playlist [modified] Pin
eawedat6-Oct-09 6:35
eawedat6-Oct-09 6:35 
AnswerRe: Media Player -XML playlist Pin
EliottA6-Oct-09 7:19
EliottA6-Oct-09 7:19 
GeneralRe: Media Player -XML playlist Pin
eawedat6-Oct-09 7:24
eawedat6-Oct-09 7:24 
GeneralRe: Media Player -XML playlist Pin
EliottA6-Oct-09 7:29
EliottA6-Oct-09 7:29 
GeneralRe: Media Player -XML playlist Pin
eawedat6-Oct-09 7:38
eawedat6-Oct-09 7:38 
GeneralRe: Media Player -XML playlist Pin
eawedat6-Oct-09 7:45
eawedat6-Oct-09 7:45 
GeneralRe: Media Player -XML playlist Pin
Dave Kreskowiak6-Oct-09 8:16
mveDave Kreskowiak6-Oct-09 8:16 
GeneralRe: Media Player -XML playlist Pin
eawedat6-Oct-09 8:21
eawedat6-Oct-09 8:21 
GeneralRe: Media Player -XML playlist Pin
Mirko19806-Oct-09 21:44
Mirko19806-Oct-09 21:44 
GeneralRe: Media Player -XML playlist Pin
Richard MacCutchan6-Oct-09 10:51
mveRichard MacCutchan6-Oct-09 10:51 

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.