SpriteHand
Module Border
  A Simple Threading Sample for WindowsForms
Module Border
Location: BlogsAndy's Blog    
Posted by: host 2/1/2004 8:28 PM

You can use a separate thread to, for example, fill a long ListBox without making the user wait. You must create a delegate and use the Invoke method on the form (or a control) so that the separate thread can safely update the UI. Here is an example that fills a ListBox with all customers from a separate thread. Note that the data comes from a simple Middle-Tier component, which is beyond the scope of this post.

Imports System.Threading

 

Public Class frmThreadUI

    Inherits System.Windows.Forms.Form

    Private Sub LoadCustomers()

        ' this function will execute on a separate thread.

        Dim dsCustomers As CustomerSet = Customer.GetAll()

        ' call our threadsafe DisplayCustomers from our separate thread

        DisplayCustomers_ThreadSafe(dsCustomers)

    End Sub

 

    Private Sub DisplayCustomers_ThreadSafe(ByVal dsCustomers As CustomerSet)

        ' this is a threadsafe method called from the separate worker thread

        Dim oDel As New DisplayCustomersDelegate(AddressOf Me.DisplayCustomers)

        Dim args() As DataSet = {dsCustomers}

        Me.Invoke(oDel, args)

    End Sub

    ' define a delegate (signature) for the callback.

    Delegate Sub DisplayCustomersDelegate(ByVal dsCustomers As CustomerSet)

 

    Private Sub DisplayCustomers(ByVal dsCustomers As CustomerSet)

        ' display the customer list in the ListBox

        lstCustomers.DataSource = dsCustomers.Tables(0)

        lstCustomers.DisplayMember = "CompanyName"

        lstCustomers.ValueMember = "CustomerId"

    End Sub

 

    Private Sub frmThreadUI_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' give a "loading" message.

        lstCustomers.Items.Add("Loading... please wait.")

 

        ' kick off a separate thread to load the Customers into a ListBox

        Dim oTS As New ThreadStart(AddressOf LoadCustomers)

        Dim oThread As Thread = New Thread(oTS)

        oThread.Start()

    End Sub

End Class

Permalink |  Trackback

Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Blog_List
Module Border
Module Border Module Border
Module Border
  Subscribe
Module Border

RSS

Module Border Module Border
Module Border
  Diversions
Module Border

DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game was submitted as an entry to the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

INFO AND CODE



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2008 andy.beaulieu.com - Login