|
|
|
C# สอบถามเรื่่อง Thread มันกระตุกและค้างไปพักนึงอ่ะครับ |
|
|
|
|
|
|
|
รันไม่ได้ เลยไม่รู้จะแนะนำยังไงครับ ลองดูบทความนี้ ใช้ .Net เช่นเดียวกัน
Windows Phone and Thread (Silverlight)
|
|
|
|
|
Date :
2013-08-28 17:52:34 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช้ background worker จะง่ายกว่าไหม
|
|
|
|
|
Date :
2013-08-29 08:06:50 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
พอช่วงคำนวนมันยัีงช้าอยู่อ่ะ คับ แล้วแล้วมันเยอะมากมันต้องส่งไปคำนวณหลายฟังก์ชัน
มากอ่ะครับ คือแค่อยากให้หน้าจอมันไม่หยุดเฉยๆึครับ
ลองดีบั๊กดูมันค้างช่วงการเข้าฟังชันคำนวณครับคือมันทำหลายรอบเพื่อจะนำข้อมูลมาใส่ในdatagridviewทีละรอบคับ
การคำนวณ 1ครั้ง 1 ช่อง ครับ โดยใส่ใหญ่ก็จะ ประมาณ 5X8 ถึง 8X13 ครั้ง
ส่วน background worker ลองแล้วครับ มันก็ไม่ได้เหมือนกัน คับ คือรองทุกรูปแบบที่พอจะเสิทหาได้
|
|
|
|
|
Date :
2013-08-29 13:54:56 |
By :
Maikung |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
background worker + progress bar + listview
add resource
form layout
Form1.Designer.cs
namespace ProgressBar
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.components = new System.ComponentModel.Container();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.listView1 = new System.Windows.Forms.ListView();
this.button1 = new System.Windows.Forms.Button();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(12, 12);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(398, 23);
this.progressBar1.TabIndex = 0;
//
// listView1
//
this.listView1.GridLines = true;
this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.listView1.Location = new System.Drawing.Point(12, 41);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(398, 221);
this.listView1.TabIndex = 1;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// button1
//
this.button1.Location = new System.Drawing.Point(254, 268);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
this.imageList1.ImageSize = new System.Drawing.Size(20, 20);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// button2
//
this.button2.Location = new System.Drawing.Point(335, 268);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Close";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(422, 303);
this.ControlBox = false;
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listView1);
this.Controls.Add(this.progressBar1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.Button button2;
}
}
Form1.cs
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;
namespace ProgressBar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PrepareListView();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
// Start the background worker
backgroundWorker1.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
double percent = 0.0f;
// Process 1
//------------------------------------------------------------//
percent = DoProcess(EnumProcess.Process1, percent);
// Process 2
//------------------------------------------------------------//
percent = DoProcess(EnumProcess.Process2, percent);
// Process 3
//------------------------------------------------------------//
percent = DoProcess(EnumProcess.Process3, percent);
// Process 4
//------------------------------------------------------------//
percent = DoProcess(EnumProcess.Process4, percent);
// Process 5
//------------------------------------------------------------//
percent = DoProcess(EnumProcess.Process5, percent);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Report report = (Report)e.UserState;
UpdateListView(report.Process, report.Status, report.Message);
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button2.Enabled = true;
}
private double DoProcess(EnumProcess process, double percent)
{
double processPercent = 0;
int maxLoop = 10000;
backgroundWorker1.ReportProgress(Convert.ToInt16(Math.Round(percent)), new Report(process, EnumStatus.Running, "Wait..."));
System.Threading.Thread.Sleep(500);
for (int i = 1; i <= maxLoop; i++)
{
if (i % 1000 == 0)
{
processPercent += (100.0f * Convert.ToDouble(1000)) / Convert.ToDouble(maxLoop);
percent += (20.0f * Convert.ToDouble(1000)) / Convert.ToDouble(maxLoop);
System.Threading.Thread.Sleep(100);
backgroundWorker1.ReportProgress(Convert.ToInt16(Math.Round(percent)), new Report(process, EnumStatus.Running, string.Format("Running: {0}%", processPercent.ToString("0.00"))));
}
}
backgroundWorker1.ReportProgress(Convert.ToInt16(Math.Round(percent)), new Report(process, EnumStatus.Pass, "Complete"));
return percent;
}
private void UpdateListView(EnumProcess Process, EnumStatus Status, string Message)
{
progressBar1.Style = ProgressBarStyle.Blocks;
ListViewItem currentItem = listView1.Items[(int)Process];
currentItem.ImageIndex = (int)Status;
currentItem.UseItemStyleForSubItems = false;
currentItem.SubItems[2].Text = Message;
switch (Status)
{
case EnumStatus.Pass:
currentItem.SubItems[2].ForeColor = System.Drawing.Color.DarkGreen;
break;
case EnumStatus.Fail:
currentItem.SubItems[2].ForeColor = System.Drawing.Color.Red;
break;
case EnumStatus.Warning:
currentItem.SubItems[2].ForeColor = System.Drawing.Color.DarkOrange;
break;
default:
currentItem.SubItems[2].ForeColor = System.Drawing.Color.Black;
break;
}
}
private void PrepareListView()
{
// Clear ListView Column.
if (listView1.Columns.Count > 0)
listView1.Columns.Clear();
// Create columns for the items and subitems.
// Width of -2 indicates auto-size.
listView1.Columns.Add(string.Empty, 25, HorizontalAlignment.Left);
listView1.Columns.Add("Process", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Status", -2, HorizontalAlignment.Left);
// Initialize the ImageList objects with bitmaps.
imageList1.Images.Add(global::ProgressBar.Properties.Resources.Pending);
imageList1.Images.Add(global::ProgressBar.Properties.Resources.Running);
imageList1.Images.Add(global::ProgressBar.Properties.Resources.TrueSign);
imageList1.Images.Add(global::ProgressBar.Properties.Resources.FalseSign);
imageList1.Images.Add(global::ProgressBar.Properties.Resources.CautionSign);
// Assign the ImageList objects to the ListView.
listView1.SmallImageList = imageList1;
// Clear ListView Item.
if (listView1.Items.Count > 0)
listView1.Items.Clear();
string[] allProcess = new string[] {
"Process 1",
"Process 2",
"Process 3",
"Process 4",
"Process 5"
};
foreach (string process in allProcess)
{
ListViewItem item = new ListViewItem(string.Empty, 0);
item.SubItems.Add(process);
item.SubItems.Add(string.Empty);
listView1.Items.Add(item);
}
}
}
}
Report.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProgressBar
{
class Report
{
private EnumProcess _process;
private EnumStatus _status;
private string _message;
public Report(EnumProcess process, EnumStatus status, string message)
{
_process = process;
_status = status;
_message = message;
}
public EnumProcess Process { get { return _process; } }
public EnumStatus Status { get { return _status; } }
public string Message { get { return _message; } }
}
public enum EnumProcess
{
Process1 = 0,
Process2 = 1,
Process3 = 2,
Process4 = 3,
Process5 = 4
}
public enum EnumStatus
{
Pending = 0,
Running = 1,
Pass = 2,
Fail = 3,
Warning = 4
}
}
run
|
|
|
|
|
Date :
2013-08-29 16:45:20 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|