calling a delegate in a another thread (compact .NET)

A

Alexander

Me again... ;)

I am running a small borderless dialog in a seperate thread, that
shows a small animation and I want to signal it to close itself. But I
am running in a System.ArgumentException which I do not understand
because I do not have any arguments.

Here is what I am doing:

-----------------------------------------------------
namespace MyNamespace
{
// delegate declaration
public delegate void CloseDialogDelegate();

// the dialog showing a animation
public class MyDialog : System.Windows.Forms.Form
{
public CloseDialogDelegate m_CloseDialog;

public WorkingDialog()
{
this.m_CloseDialog = new CloseDialogDelegate(CloseDialog);
...
}
...
public void CloseDialog()
{
this.Close();
}
}

///////////////////////////////////////////////////////////
// the big main program
public class MyMainForm : System.Windows.Forms.Form
{
private MyDialog m_MyDialog;
private Thread m_MyDialogThread;
...

private void MyDialogThreadFunc()
{
Application.Run(this.m_MyDialog = new MyDialog());
}

private ShowMyDialog()
{
this.m_MyDialogThread = new Thread(new
ThreadStart(MyDialogThreadFunc));
this.m_MyDialogThread.Start();
}

private CloseMyDialog()
{
// System.ArgumentException
this.m_MyDialog.Invoke(this.m_MyDialog.m_CloseDialog);
}

private DoSomeWork()
{
this.ShowDialog();
...
// Do some excessive Work, including
// preparing some UI stuff of this form.
...
this.CloseDialog();
}
...
}
}
-----------------------------------------------------


The exception is a System.ArgumentException with additional
information ArgumentException. Strange is especially that the compact
..NET framework does not support any Invoke function which passes
arguments....

I am confused...
Alexander
 
A

Alexander

mmh, double post, seems the first try did actually work ;)

Thanks Jon, that brought me directly to the easiest solution for this problem:


public class MainForm : Form
{
...
public void OnCloseDialog(object sender, EventArgs e)
{
// dialog running in own thread
this.m_MyDialog.Close();
}

public void CloseMyDialog()
{
this.m_MyDialog.Invoke(new EventHandler(OnCloseDialog));
}
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top