Posts


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);
        }
    }
   
}


MessageDialog m = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.YesNo, false, 
    "Should the textbox be set to Hello World");
ResponseType result = (ResponseType)m.Run();
m.Destroy();
		
if (result == ResponseType.Yes)
{
	entry1.Text = "Hello World";
}

Cocaine Cowgirl, O Holy Night, and I Don’t want to miss a Thing are progressing nicely.


Book List: Precalculus - It has been seven years since I have done math, so I will start with this. Calculus Made Easy Applied Cryptography PostgreSQL - Finish few remaining chapters Data Structures and Problem Solving Using Java Programming Linux Games Bookkeeping and Accounting Nemsis

Guitar: 10 new songs that I will be learning are: Everlong - foo fighters Cocaine Cowgirls - Matt Mays I don’t want to miss a thing - aerosmith 1979 - smashing pumpkins (I do believe I used to know this one) Killer in me - smashing pumpkins Heart of Gold - Neil Young O Holy Night …..


I am reading through a chapter on binary trees. The first simple example they give is traversing through directory structures. As the book is for java the attached file is the example translated to c#.

Once I have finished the chapter I will upload the binary tree example translated to c#.

Here is an example postorder recursion through a directory tree (attachment main.cs). See attachment Main.cs for a different example.

using System.IO;

class RecurseDirectory
{
	public static void Main(string[] args)
	{
		RecurseDirectory r = new RecurseDirectory();
		RecurseDirectory.PrintDirsRecursively(new System.IO.DirectoryInfo(@"\\Directory\Location"), 0);

		Console.Read();
	}
	
	
	public static void PrintDirsRecursively(System.IO.DirectoryInfo source, int depth) 
	{
		foreach (System.IO.DirectoryInfo dir in source.GetDirectories())
		{
			PrintTabs(depth);
			System.Console.WriteLine(source.FullName);
			PrintDirsRecursively(dir, depth+1);
		}
		foreach (System.IO.FileInfo file in source.GetFiles())
		{
			PrintTabs(depth);
			System.Console.WriteLine(file.FullName);
		}
	}


	private static void PrintTabs(int depth)
	{
		for (int j=0; j < depth; j++)
		{
			System.Console.Write("\t");
		}
	}
}

Here is a simple recursive directory/file copy function.

using System.IO;

public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) 
{
    foreach (DirectoryInfo dir in source.GetDirectories())
    {
        CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
    }
    foreach (FileInfo file in source.GetFiles())
    {
        file.CopyTo(Path.Combine(target.FullName, file.Name), true); //overwrite
    }
}