المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Thread "BringToFront"



C# Programming
11-10-2009, 04:00 AM
Hi,

I have an interesting problem.

I have a tray utility program with various menu options that in turn bring up different dialog boxes. I want each dialog box to work as if they were modeless, but track them as if they were modal. I have solved this by making each box modal but have them on individual threads with a boolean to track if it is open or not.

Some code...

#region Configure MenuItem

private bool MyFormStarted = false;
private MyForm MyDlg;

private void MyDlgMenuItem_Click(object sender, EventArgs e)
{
if (MyFormStarted)
{
if (MyDlg.InvokeRequired)
{
MyDlg.BeginInvoke((ThreadStart)delegate()
{
//MyDlg.Show();
//MyDlg.BringToFront();
//MyDlg.Focus();
});
}
else
{
//MyDlg.Show();
//MyDlg.BringToFront();
//MyDlg.Focus();
}
}
else
{
MyFormStarted = true;

new Thread(new ThreadStart(delegate
{
MyDlg = new MyForm ();
MyDlg.ShowDialog();
MyDlg.Dispose();

MyFormStarted = false;
})).Start();
}
}

#endregion



I have a block of code similar to this for each dialog that the tray utility is to open. By doing it this way, I can allow the opening of multiple dialog boxes at the same time while ensuring that each dialog can only be opened once.

What the problem is though, I cannot work out how to put the selected dialog on top. I have rem'ed out the MyDlg.Show() because this does not seem to work. I have tried

MyDlg.BringToFront();
MyDlg.Focus();

As well, but while the form obtains the focus, I cannot get it to display on top. I think it might have something to do with the threads.

So I guess ultimately my question is this: Is there a way to bring one thread in front of the other as 'BringToFront' would do to to the form?

Obviously, I will have to use a private class variable to hold the thread instance, but then what should I do with it.


Regards,

David