1

My Winforms app needs to launch one or more non-modal Report Viewer windows, after which the main window goes about its business(which includes opening modal dialogs). I found the code below (by H. Passant) in an earlier article:

private void button1_Click(object sender, EventArgs e) {
    var t = new System.Threading.Thread(() => Application.Run(new Form2()));
    t.SetApartmentState(System.Threading.ApartmentState.STA);
    t.Start();
}

In my case, "Form2" contains a single docked ReportViewer control set up to display the desired report [new frmRptView(sReport, aRptParams)].

All seemed to work well during my testing, but when I gave a build to testers, they reported the viewer windows would sometimes lock up, and the only way to get rid of them was to log out or reboot. We had the same trouble using form.Show(), too.

edit: What is the correct way to launch a non-modal window so it will be independent of the main app window?

Community
  • 1
  • 1
kaborka
  • 11
  • 2
  • There's probably not enough information to diagnose the issue. Could you provide more details? – jww Feb 12 '14 at 02:40
  • Why is this not sufficient to open new window in a regular manner? – T.S. Feb 12 '14 at 04:07
  • I tried using form.Show() at first, but those windows became unresponsive, too. I will rephrase the question. – kaborka Feb 12 '14 at 20:23

1 Answers1

0

If all you need is to show a non-modal window which does not have a longer lifespan than the main window, just create an instance of your form and show it from your button click handler. This is simple and a lot safer than creating multiple UI threads.

private void button1_Click(object sender, EventArgs e) 
{
   (new Form2()).Show();
}
Rahul Misra
  • 561
  • 1
  • 7
  • 15
  • That's what I was using before trying the threaded approach. Users kept reporting the viewer windows becoming unresponsive. I thought that might be due to the modal forms used by the main window, so I tried the MT approach. I've never been able to reproduce the behavior. Any ideas what could be causing the non-modal viewier windows to lock up? – kaborka Feb 13 '14 at 20:27
  • Check if there is something in the viewer windows which is leaking resources or causing a spike in usage. Are all connections to query the report closed once the data is retrieved. As it is the report viewer windows which are becoming unresponsive, the problem needs to be investigated inside that control. – Rahul Misra Feb 14 '14 at 10:12