1

I have a timer tick event that it's interval set to 10000

private void timer1_Tick(object sender, EventArgs e)
{
    Update();
}

In Update i have:

public int Update()
{
    counter += 1;
    int position = (int)Math.Round((counter / updateTime) * 100);
    xpProgressBar1.Text = counter.ToString() + " %";
    xpProgressBar1.Position = counter;
    if (counter == 10)
    {
        DownloadingHtml();
        ScrollNews();
        counter = 0;
    }
    return position;
}

Then in the backgroundworker do work:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    int position = NewsUpdate();
    object param = "report";
    backgroundWorker1.ReportProgress(position, param);
}

And the backgroundworker progress event:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{

    xpProgressBar1.Text = e.ProgressPercentage.ToString() + " %";
    xpProgressBar1.Position = e.ProgressPercentage;
    if (counter == 10)
    {
        DownloadingHtml();
        ScrollNews();
        counter = 0;
    }
}

What i want to do in the first step is that the Update method will be called each 10 seconds but through the backgroundworker.

In the second step i want to add to the backgroundworker another method:

public void ScrollNews()
{
    label3.Text = SaveOldHtml.HtmlLoadedFileNumber.ToString();
    richTextBox1.Clear();
    combindedString = string.Join(Environment.NewLine, ListsExtractions.myList);
    richTextBox1.SelectAll();
    richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
    richTextBox1.Text = combindedString;
    scrollerText = string.Join(Environment.NewLine, ListsExtractions.myListWithoutLinks);
    scroller1.TextToScroll = scrollerText;
    if (NewsLevels.newsLevel && NewsLevels.shouldStart)
    {
        scroller1.Start();
        NewsLevels.shouldStart = false;
    }
    if (NewsLevels.newsLevel == false && NewsLevels.shouldStart)
    {
        scroller1.Start();
        NewsLevels.shouldStart = false;
    }
    string[] rlines = richTextBox1.Lines;
    richTextBox1.SelectionStart = 0;
    richTextBox1.SelectionLength = rlines[0].Length;
    richTextBox1.SelectionColor = Color.Red;
    richTextBox1.Select(rlines[0].Length, rlines[1].Length + 1);
    richTextBox1.SelectionColor = Color.Green;
}

The ScrollNews method is being called from the Update method and it's updating richTextBox1 and Scroller1 with text.

And in the end i want to add the last method in Update:

private void DownloadingHtml()
{           
    using (var webClient = new WebClient())
    {
        webClient.Encoding = System.Text.Encoding.GetEncoding(1255);
        page = webClient.DownloadString("http://rotter.net/scoopscache.html");
    }            
    StreamWriter w = new StreamWriter(@"d:\rotterhtml\rotterscoops.html");
    w.Write(page);
    w.Close();
    page = @"d:\rotterhtml\rotterscoops.html";
    listsext.Ext(page);
    count++;
}

All this methods i want to be working from the backgroundworker.

In the form1 constructor i did that first it will call the DownloadingHtml method once then call the ScrollNews method once then activate the backgroundworker and then start the timer1.

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
Sharon Kasis
  • 113
  • 1
  • 1
  • 8

1 Answers1

0

Seems like you are misusing BackgroundWorker class. It is usually used to perform a single time-consuming action that should not block th UI. All time consuming operations should take place in OnDoWork event that is executed on a separate thread. Report progress is executed on UI thread and is used to update progress bar and other UI elements that show progress.

timer1_Tick is executed on the UI thread and blocks your UI while executing. It's not a good idea to perform any downloading or processing there if you don't want your UI to hang. You could start TPL Task, Thread or just start BackgroundWorker anew in every timer1_Tick execution. This Task or Thread can then report progress and update current UI state, calling form's thread-safe methods. BackgroundWorker can use it's own ReportProgress mechanism for this purpose.

In case of using separate Task or Thread, each method called from a separate thread should check Form's InvokeRequired and call BeginInvoke to perform thread-safe UI update. This is described well here: beginInvoke, GUI and thread and in many other similar questions.

Community
  • 1
  • 1
sasha_gud
  • 1,635
  • 13
  • 18