Chủ đề: Cross-thread operation not valid
-
27-03-2015, 06:05 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Cross-thread operation not valid
Mình đang làm một chương trình kiểm tra đường truyền (ping) giống cái cmd/ping của win, mình đã tham khảo chương trình này http://www.java2s.com/Code/CSharp/Network/AdvancedPingProgram.htm và mình có chạy thử trên vs2003 thì chạy ngon lành cành đào, nhưng mình làm trên vs2008 thì bị lỗi Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on. Mình đã xem bài của bạn sunflower ở bài http://diendan.congdongcviet.com/threads/t5794::xu-ly-loi-cross-thread-cross-thread-operation-not-valid.cpp và đã sửa thành công nhưng đến lúc chạy thì ô listbox ko hiện lên kết quả. Chú ý phần tớ làm trên vs2008 phải chờ lâu lâu 1 tý nhé.
File ping là file tớ làm trên vs2008 còn file ping_test là tớ thử trên 2003.View more random threads:
- Tạo hiệu ứng Icon lớn cho chương trình với WPF
- Teach yourself Regular Expression in two minutes
- Nhập, thêm, sửa, xóa dữ liệu trên DataGridView cơ bản nhất
- Chương trình tạo hiệu ứng tuyết rơi với lập trình C#
- Hook keyboard và mouse hook trong c# không cần code
- Windowsform | Dòng chữ chạy (không thay đổi location)
- Xin hỏi về Invoke trong C#
- Những phương thuốc trắng đa từ bài bác thuốc dân phòng
- Ngôn ngữ lập trình c: chỉnh sửa file nhị phân
- Mã hóa chuỗi kết nối trong web.config
-
30-03-2015, 10:58 AM #2Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Mình cũng đang research về vấn đề thay đổi giá trị dynamic trên control. Bạn có thể dựa vào demo bên dưới để sửa lại ví dụ của bạn. Nếu có thắc mắc về việc áp dụng demo này qua ví dụ của bạn thì post lên đây để cùng thảo luận.
Form 1:
Mã:delegate void ShowProgressDelegate(int totalMessages, int messagesSoFar, bool statusDone); private void button2_Click(object sender, EventArgs e) { ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress); int imsgs = 100; 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(); } private void ShowProgress(int totalMessages, int messagesSoFar, bool done) { this.dataGridView1.Rows.Add(messagesSoFar.ToString()); this.dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Selected = true; dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1; }
Mã:/// <summary> /// Summary description for WorkerClass. /// </summary> public class WorkerClass { /// <summary> /// Usually a form or a winform control that implements "Invoke/BeginInvode" /// </summary> ContainerControl m_sender = null; /// <summary> /// Messages sent in - this is implementation specific /// </summary> int m_totalMessages = 0; /// <summary> /// The delegate method (callback) on the sender to call /// </summary> Delegate m_senderDelegate = null; /// <summary> /// Constructor used by caller using ThreadPool /// </summary> public WorkerClass() { } /// <summary> /// Constructor called by calle using ThreadPool OR ThreadStart /// </summary> /// <param name="sender"></param> /// <param name="totalMessages"></param> /// <param name="sp"></param> public WorkerClass ( ContainerControl sender, Delegate senderDelegate, int totalMessages) { m_sender = sender; m_senderDelegate = senderDelegate; m_totalMessages = totalMessages; } /// <summary> /// Another constructor using the params array pattern. Used by ThreadPool or ThreadStart /// </summary> /// <param name="sender"></param> /// <param name="senderDelegate"></param> /// <param name="list"></param> public WorkerClass ( ContainerControl sender, Delegate senderDelegate, params object[] list) { m_sender = sender; m_senderDelegate = senderDelegate; m_totalMessages = (int) list.GetValue(0); } /// <summary> /// Method for ThreadStart delegate /// </summary> public void RunProcess() { Thread.CurrentThread.IsBackground = true; //make them a daemon LocalRunProcess(); } /// <summary> /// Local Method for the actual work. /// </summary> private void LocalRunProcess() { int i = 0; for ( ; i < m_totalMessages; i++) { Thread.Sleep(50); m_sender.BeginInvoke( m_senderDelegate, new object[] { m_totalMessages, i, false } ); } m_sender.BeginInvoke( m_senderDelegate, new object[] { m_totalMessages, i, true } ); }
-
30-03-2015, 06:42 PM #3Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
bạn có thể gửi code dể mình chạy thử đc ko ? Mình dùng vs2008 nhé.
-
31-03-2015, 03:14 PM #4Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Code đó rồi bạn. Tạo thêm 1 Button và 1 datagridview nữa là run được thôi.
-
31-03-2015, 06:23 PM #5Junior Member
- Ngày tham gia
- Dec 2015
- Bài viết
- 0
The type or namespace name 'ContainerControl' could not be found (are you missing a using directive or an assembly reference?)
mình thử và nó báo bị lỗi này bạn ơi, bên class ý
- - - Nội dung đã được cập nhật ngày 31-03-2015 lúc 11:23 PM - - -
Exception has been thrown by the target of an invocation
mình sửa đc rồi nhưng giờ lại thành lỗi này, bạn gửi code mình chạy luôn cho nhanh.
-
01-04-2015, 04:29 PM #6Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Đây bạn, dựa vào rồi edit lại cho project của bạn thôi
-
01-04-2015, 05:07 PM #7Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Gửi bởi hoangthi
P/s: cám ơn bạn hvcuongit, mình sẽ nghiên cứu và thử
-
01-04-2015, 05:25 PM #8Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Gửi bởi nobita611Mã:public static class ThreadHelperClass { delegate void SetTextCallback(Form f, ListBox ctrl, string text); public static void AddText(Form form, ListBox ctrl, string text) { if (ctrl.InvokeRequired) { SetTextCallback d = new SetTextCallback(AddText); form.Invoke(d, new object[] { form, ctrl, text }); } else { ctrl.Items.Add(text); } } }
Mã:lbKetqua.Items.Add("Ping to " + txtPing.Text + "[" + r.Address.ToString() + "]" + " Successful" //+ " Response delay = " + r.RoundtripTime.ToString() + " ms" + " ");
Mã:string sdfs = "Ping to " + txtPing.Text + "[" + r.Address.ToString() + "]" + " Successful" + " Response delay = " + r.RoundtripTime.ToString() + " ms" + " "; ThreadHelperClass.AddText(this,lbKetqua,sdfs);
Mã:lbKetqua.Items.Add("Request time out");
Mã:ThreadHelperClass.AddText(this, lbKetqua, "Request time out");
-
02-04-2015, 03:38 PM #9Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Đã edit lại project theo yêu câu của bạn, mình chỉ sửa lại code để chạy cho đúng kết quả thôi, bạn tối ưu lại nó nhé.
-
02-04-2015, 04:47 PM #10Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
ko chạy đc bạn ơi, mình dùng vs2008, cậu dùng vs2015 ah.
Với sự đa dạng và phong phú về mẫu mã và giá cả, choxeonline.vn hứa hẹn sẽ là địa chỉ tin cậy cho những ai đang tìm kiếm chiếc xe ô tô ưng ý. Từ những mẫu xe hạng sang đến những chiếc xe gia đình...
Khám Phá Đa Dạng Sản Phẩm Xe Ô Tô...