Autor Beitrag
nellyville
Hält's aus hier
Beiträge: 14



BeitragVerfasst: Di 13.01.09 12:28 
Hallo Leute ,

Ich bins wieder mal. Ich wollte euch fragen wo ich den irgendeine Übung z.b von einem Buch über den Backgroundworker finden kann damit ich verstehe wie der Backgroundworker in der Programmiersprache C# genau funktioniert. Ich muss eine Stoppuhr programmieren welches sich mit dem Backgroundworker raufzählt. Ich habe einige Code beispiele wie z.b der WinformThreading
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace WinFormThreading
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.CheckBox cbThreadPool;
    private System.Windows.Forms.ProgressBar progressBar1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
             this.button1 = new System.Windows.Forms.Button();
             this.textBox1 = new System.Windows.Forms.TextBox();
             this.button2 = new System.Windows.Forms.Button();
             this.cbThreadPool = new System.Windows.Forms.CheckBox();
             this.progressBar1 = new System.Windows.Forms.ProgressBar();
             this.SuspendLayout();
             // 
             // button1
             // 
             this.button1.Location = new System.Drawing.Point(2456);
             this.button1.Name = "button1";
             this.button1.Size = new System.Drawing.Size(8824);
             this.button1.TabIndex = 0;
             this.button1.Text = "Start..";
             this.button1.Click += new System.EventHandler(this.button1_Click);
             // 
             // textBox1
             // 
             this.textBox1.Location = new System.Drawing.Point(2424);
             this.textBox1.Name = "textBox1";
             this.textBox1.ReadOnly = true;
             this.textBox1.Size = new System.Drawing.Size(10020);
             this.textBox1.TabIndex = 1;
             this.textBox1.Text = "--";
             // 
             // button2
             // 
             this.button2.Location = new System.Drawing.Point(12056);
             this.button2.Name = "button2";
             this.button2.Size = new System.Drawing.Size(8824);
             this.button2.TabIndex = 1;
             this.button2.Text = "Click Me..";
             this.button2.Click += new System.EventHandler(this.button2_Click);
             // 
             // cbThreadPool
             // 
             this.cbThreadPool.Location = new System.Drawing.Point(13624);
             this.cbThreadPool.Name = "cbThreadPool";
             this.cbThreadPool.Size = new System.Drawing.Size(10424);
             this.cbThreadPool.TabIndex = 3;
             this.cbThreadPool.Text = "ThreadPool";
             // 
             // progressBar1
             // 
             this.progressBar1.Location = new System.Drawing.Point(3288);
             this.progressBar1.Name = "progressBar1";
             this.progressBar1.Size = new System.Drawing.Size(18423);
             this.progressBar1.TabIndex = 4;
             // 
             // Form1
             // 
             this.AutoScaleBaseSize = new System.Drawing.Size(513);
             this.ClientSize = new System.Drawing.Size(272118);
             this.Controls.Add(this.progressBar1);
             this.Controls.Add(this.cbThreadPool);
             this.Controls.Add(this.button2);
             this.Controls.Add(this.textBox1);
             this.Controls.Add(this.button1);
             this.Name = "Form1";
             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
             this.Text = "WinForm Threading";
             this.ResumeLayout(false);
             this.PerformLayout();

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new Form1());
    }

    delegate void ShowProgressDelegate ( int totalMessages, int messagesSoFar, bool statusDone );

    private void button1_Click(object sender, System.EventArgs e)
    {

      ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress);
      int imsgs = 100;

      //One Way... Using ThreadPool
      if ( cbThreadPool.Checked )
      {
        object obj = new object[] { this, showProgress, imsgs };
        WorkerClass wc = new WorkerClass();
        bool rc = ThreadPool.QueueUserWorkItem( new WaitCallback (wc.RunProcess), obj);
        EnableButton( ! rc );
      }
      else 
      {
        //another way.. using straight threads
        //WorkerClass wc = new WorkerClass( this, showProgress, imsgs);
        WorkerClass wc = new WorkerClass( this, showProgress, new object[] { imsgs } );
        Thread t = new Thread( new ThreadStart(wc.RunProcess));
        t.IsBackground = true//make them a daemon - prevent thread callback issues
        t.Start();
        EnableButton ( false );
      }
    }

    private void EnableButton ( bool flag )
    {
      button1.Enabled = flag;
    }
    /// <summary>
    /// Simple method that updates the text box & progress bar
    /// </summary>
    /// <param name="totalMessages"></param>
    /// <param name="messagesSoFar"></param>
    /// <param name="done"></param>
    private void ShowProgress ( int totalMessages, int messagesSoFar, bool done )
    {
      textBox1.Text = String.Format( messagesSoFar.ToString() );
      progressBar1.Value = messagesSoFar;
      if ( done ) EnableButton ( done );
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
      MessageBox.Show("You clicked me!");
    }

         


  }
}


Doch der Code ist für mich nicht verständlich und viel zu komplex , ich möchte lieber das ganze alleine in die Hand nehmen und programmieren doch dazu muss ich wissen wie der Backgroundworker funktioniert.

Freundliche Grüsse

nellyville

Moderiert von user profile iconChristian S.: C#-Tag repariert
miniC#
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 75

Wiin XP Home
C# VS Express 2008
BeitragVerfasst: Di 13.01.09 12:59 
hast du dir schon den artikel auf mdsn durchgelesen ? außerdem gab es hier im forum vor kurzem exakt die gleiche frage (stichworte : stoppuhr, threads, backgroundworker). außerdem noch zwo links zu onlinebüchern zu c#, vielleicht wirst du in denen auch noch fündig bezüglich des bw (bzw ich weis es nicht, ob sie das thema behandeln :) ).

msdn.microsoft.com/d...c8dcext2(VS.80).aspx
www.guidetocsharp.de/Default.aspx
openbook.galileocomp....de/csharp/index.htm

gruß,
minic#

_________________
Zitat MDSN : " ... C# (gesprochen: "si scharp") " :D