Search for objects in runtime

1

I have an application that will make simultaneous downloads of different data sources. In it I have DataGridView that will receive the result of a select and from the quantity of lines that come from that result I will have the same amount of TabPages . Each TabPage will have a Textbox to record the progress of the downloads. Downloads are done in periods, which are also variable, depending on the download setting. That said, I have the following situation ... As every time I run the query, I mount new objects and even create Timer and BackgroundWorker to perform all actions.

To avoid creating too many tabs, I'm traversing TabControl in a foreach and using TabPage.Dispose() to free up application resources. I just could not find a way to do the same with Timer and BackgroundWorker . How could I find the two to "kill" them at runtime?

  

Update: Object creation code

    private void createLogColTab(int pCodigoCol,
                                    string pTipoCol)
    {
        TabPage tbpNew = new TabPage();
        tbpNew.Tag = pCodigoCol.ToString();
        tbpNew.Name = "tbpLogCol" + clsGeneric.TruncateLongString(pCodigoCol.ToString(), 3, "R", "0");
        tbpNew.Text = pTipoCol;
        tbpNew.Height = panel1.Height;
        tbpNew.Width = panel1.Width;

        TextBox txtResult = new TextBox();
        txtResult.Name = "txtLogCol" + clsGeneric.TruncateLongString(pCodigoCol.ToString(), 3, "R", "0");
        txtResult.Multiline = true;
        txtResult.Height = tbpNew.Height;
        txtResult.Width = tbpNew.Width;
        txtResult.Text = tbpNew.Name;
        txtResult.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
        txtResult.Tag = pCodigoCol.ToString();

        Timer tmrNew = new Timer();
        tmrNew.Tag = pCodigoCol.ToString();

        BackgroundWorker bgwNew = new BackgroundWorker();
        bgwNew.WorkerReportsProgress = true;
        bgwNew.DoWork += bgwMain_DoWork;
        bgwNew.ProgressChanged += bgwMain_ProgressChanged;
        bgwNew.RunWorkerCompleted += bgwMain_RunWorkerCompleted;

        tbcLogColeta.TabPages.Add(tbpNew);
    }
    
asked by anonymous 19.10.2016 / 21:16

1 answer

2

Timer has a Elapsed event ( Timer.Elapsed ) in which you can check (with a global variable) whether this timer still needs to be executed. If you do not need it, you disable it.

In the BackgroundWorker case, it has the WorkerSupportsCancellation property:

bw.WorkerSupportsCancellation = true;

Where you can cancel the execution:

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

    for (int i = 1; (i <= 10); i++)
    {
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            break;
        }
        else
        {
            // Perform a time consuming operation and report progress.
            System.Threading.Thread.Sleep(500);
            worker.ReportProgress((i * 10));
        }
    }
}

The links below have examples of this use:

link

link

But in both cases, the worker is declared global. Could you do that in your case? Declare a list (or a dictionary) of workers globally?

EDIT

As we talked about the global dictionary:

// Dicionários:
private Dictionary<string, Timer> _allTimers = new Dictionary<string, Timer>();
private Dictionary<string, BackgroundWorker> _allWorkers = new Dictionary<string, BackgroundWorker>();

private void createLogColTab(int pCodigoCol,
                                string pTipoCol)
{
    TabPage tbpNew = new TabPage();
    tbpNew.Tag = pCodigoCol.ToString();
    tbpNew.Name = "tbpLogCol" + clsGeneric.TruncateLongString(pCodigoCol.ToString(), 3, "R", "0");
    tbpNew.Text = pTipoCol;
    tbpNew.Height = panel1.Height;
    tbpNew.Width = panel1.Width;

    TextBox txtResult = new TextBox();
    txtResult.Name = "txtLogCol" + clsGeneric.TruncateLongString(pCodigoCol.ToString(), 3, "R", "0");
    txtResult.Multiline = true;
    txtResult.Height = tbpNew.Height;
    txtResult.Width = tbpNew.Width;
    txtResult.Text = tbpNew.Name;
    txtResult.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
    txtResult.Tag = pCodigoCol.ToString();

    Timer tmrNew = new Timer();
    tmrNew.Tag = pCodigoCol.ToString();

    // Adicionar ao dicionário:
    _allTimers.Add(pCodigoCol.ToString(), tmrNew);

    BackgroundWorker bgwNew = new BackgroundWorker();
    bgwNew.WorkerReportsProgress = true;
    bgwNew.DoWork += bgwMain_DoWork;
    bgwNew.ProgressChanged += bgwMain_ProgressChanged;
    bgwNew.RunWorkerCompleted += bgwMain_RunWorkerCompleted;

    // Adicionar ao dicionário:
    _allWorkers.Add(pCodigoCol.ToString(), bgwNew);

    tbcLogColeta.TabPages.Add(tbpNew);
}
    
19.10.2016 / 21:27