Asynchron und Controls

Hier ein Standardbeispiel für das Starten einer asynchronen Aufgabe aus einem Windows-Form und der Verwendung des Ergebisses in einem Control des Forms. Vorausgesetzt wird eine Klasse StringZahl in einem Namespace Zahl.

1. Die Form-Klasse

using System;
using System.Text;
using System.Windows.Forms;

namespace Umrechnung
{
    public partial class Form1 : Form
    {
        ASyncRechnen cThreadClass;
        private delegate void ASyncResultIsReady(Object sender,
                                    Umrechnung.ASyncRechnen.CallbackEventArgs e);
        // Initialisieren der asynchronen Funktionalität
        public Form1()
        {
            InitializeComponent();
            cboBasis.SelectedItem = “10″;
            cThreadClass = new ASyncRechnen();
            cThreadClass.ASyncResultIsReady
                += new ASyncRechnen.ASyncResultIsReadyDelegate(
                            cThreadClass_ASyncResultIsReady);

        }
        // Asynchrones Rechnen starten
        private void button1_Click(object sender, EventArgs e)
        {
            txtString.Text = “Programm rechnet !!!”;
            StringBuilder strVor    =   new StringBuilder(txtVorkomma.Text);
            StringBuilder strNach   =   new StringBuilder(txtNachkomma.Text);
            Int32 nBasis    =   Convert.ToInt32(cboBasis.SelectedItem);
            cThreadClass.AsynchLoad(strVor, strNach, nBasis);
        }
        // Ergebnis des Asynchronen Rechnens in einem Control nutzen
        void cThreadClass_ASyncResultIsReady(Object sender,
                                Umrechnung.ASyncRechnen.CallbackEventArgs e)
        {
            if (txtString.InvokeRequired)
            {
                ASyncResultIsReady cResultIsReady
                    = new ASyncResultIsReady(cThreadClass_ASyncResultIsReady);
                object[] param = new object[] { this, e };
                txtString.Invoke(cResultIsReady, param);
            }
            else
                txtString.Text = e.Result;
        }
        // Anzeige wird ungültig
        private void cboBasis_SelectedValueChanged(object sender, EventArgs e)
        {
            txtString.Text = “Basis hat sich geändert. Wert muss neu errechnet werden.”;
        }
    }
}
2. Asynchrone Funktionalität

using System;
using System.Text;
using Zahl;
using System.Windows.Forms;

namespace Umrechnung
{
    public class ASyncRechnen
    {
        public delegate String WorkerDelegat(StringBuilder p_strVor,
                                            StringBuilder p_strNach,
                                            Int32 p_nBasis);
        public delegate void ASyncResultIsReadyDelegate(Object sender,
                                            CallbackEventArgs e);
        public event ASyncResultIsReadyDelegate ASyncResultIsReady;

        public String WorkerMethod(StringBuilder p_strVor,
                                   StringBuilder p_strNach,
                                   Int32 p_nBasis)
        {
            String strRet   =   “”;
            try
            {
                StringZahl cZahl = new StringZahl(p_strVor, p_strNach, p_nBasis);
                strRet = cZahl.ZahlString.ToString();
            }
            catch (ArgumentException exc)
            {
                MessageBox.Show(exc.Message, “Fehler”);
            }
            return strRet;
        }
        public void AsynchLoad(StringBuilder p_strVor,
                               StringBuilder p_strNach,
                               Int32 p_nBasis)
        {
            WorkerDelegat workerDelegat = new WorkerDelegat(WorkerMethod);
            IAsyncResult asyncResult
                = workerDelegat.BeginInvoke(p_strVor,p_strNach,p_nBasis,
                            new AsyncCallback(CallbackHandler), workerDelegat);
        }
        public void CallbackHandler(IAsyncResult p_cResult)
        {
            WorkerDelegat workerDelegat = (WorkerDelegat)p_cResult.AsyncState;
            String strResult = workerDelegat.EndInvoke(p_cResult);
            CallbackEventArgs arg = new CallbackEventArgs(strResult);
            if (ASyncResultIsReady != null)
                ASyncResultIsReady(this, arg);
        }
        public class CallbackEventArgs : EventArgs
        {
            String strResult;
            public String Result { get { return strResult; } }
            public CallbackEventArgs(String p_strResult)
            {
                strResult = p_strResult;
            }
        }
    }
}

Einen Kommentar schreiben:


luvox period controindicazioni del femara alternative naturali al clomid clomid provoca corrimento diflucan tempo azione voltaren pastiglie senza ricetta plavix e impotenza prometrium 200 come si usa metoprolol toprol xl conversion voltaren dispers 600 voltaren retard prezzo panière allegra tupperware lasix in gravidanza voltaren tissugel voltaren gel blood sugar cosa mangiare a cipro nord viagra pepsi cola cialis 10 mg effetti voltaren generico prezzo hot propecia cinsel compare metformin to gliclazide cialis lo vendono in farmacia metoclopramide ileus glucophage chromium picolinate antibiotico augmentin sospensione clindamycin molluscum contagiosum metoprolol succinate 100 mg ter orlistat venta peru effetti collaterali di finasteride voltaren emulgel tendonitis risperdal fa male metoprolol tratament