1

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();
    }
whytheq
  • 34,466
  • 65
  • 172
  • 267

3 Answers3

1

You can call p.WaitForExit() on the process, but don't do that on the main thread (ie in a buttonClick);

You'll want something like:

//untested
private void btRunProcessAndRefresh_Click(object sender, EventArgs e)
{ 
    var p = Process.Start(@"\\fileserve\department$\ReportScheduler_v3.exe", "12"); 
    p.Exited += ProcessExited;
    p.EnableRaisingEvents = true;
    //InitializeGridView(); 
}     

void ProcessExited(object sender, EventArgs e)
{
    InitializeGridView();    
}
H H
  • 263,252
  • 30
  • 330
  • 514
  • p.Exited = ProcessExited; says can only exist on left side of += or -= – whytheq Mar 14 '12 at 12:49
  • I see this example of using exited but tricky for a newbie : http://127.0.0.1:47873/help/1-7768/ms.help?product=VS&productVersion=100&method=f1&query=System.Diagnostics.Process.Exited&locale=en-US&category=DevLang%3acsharp%00TargetFrameworkMoniker%3a.NETFramework,Version%3Dv4.0 – whytheq Mar 14 '12 at 12:50
  • ok - now the winform is destroyed before I can see if the DGV got refreshed - I'll amend above code to what I'm using. – whytheq Mar 14 '12 at 13:14
1

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:

  1. Make available Dispatcher.CurrentDispatcher as _currentDispatcher.
  2. Start the Process in a seperate Thread and have it WaitForExit() there.
  3. 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";
}
Till
  • 3,084
  • 17
  • 18
0

You can call WaitForExit on the return value of Process.Start.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • thanks Nico - I've edited the code supplied in Henk's post to what seems to work; should have just amended the code in the question post really but I'm a newbie so hopefully forgiven – whytheq Mar 14 '12 at 13:43