If you need to perform a time consuming operation and need to show the result as soon as it is ready you can use that approach. Replace or remove BarcodeReader.BarcodeReader with your own object.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace BarcodeReaderAsync
{
public delegate string ReadBarcode();
public delegate void ShowResultsCallBack();
public partial class Form1 : Form
{
private BarcodeReader.BarcodeReader bcr = new BarcodeReader.BarcodeReader();
public Form1()
{
InitializeComponent();
}
private void btnRead_Click(object sender, EventArgs e)
{
lblResults.Text = "Reading...";
ReadBarcode b = new ReadBarcode(Reading);
IAsyncResult iftAR = b.BeginInvoke(new AsyncCallback(ReadingDone),null);
}
private void ReadingDone(IAsyncResult itfAR)
{
AsyncResult ar = (AsyncResult)itfAR;
ReadBarcode b = (ReadBarcode)ar.AsyncDelegate;
//return b.EndInvoke(itfAR);
ShowResults();
}
private void ShowResults()
{
if (lblResults.InvokeRequired)
{
ShowResultsCallBack b = new ShowResultsCallBack(ShowResults);
Invoke(b);
}
else
{
lblResults.Text = "Done";
}
}
static string Reading()
{
BarcodeReader.BarcodeReader br = new BarcodeReader.BarcodeReader();
br.BeginRead();
int i=0;
while (br.Status == "BUSY")
{
i++;
System.Diagnostics.Debug.Print("Reading " + i);
}
return br.Status;
}
private void btnReset_Click(object sender, EventArgs e)
{
lblResults.Text = string.Empty;
}
}
}