Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

UI Invoke

Posted on 2009-05-13
5
Medium Priority
?
1,897 Views
Last Modified: 2013-12-17
I am have a problem where I am getting a cross-threading exception error when trying to show a form. So I tried to invoke the form using the code below. But I get the error that, Form showDialog tried to set an ineligible form as its owner. Forms cannot own themselves or their owners.
Parameter name: owner
So how do I invoke the thread I need and show the form?

public void ShowForm(System.Windows.Forms.Form form)
    {
        if (form.InvokeRequired)
        {
            form.Invoke(new ShowFormDelegate(ShowForm), new object[] { form});
            return;
        }
        form.ShowDialog();
    }

Open in new window

0
Comment
Question by:EAK31640GW
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24377206
You say you are receiving an error. Since you are using asynchronous calls, the reason and the location of the error may be different from what the debugging is actually reporting. In a recent thread, I researched this for someone else. Can you check it out and see if you can use the technique mentioned in my comment to find out where exactly the error is raised? http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_24344645.html#24210671
0
 
LVL 39

Expert Comment

by:abel
ID: 24377212
In particular, the section on "not the right line".
0
 

Author Comment

by:EAK31640GW
ID: 24377411
Stepped through it again and it is still showing the same thing... The error is being raise on line 8: form.ShowDialog(); Which makes sense since I can't call form.ShowDialog() on itself. But how do I get rid of the cross-Thread calls. I'm trying to use form.Show(), but am having other problems with that... I'll update.
0
 
LVL 2

Accepted Solution

by:
T0ni earned 1500 total points
ID: 24378661
Hi EAK31640GW,

Try doing it this way..
    //This is the Form that you are trying to show from a thread
    public partial class Form1 : Form
    {
        public delegate void ShowForm();
        public ShowForm _showForm;
 
        public Form1()
        {
            InitializeComponent();
            _showForm = new ShowForm(ShowFormMethod);
        }
 
        public void ShowFormMethod()
        {
            this.Show();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Class1 cls1 = new Class1(this);
            Thread oThread = new Thread(new ThreadStart(cls1.DoSomeWork));
            oThread.Start();
        }
    }
 
    //This is the class that is being called by the thread
    class Class1
    {
        Form1 form;
        public Class1(Form1 f)
        {
            form = f;
        }
        public void DoSomeWork()
        {
            form.Invoke(form._showForm);
        }
    }

Open in new window

0
 

Author Comment

by:EAK31640GW
ID: 24434607
T0ni, thank you that worked for the most part, though I used a little different format... Now I am having the same sort of problem but am not really sure what to do this time. I have a click event that creates a progress monitor (window) and begins a background worker thread to process data, the problem is that when the background worker continues to run past the creation of the progress monitor, the progress monitor gets disposed, even when I use .ShowDialog()
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question