Compact Framework & Threads

realbora

Mitglied
Hallo,

ich versuche z.Zt. eine CF-Anwendung zu programmieren, die beim Start verschiedene Einstellungen über einen Webservice aus einer DB lädt. Vor diesem Prozess(Thread) soll ein anderer Thread gestartet werden, der den Verlauf des Ladevorgangs mittels einer ProgressBar anzeigt. Mittels Invoke und den dazugehörigen Events versuche ich nun die Threads zu steuern, was aber bis jetzt leider nicht zum Erfolg geführt hat.

Vielleicht kann mir ja hier jemand helfen?

Danke.

Code:
        public Form_KeKa(Mitarbeiter arg1, Tisch arg2 ,WebserviceKeKa arg3)
        {
            this.m_Mitarbeiter = arg1;
            this.m_Tisch = arg2;
            this.m_WebserviceKeKa = arg3;

            //Event-Handler
            this.Resize += new EventHandler(this.Form_Resize);

            //Methoden
            InitializeComponent();

            Thread t1 = new Thread(new ThreadStart(OnShowProgressBar));
            t1.Start();
            Thread t2 = new Thread(new ThreadStart(OnFillProgressBar));
            t2.Start();

            Initialize_Panel_Main();

            //Attribute
            //SHFullScreenHelper.ShowSIPButton(this, false); 

            //Controlls
            this.Controls.Add(this.m_Panel_Main);
            Keyboard k = new Keyboard(this);
            this.Controls.Add(k);
        }

        delegate void ShowProgressBar();
        private void OnShowProgressBar()
        {
            if (this.s_Form_Progress.InvokeRequired)
            {
                this.s_Form_Progress.Invoke(new ShowProgressBar(OnShowProgressBar));
                return;
            }
            this.s_Form_Progress.ShowDialog();
        }

        delegate void FillProgressBar();
        private void OnFillProgressBar()
        {
            if (this.s_Form_Progress.InvokeRequired)
            {
                this.s_Form_Progress.Invoke(new FillProgressBar(OnFillProgressBar));
                return;
            }
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(20);
                OnUpdateProgressBar(i);
            }
            OnFinished(null, null);
        }

        delegate void UpdateProgressBar(int i);
        private void OnUpdateProgressBar(int i)
        {
            if (s_Form_Progress.InvokeRequired)
            {
                s_Form_Progress.Invoke(new UpdateProgressBar(OnUpdateProgressBar), new object[] { i });
                return;
            }
            if (i < s_Form_Progress.ProgressBar.Maximum)
            {
                s_Form_Progress.ProgressBar.Value = i;
            }
        }

        delegate void FinishedProgressBar(object sender, EventArgs e);
        private void OnFinished(object sender, EventArgs e)
        {
            if (s_Form_Progress.ProgressBar.InvokeRequired)
            {
                s_Form_Progress.Invoke(new FinishedProgressBar(OnFinished));
                return;
            }
            s_Form_Progress.Close();
        }
...
 
Habe eine Lösung gefunden.

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TestThread
{
    public partial class Form1 : Form
    {
        private Form_Progress m_Form_Progress = new Form_Progress();
        Thread t1;
        Thread t2;
        Thread t3;
        Thread t4;

        public Form1()
        {
            Application.DoEvents();
            t1 = new Thread(new ThreadStart(OnShowProgressBar));
            t1.Start();

            t2 = new Thread(new ThreadStart(OnFillProgressBar));
            t2.Start();

            t3 = new Thread(new ThreadStart(Worker));
            t3.Start();

            t4 = new Thread(new ThreadStart(OnInit));
            t4.Start();
        }

        private void Worker()
        {
            for (int i = 0; i < 50000; i++)
            {
                int x = new Random().Next() + i;
                //double y = Math.Pow(x,Math.Cos(x)) * Math.Pow(x, Math.Sin(x));
            }
            System.Diagnostics.Debug.WriteLine("Ende");
            t3.Abort();
        }

        delegate void Init();
        private void OnInit()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Init(OnInit)); 
                return;
            }
            InitializeComponent();
            t4.Abort();
        }


        delegate void ShowProgressBar();
        private void OnShowProgressBar()
        {
            if (this.m_Form_Progress.InvokeRequired)
            {
                this.m_Form_Progress.Invoke(new ShowProgressBar(OnShowProgressBar));
                return;
            }

            this.m_Form_Progress.ShowDialog();
            t1.Abort();
        }

        delegate void FillProgressBar();
        private void OnFillProgressBar()
        {
            if (this.m_Form_Progress.InvokeRequired)
            {
                this.m_Form_Progress.Invoke(new FillProgressBar(OnFillProgressBar));
                return;
            }
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(20);
                OnUpdateProgressBar(i);
            }
            OnFinished(null, null);
            t2.Abort();
        }

        delegate void UpdateProgressBar(int i);
        private void OnUpdateProgressBar(int i)
        {
            if (this.m_Form_Progress.InvokeRequired)
            {
                this.m_Form_Progress.Invoke(new UpdateProgressBar(OnUpdateProgressBar), new object[] { i });
                return;
            }
            if (i < this.m_Form_Progress.ProgressBar.Maximum)
            {
                this.m_Form_Progress.ProgressBar.Value = i;
            }
        }

        delegate void FinishedProgressBar(object sender, EventArgs e);
        private void OnFinished(object sender, EventArgs e)
        {
            if (this.m_Form_Progress.ProgressBar.InvokeRequired)
            {
                this.m_Form_Progress.Invoke(new FinishedProgressBar(OnFinished));
                return;
            }
            this.m_Form_Progress.Close();
        }

    }
}
 

Neue Beiträge

Zurück