Click here to Skip to main content
15,886,807 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
I have a main form, and depending on the users choice, I show user controls with the information the user needs.

This way I get about 6 user control windows, which should be managed properly.

What I want is to create a function which is called SelectActiveWindow(string windowname) I put this function in a new class, because I want it to be accessable from each class.

I used Singleton in each class, because I do not want to create the controls twice.

I have this function to manage my controls but it does not work properly, please see the code:

C#
 public void SelectActiveWindow(string windowname)
            {
                // Any active control should be hidden thats what this function does:
                HideCurrentActiveControl();
                // Check whether the window is already created
                if (!WindowExists(windowname))
                {
                    // window is not yet created; see which window should be created.
                    switch (windowname)
                    {     
                        case STP_Data.Data.MenuControl:
                            STP_Design.ProgramParameters.C.CurrentActiveControl = STP_Data.Data.MenuControl;
                            STP_Design.ProgramParameters.C.MenuControlIsCreated = true;
                            // I am getting compiler errors on the following line: It says:
                            // Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
                            STP_Design.MenuControl.MenuControl.Instance.Parent = STP2Main.ActiveForm;
                            STP_Design.MenuControl.MenuControl.Instance.Location = new Point(3, 30);
                            STP_Design.MenuControl.MenuControl.Instance.Show();
                            STP_Design.MenuControl.MenuControl.Instance.BringToFront();
                            break;
// here are some more windows to call but not needed to show here.
                    }
            }
            // if the window already exists show the window: 
            else
            {// here is another switch - case structure to show the  window when it does exist


How I hope it is clear what I want. Probably I have a wrong working method, please tell me what would be better. Is there a better way to do this, or an easy way to work around the error I'm getting?
Posted
Comments
Sergey Alexandrovich Kryukov 29-Oct-12 13:03pm    
"Does not work properly" is not informative. The whole questions makes no sense at all.
--SA
pieterjann 29-Oct-12 13:23pm    
Agree, how would you rewrite the question?
Sergey Alexandrovich Kryukov 29-Oct-12 13:37pm    
I don't need to rewrite, but you might need. If my assumptions I made in my answer are right, you might not need anything, but in other cases...
You see, you need to provide enough information to resolve your problem. If you have appropriate information; and we do not, it might not work.

Very typically, you would need to create a separate minimalistic but complete project and try to reproduce the problem, focusing only one problem and nothing else. It could allow you to make such code sample really small. At this step, as your problem is isolated, you got a chance to fix it by yourself. If not -- it makes a good sample to post here, using "Improve question".

--SA

1 solution

The question makes no sense at all. If you really wanted to "prevent multithreading", you would not use threads at all. The problem not to avoid it, but to use threads properly. It's also not very nice for you to ask something based on your threading concern, but to say nothing about your threads, showing no thread-related code.

By these reasons, I'll answer only to a degree allowed by the information I could get from your exception information (and even that is not precise: you need to provide comprehensive exception information: type, message, parameters or inner exceptions, if any, indicate the line of code where the exception is thrown, etc). Actually, such problems are resolved relatively easily, because the UI libraries (System.Windows.Forms, WPF) are equipped to solve them.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

I only assumed that your exception were thrown from UI, first, because this is the most typical problem, and, second, because you show some UI code. In more general case, you would need to delegate some calls from one thread to another. In particular, there is a number of some other APIs requiring all the call from a single thread, but the logic of your application often requires using such APIs in different threads. The right approach to such situations is to have a single thread to work with such API; and all other threads should use this thread as a server to serve those calls. So, this special thread should be programmed in a special way to serve those requests, and provide a similar mechanism for delegating the calls to it from other threads.

To understand how to make such mechanism, please see my article, complete with source code and usage samples:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA
 
Share this answer
 
Comments
pieterjann 29-Oct-12 13:22pm    
Thank you for this answer! What you are saying in the beginning, I am not showing anything of the code where I use multithreading, is because I am not starting a second thread anywhere in my code. I'll read the articles you pointed to and hopefully this solved the problem!
Sergey Alexandrovich Kryukov 29-Oct-12 13:33pm    
Anyway, I hope these knowledge will be enough to resolve your problem.
If you have some follow-up questions, you are quite welcome to ask them.

If you agree that it's helpful, please accept this answer formally (green button) -- thanks.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900