问题描述
我有一个表单上的按钮启动它,后面的x(变化)的过程秒钟,改变了一个数据库表Y.调用InitializeGridView(),然后刷新DGV这说明前面提到的表Y.一些数据问题是,InitializeGridView()这样的过程中所做的更改不会反映此过程之前完成。我如何InitializeGridView等待进程完成?
私人无效btRunProcessAndRefresh_Click(对象发件人,EventArgs五){
的Process.Start(@\\fileserve\department $ \ReportScheduler_v3.exe,12);
InitializeGridView();
}
修改
我结束了以下内容。现在的问题是,如果这一过程需要10分钟,然后应用程序被冻结10分钟。我想我需要学习如何为多线程!
私人无效btRunReport_Click(对象发件人,EventArgs五){
进程p =新工艺();
p.StartInfo.FileName = @\\fileserve\department $ \ReportScheduler_v3.exe
p.StartInfo.Arguments =12;
p.Start();
p.WaitForExit();
InitializeGridView();
}
一个后续的问题是关闭所以这里是我的答案的副本:
如果您需要InitialilzeGridView()方法来运行过程完成之后:
- 请使用
Dispatcher.CurrentDispatcher
作为_currentDispatcher。 - 在一个单独的启动进程线程和有它
WaitForExit()
那里。 - 纷纷跟帖打电话给你的
InitializeGridview()
通过方法_currentDispatcher.BeginInvoke
。
下面是一些代码来让你去:
请注意:您将需要通过项目的添加引用对话框添加引用WindowsBase
$ b $。 b
使用系统;使用System.Diagnostics程序
;
使用的System.Threading;使用System.Windows.Forms的
;
使用System.Windows.Threading程序;
私人只读调度_currentDispatcher = Dispatcher.CurrentDispatcher;
私人委托无效ReportingSchedulerFinishedDelegate();
私人无效btRunReport_Click(对象发件人,EventArgs五)
{
btRunReport.Enabled = FALSE;
btRunReport.Text =处理..;
变种线程=新主题(RunReportScheduler);
thread.Start();
}
私人无效InitializeGridView()
{
//无论你需要做
}
私人无效RunReportScheduler()
{
进程p =新工艺();
p.StartInfo.FileName = @\\fileserve\department $ \ReportScheduler_v3.exe
p.StartInfo.Arguments =12;
p.Start();
p.WaitForExit();
_currentDispatcher.BeginInvoke(新ReportingSchedulerFinishedDelegate(ReportingSchedulerFinished),DispatcherPriority.Normal);
}
私人无效ReportingSchedulerFinished()
{
InitializeGridView();
btRunReport.Enabled = TRUE;
btRunReport.Text =开始;
}
I've got a button on a form that starts a process which, after x (varies) seconds, changes some data in a database table Y. The call InitializeGridView() then refreshes the DGV which shows previously mentioned table Y. Problem is that InitializeGridView() finishes before the process so the changes made by the process are not reflected. How do I make InitializeGridView wait for the process to complete?
private void btRunProcessAndRefresh_Click(object sender, EventArgs e){
Process.Start(@"\\fileserve\department$\ReportScheduler_v3.exe", "12");
InitializeGridView();
}
EDIT
I ended up with the following. Problem now is that if the process takes 10 minutes then the app is frozen for 10minutes. Think I need to learn how to multi thread!
private void btRunReport_Click(object sender, EventArgs e){
Process p = new Process();
p.StartInfo.FileName = @"\\fileserve\department$\ReportScheduler_v3.exe";
p.StartInfo.Arguments = "12";
p.Start();
p.WaitForExit();
InitializeGridView();
}
A follow-up question was closed so here's a copy of my answer:
If you need your InitialilzeGridView() Method to run after the process has finished:
- Make available
Dispatcher.CurrentDispatcher
as _currentDispatcher. - Start the Process in a seperate Thread and have it
WaitForExit()
there. - Have the Thread call your
InitializeGridview()
Method via_currentDispatcher.BeginInvoke
.
Here's some code to get you going:
Note: You will need to add a Reference to WindowsBase via the Add Reference dialog of your project.
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Threading;
private readonly Dispatcher _currentDispatcher = Dispatcher.CurrentDispatcher;
private delegate void ReportingSchedulerFinishedDelegate();
private void btRunReport_Click(object sender, EventArgs e)
{
btRunReport.Enabled = false;
btRunReport.Text = "Processing..";
var thread = new Thread(RunReportScheduler);
thread.Start();
}
private void InitializeGridView()
{
// Whatever you need to do here
}
private void RunReportScheduler()
{
Process p = new Process();
p.StartInfo.FileName = @"\\fileserve\department$\ReportScheduler_v3.exe";
p.StartInfo.Arguments = "12";
p.Start();
p.WaitForExit();
_currentDispatcher.BeginInvoke(new ReportingSchedulerFinishedDelegate(ReportingSchedulerFinished), DispatcherPriority.Normal);
}
private void ReportingSchedulerFinished()
{
InitializeGridView();
btRunReport.Enabled = true;
btRunReport.Text = "Start";
}
这篇关于计时问题 - DGV刷新过程中修改了之前的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!