Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > C# สอบถามเรื่่อง Thread มันกระตุกและค้างไปพักนึงอ่ะครับ



 

C# สอบถามเรื่่อง Thread มันกระตุกและค้างไปพักนึงอ่ะครับ

 



Topic : 099705

Guest




ขอโทษครับ ขอถามเรื่อวเทรดหน่อยได้มั๊ยครับ
พอดีผมสร้างเทรดมา2เทรด ไว้สำหรับ คำนวณกับยัดลงดาตาต้ากริด และอีกอันรอการทำงานเสร็จ โดยจะโชว์โปรเกสบาร์แบบหมุนเมื่อทำเสร็จโปรเกสบาร์จะหายไป แต่ในระหว่างการทำงานมันเกิดกระค้างไปแปปนึงอะครับมันไม่หมุนแล้วสักพักก็กลับมาพร้อมการคำนวณเสร็จ

Code
Thread wait = new Thread(new ThreadStart(Wait)); Thread test = new Thread(new ThreadStart(TestThread)); test.IsBackground = true; wait.IsBackground = true; wait.Start(); test.Start(); test คือการทำงาน wait คือรอ private void TestThread() { Thread.Sleep(5000); dtg_operating.Dispatcher.Invoke( new UpdateTextCallback(this.cal_operating), new object[] { er } ); dtg_operating.Dispatcher.Invoke( new UpdateTextCallbackh(this.Finish) ); } private void Wait() { Thread.Sleep(10); dtg_operating.Dispatcher.Invoke( new UpdateTextCallback(this.UpdateText), new object[] { "false" } ); } private void Finish() { Thread.Sleep(9); dtg_operating.Dispatcher.Invoke( new UpdateTextCallback(this.UpdateText), new object[] { "true" } ); } private void UpdateText(string message) { //rere is circle progress bar if(message=="true") { rere.Visibility = Visibility.Hidden; } else if (message == "false") { rere.Visibility = Visibility.Visible; } }

ในระหว่างที่ไปทำ cal_operating มันค้างแปปนึงอะครับ หน้าจอมันfreeze



Tag : .NET, C#, VS 2010 (.NET 4.x)







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-08-28 10:42:32 By : Maikung View : 5769 Reply : 5
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

รันไม่ได้ เลยไม่รู้จะแนะนำยังไงครับ ลองดูบทความนี้ ใช้ .Net เช่นเดียวกัน

Windows Phone and Thread (Silverlight)







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-28 17:52:34 By : mr.win
 


 

No. 2

Guest


ใช้ background worker จะง่ายกว่าไหม
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-29 08:06:50 By : ห้ามตอบเกินวันละ 2 กระทู้
 

 

No. 3

Guest


พอช่วงคำนวนมันยัีงช้าอยู่อ่ะ คับ แล้วแล้วมันเยอะมากมันต้องส่งไปคำนวณหลายฟังก์ชัน
มากอ่ะครับ คือแค่อยากให้หน้าจอมันไม่หยุดเฉยๆึครับ
ลองดีบั๊กดูมันค้างช่วงการเข้าฟังชันคำนวณครับคือมันทำหลายรอบเพื่อจะนำข้อมูลมาใส่ในdatagridviewทีละรอบคับ
การคำนวณ 1ครั้ง 1 ช่อง ครับ โดยใส่ใหญ่ก็จะ ประมาณ 5X8 ถึง 8X13 ครั้ง


ส่วน background worker ลองแล้วครับ มันก็ไม่ได้เหมือนกัน คับ คือรองทุกรูปแบบที่พอจะเสิทหาได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-29 13:54:56 By : Maikung
 


 

No. 4

Guest


background worker + progress bar + listview

add resource
7

form layout
5

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

1

2

3
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-29 16:45:20 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 5



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ตอบความคิดเห็นที่ : 4 เขียนโดย : ห้ามตอบเกินวันละ 2 กระทู้ เมื่อวันที่ 2013-08-29 16:45:20
รายละเอียดของการตอบ ::
แจ่มมากครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-29 20:35:34 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# สอบถามเรื่่อง Thread มันกระตุกและค้างไปพักนึงอ่ะครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่