freeze when launching form from c# dll

  • Thread starter Thread starter John Dolinka
  • Start date Start date
J

John Dolinka

When I launch a form from a c# dll (part of the c# project), after I do a
show the launched form freezes. If the dll host launches the same form from
an exposed public method in the c# dll project then no problems.
Is there anyway I can launch the windows form that is part of my c# project
with out it "freezing".
Thanks,

John Dolinka
 
* "John Dolinka said:
When I launch a form from a c# dll (part of the c# project), after I do a
show the launched form freezes. If the dll host launches the same form from
an exposed public method in the c# dll project then no problems.
Is there anyway I can launch the windows form that is part of my c# project
with out it "freezing".

I thought the public method resides in the C# DLL? Please post some
code.
 
Thanks, for repsonding;

I now know the cause of the problem but not the solution. I am using
Process in System.Diagnostics to launch an application. On it's exit I
launch the Window form and then display it, it is displayed and then
freezes. Code follows;

The class library that launches the form...
using System;
using System.Threading;
using System.Diagnostics;

namespace LaunchWindowFromDllMain
{
public class Class1
{
static Form1 _form1;
private Process _process=null;
public Class1()
{
}
public void TestActivateWindow(){
_process = new Process();
_process.StartInfo.FileName = "notepad.exe";
_process.StartInfo.Arguments= "";
_process.StartInfo.WorkingDirectory="";


_process.Disposed += new EventHandler(DisposedHandler);
_process.Exited += new EventHandler(ExitHandler);

_process.EnableRaisingEvents = true;

_process.Start();

}
private void DisposedHandler(object sender, EventArgs e) {
Console.WriteLine("Disposed...");
}
private void ExitHandler(object sender, EventArgs e) {

_process.Dispose();
_process=null;

Console.WriteLine("Exited...");

_form1=new Form1();
_form1.Show(); <--after form is displayed it is frozen------------
}

}
}
---the form that is launched in class library dll----

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace LaunchWindowFromDllMain
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private bool _toggle=false;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
#endregion

private void button1_Click(object sender, System.EventArgs e) {
if(_toggle==true)
this.textBox1.BackColor=System.Drawing.Color.LightBlue;
else
this.textBox1.BackColor=System.Drawing.Color.Red;

_toggle=!_toggle;
}
}
}
----The main aapplication that launches the classLibrary dll------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace LaunchWindowFromDllMain
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
private Class1 _Class1=null;
public Form1()
{
InitializeComponent();
_Class1=new Class1();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e) {
_Class1.TestActivateWindow();
}
}
}
 
I think this is because the ExitHandler is being invoked from a background
threadpool thread. Instead you should invoke a method back on the main
uithread that creates and shows the form. So add a static form variable that
holds a reference to your main app's form and use that to marshal a call to
create and show your other from.
e.g.
private void ExitHandler(object sender, EventArgs e)
{
_process.Dispose();
_process=null;

Console.WriteLine("Exited...");

//Form _form1=new Form1();
//_form1.Show(); //<--after form is displayed it is frozen------------

// this is the static property that points to your main form on the ui
thread
mainForm.Invoke( new MethodInvoker(this.ShowForm) );
}

private void ShowForm()
{
_form1=new Form1();
_form1.Show();
}


John Dolinka said:
Thanks, for repsonding;

I now know the cause of the problem but not the solution. I am using
Process in System.Diagnostics to launch an application. On it's exit I
launch the Window form and then display it, it is displayed and then
freezes. Code follows;

The class library that launches the form...
using System;
using System.Threading;
using System.Diagnostics;

namespace LaunchWindowFromDllMain
{
public class Class1
{
static Form1 _form1;
private Process _process=null;
public Class1()
{
}
public void TestActivateWindow(){
_process = new Process();
_process.StartInfo.FileName = "notepad.exe";
_process.StartInfo.Arguments= "";
_process.StartInfo.WorkingDirectory="";


_process.Disposed += new EventHandler(DisposedHandler);
_process.Exited += new EventHandler(ExitHandler);

_process.EnableRaisingEvents = true;

_process.Start();

}
private void DisposedHandler(object sender, EventArgs e) {
Console.WriteLine("Disposed...");
}
private void ExitHandler(object sender, EventArgs e) {

_process.Dispose();
_process=null;

Console.WriteLine("Exited...");

_form1=new Form1();
_form1.Show(); <--after form is displayed it is frozen------------
}

}
}
---the form that is launched in class library dll----

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace LaunchWindowFromDllMain
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private bool _toggle=false;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
#endregion

private void button1_Click(object sender, System.EventArgs e) {
if(_toggle==true)
this.textBox1.BackColor=System.Drawing.Color.LightBlue;
else
this.textBox1.BackColor=System.Drawing.Color.Red;

_toggle=!_toggle;
}
}
}
----The main aapplication that launches the classLibrary dll------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace LaunchWindowFromDllMain
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
private Class1 _Class1=null;
public Form1()
{
InitializeComponent();
_Class1=new Class1();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e) {
_Class1.TestActivateWindow();
}
}
}


do
form
 
Thanks so very much, that did the trick.

John Dolinka

Andrew (Infragistics) said:
I think this is because the ExitHandler is being invoked from a background
threadpool thread. Instead you should invoke a method back on the main
uithread that creates and shows the form. So add a static form variable that
holds a reference to your main app's form and use that to marshal a call to
create and show your other from.
e.g.
private void ExitHandler(object sender, EventArgs e)
{
_process.Dispose();
_process=null;

Console.WriteLine("Exited...");

//Form _form1=new Form1();
//_form1.Show(); //<--after form is displayed it is frozen------------

// this is the static property that points to your main form on the ui
thread
mainForm.Invoke( new MethodInvoker(this.ShowForm) );
}

private void ShowForm()
{
_form1=new Form1();
_form1.Show();
}


John Dolinka said:
Thanks, for repsonding;

I now know the cause of the problem but not the solution. I am using
Process in System.Diagnostics to launch an application. On it's exit I
launch the Window form and then display it, it is displayed and then
freezes. Code follows;

The class library that launches the form...
using System;
using System.Threading;
using System.Diagnostics;

namespace LaunchWindowFromDllMain
{
public class Class1
{
static Form1 _form1;
private Process _process=null;
public Class1()
{
}
public void TestActivateWindow(){
_process = new Process();
_process.StartInfo.FileName = "notepad.exe";
_process.StartInfo.Arguments= "";
_process.StartInfo.WorkingDirectory="";


_process.Disposed += new EventHandler(DisposedHandler);
_process.Exited += new EventHandler(ExitHandler);

_process.EnableRaisingEvents = true;

_process.Start();

}
private void DisposedHandler(object sender, EventArgs e) {
Console.WriteLine("Disposed...");
}
private void ExitHandler(object sender, EventArgs e) {

_process.Dispose();
_process=null;

Console.WriteLine("Exited...");

_form1=new Form1();
_form1.Show(); <--after form is displayed it is frozen------------
}

}
}
---the form that is launched in class library dll----

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace LaunchWindowFromDllMain
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private bool _toggle=false;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
#endregion

private void button1_Click(object sender, System.EventArgs e) {
if(_toggle==true)
this.textBox1.BackColor=System.Drawing.Color.LightBlue;
else
this.textBox1.BackColor=System.Drawing.Color.Red;

_toggle=!_toggle;
}
}
}
----The main aapplication that launches the classLibrary dll------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace LaunchWindowFromDllMain
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
private Class1 _Class1=null;
public Form1()
{
InitializeComponent();
_Class1=new Class1();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e) {
_Class1.TestActivateWindow();
}
}
}


Herfried K. Wagner said:
* "John Dolinka" <[email protected]> scripsit:
When I launch a form from a c# dll (part of the c# project), after I
do
a
show the launched form freezes. If the dll host launches the same
form
from
an exposed public method in the c# dll project then no problems.
Is there anyway I can launch the windows form that is part of my c# project
with out it "freezing".

I thought the public method resides in the C# DLL? Please post some
code.
 
Back
Top