Created
Backgroundworker ReportProgress / Tooltip balloon Messages
The following code is a partial example of using backgroundworker ReportProgress method to access controls from the backgroundworker thread.
The form has one button called Button2. When the form is activated it creates a balloon tooltip message for 5 seconds on it.
These examples will not work unless you create a winform to go along with them.
VB.NET Example
Imports System.ComponentModel
Public Class Form1
Dim bwCheckForUpdates As BackgroundWorker
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ToolTip1.SetToolTip(Button2, "An update is available.")
End Sub
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
bwCheckForUpdates = New BackgroundWorker()
bwCheckForUpdates.WorkerReportsProgress = True
AddHandler bwCheckForUpdates.ProgressChanged, AddressOf bwCheckForUpdates_ProgressChanged
AddHandler bwCheckForUpdates.DoWork, AddressOf bwCheckForUpdates_DoWork
bwCheckForUpdates.RunWorkerAsync()
End Sub
Private Sub bwCheckForUpdates_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
' Check for update
' if available call bwCheckForUpdates.ReportProgress(100, True) with value of true
bwCheckForUpdates.ReportProgress(100, True)
End Sub
Private Sub bwCheckForUpdates_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If CType(e.UserState, Boolean) = True Then
ToolTip1.Show("An update is available.", Button2, 5000)
End If
End Sub
End Class
C# Example
using System.ComponentModel;
public class Form1
{
BackgroundWorker bwCheckForUpdates;
private void Button1_Click(System.Object sender, System.EventArgs e)
{
Balloon b = new Balloon();
b.Show();
}
private void Form1_Load(System.Object sender, System.EventArgs e)
{
ToolTip1.SetToolTip(Button2, "An update is available.");
}
private void Form1_Activated(System.Object sender, System.EventArgs e)
{
bwCheckForUpdates = new BackgroundWorker();
bwCheckForUpdates.WorkerReportsProgress = true;
bwCheckForUpdates.ProgressChanged += bwCheckForUpdates_ProgressChanged;
bwCheckForUpdates.DoWork += bwCheckForUpdates_DoWork;
bwCheckForUpdates.RunWorkerAsync();
}
private void bwCheckForUpdates_DoWork(System.Object sender, System.ComponentModel.DoWorkEventArgs e)
{
// Check for update
// if available call bwCheckForUpdates.ReportProgress(100, True) with value of true
bwCheckForUpdates.ReportProgress(100, true);
}
private void bwCheckForUpdates_ProgressChanged(System.Object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
if ((bool)e.UserState == true) {
ToolTip1.Show("An update is available.", Button2, 5000);
}
}
}