Click here to Skip to main content
11,933,038 members (55,258 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Errors multithreading , +
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:

 public void SelectActiveWindow(string windowname)
                // Any active control should be hidden thats what this function does:
                // 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);
// here are some more windows to call but not needed to show here.
            // if the window already exists show the window: 
            {// 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 29-Oct-12 6:37am
Sergey Alexandrovich Kryukov 29-Oct-12 13:03pm
"Does not work properly" is not informative. The whole questions makes no sense at all.
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".


1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^],
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[^].

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100