Click here to Skip to main content
15,891,888 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i have one GUI where i have below controls,
Tool Bar ,Combo Box, ListBox, Context Menu, ListView and Treeview.

problem is.. i am passing upper controls in one Thread which continues updating these controls.. so meanwhile my thread runs, i am not able to click on any of GUI controls.. yes i can see updated value in list box and other controls but user not able to click on any GUI controls during thread runs.


C#
private void MethodXYZ()
    		{
        this.Invoke(new MethodInvoker(delegate()
                    { 
        // loigc here
            mnuItemDatabase.Enabled = false //this line shows error when I am trying to remove Invoke from Logic
        })); 
    }

C#
ThreadStart TS1 = new ThreadStart(MethodXYZ);
                        Thread Th1 = new Thread(TS1);
                        Th1.Name = "ExecuteTestSession";
                        Th1.IsBackground = true;
                        Th1.Start();


one more thing entire thread function i wrote inside "this.INVOKE" ,reason i put thread method inside Invoke is i am using UI controls inside it, and its throw below exception if i remove Invoke,

"Cross-thread operation not valid: Control 'form name' accessed from a thread other than the thread it was created on."

What I have tried:

i tried with Background worker and Task factory call to thread method.
Posted
Updated 25-May-17 8:57am
Comments
Ralf Meier 25-May-17 7:13am    
All parts of the GUI belong to the Main-Thread and could only accessed, actualized and invalidated from there.
The right way is invoke - but your Thread must give the Main-Thread time to work. So ... how is the code of your Thread ? Did you have a Thread.Sleep in it ?
Varun_nayak 25-May-17 10:16am    
My method which is in thread don’t have any sleep code,
It has thousands of lines.. Sometime even lacs.. Running inside for loop, updating GUI List box step by step with values retrieve from database.
And I am trying to give access of 'Pause' option to user meanwhile, but my GUI got freeze. so user can just see updating List box but not able to click on Pause button.

1 solution

It looks like you're starting a background thread which simply calls Invoke. This pushes all of the logic back onto the UI thread, as if you'd never started a background thread in the first place. The only difference is, you now have a second thread stuck waiting for the logic to finish.

Remove the monolithic Invoke call, and instead wrap each call that accesses or updates a UI control in its own Invoke call.

So, instead of:
this.Invoke(new MethodInvoker(delegate{
    // Read UI
    // Logic
    // Update UI
    // More logic
    ...
});

use:
this.Invoke(new MethodInvoker(delegate{
    // Read UI
});

// Logic here

this.Invoke(new MethodInvoker(delegate{
    // Update UI
});

// More logic
...
 
Share this answer
 

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