同步问题 [英] synchronization problem

查看:69
本文介绍了同步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我创建了一个简单的桌面表单,用于将一些数据从dataGridView导入到sql server数据库中,并且为了方便起见,我在表单上放置了一个progressBar,然后为ProgressBar启动了两个线程,为save方法()启动了另一个线程,我开始导入约800条记录.
问题是,当我开始以正常方式运行此表单时,保存该数据大约需要3分钟,但是当我在工作时最小化该表单时,最多需要20秒才能保存我的数据.
因此在普通表单模式下,保存数据的速度太慢了,但是当表单不是活动表单或最小化表单时,它变得非常快.

代码示例:

hey guys,
I created a simple desktop form to import some data from dataGridView to sql server database, and to make it easy I put a progressBar on my form, then I started two threads on for the progressBar and other one for my save method( ),then I started to import about 800 records.
the problem is when I start to run this form in the normal way, it took about 3 minutes to save this data, but when I minimize the form while it working, it took like 20 seconds maximum to save my data.
so in the normal form mode it''s so slow to save my data,but when the form is not the active form or minimized it become very fast.

code sample:

System.Threading.Thread t1;
System.Threading.Thread proth;
int thcount = 0;
under the button:
void btnExport_Click(object sender, EventArgs e)
      {
       thcount = 0;
       progressBar1.Value = 0;
       progressBar1.Maximum = grdList.Rows.Count;
//save method
       t1 = new System.Threading.Thread(Save);
       t1.Start();
//progress bar method
       proth = new Thread(startProgress);
        proth.Start();      
      }



请帮助

谢谢每一位.



please help

thanks for every one

推荐答案

您只需要一个线程.该线程将保存一条记录,然后将进度报告回至更新进度条的表单.

实际上,我认为您可以通过创建BackgroundWorker对象而不是线程来使自己更轻松,因为它已经具有以下功能:a)发送进度事件,并且b)可以轻松取消.

这是描述如何从线程(包括BackgroundWorker对象)更新UI的链接:

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/17/91685.aspx [ ^ ]

您看到的处理时间的差异是因为在最小化窗口时线程不必更新UI.



编辑====================

如果您遇到性能问题,则必须精疲力尽,并使用手头的工具来查找/减轻阻塞点.尝试使用Trace语句,然后让程序运行.跟踪语句将文本发送到Visual Studio的输出窗口,您可以观察发生了什么以及何时发生.这将进一步减慢您的处理速度,但至少您将能够看到正在发生的事情.

You only need one thread. This thread would save a record and then report progress back to the form which would update the progress bar.

In fact, I think you can make it easier on yourself by creating a BackgroundWorker object instead of a thread because it already has the ability to a) send progress events, and b) can be easily canceled.

Here''s a link that describes how to update the UI from a thread (including BackgroundWorker objects):

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/17/91685.aspx[^]

The difference in processing time you''re seeings is because the thread doesn''t have to update the UI when the window is minimized.



EDIT ======================

If you''re having performance issues, you''re going to have to just muscle through it and use the tools on hand to find/mitigate the choke points. Try using Trace statements and just let the program run. The trace statements will send text to the output window in Visual Studio, and you can observe what''s happening and when. This will slow down your process even more, but at least you''ll be able to watch what''s happening.


这篇关于同步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆