Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i am working on winform application , one screen of my application i am using a wpf user control . i am setting visiblity of that WPF user control through binding.
there is no threading in my application, i want that as soon as set the visiblility of user control it should effect in UI,but is not happening like that.

i am able to see my WPF user control after the screen is loaded fully.


my code somewhat look like this

void Dosomething()
{
IsUserControlEnabled=true;//here the UI should reflect but not happening

//some time taking code assume 4-8 sec

IsUserControlEnabled=false;

}

i think my problem is ome what releated to below link

Venkatesh Bathineni: WPF: Build Responsive UI (Threading)[^]

What I have tried:

void Dosomething()
{
IsUserControlEnabled=true;//here the UI should reflect but not happening

//some time taking code assume 4-8 sec

// if aim not setting IsUserControlEnabled=false then i am able to see in my UI but i want that in this method only it should appear an disappear

}
Posted
Updated 14-Jun-16 14:55pm
v2
Comments
Kenneth Haugland 14-Jun-16 14:53pm    
Do time-consuming work off the UI thread, as you have done it, andy application can stall the UI.
kumar9avinash 14-Jun-16 14:56pm    
Hi Kennenth,there is no UI thread its the single apllication thread. i have not write any code releated to thread .
Dave Kreskowiak 14-Jun-16 15:01pm    
The IS the UI thread. The thread the application starts on is also called the "UI thread". You didn't create this thread yourself. It was created for you when your application started.
kumar9avinash 14-Jun-16 15:44pm    
Hi Dave,
you are more than 100% correct,but can you suggest me something related to issue.
Dave Kreskowiak 14-Jun-16 16:19pm    
Yeah, move the long running work to another thread. You can read up on "async" and "await" at Asynchronous Programming with async and await (C#)[^].

1 solution

As some comments suggest, you really need to overcome this problem by adding one or more additional threads. The problem related to WPF would be notification of the UI or updating something in UI as the non-UI thread gets some results.

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:
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.

—SA
 
Share this answer
 
Comments
kumar9avinash 15-Jun-16 0:16am    
Hi sir, if i use background worker and set visibility of the user control in BW then it will reflect the UI ?
Sergey Alexandrovich Kryukov 15-Jun-16 2:21am    
Yes, if you use the mechanism of invocation of the call to UI thread I described in this answer.

BackgroundWorker is just another way to execute code is a separate thread. I never use it thought, prefer to create a thread explicitly (or predefined small number of thread), keep it during all the application lifetime and reuse.

See also my other answers:
http://www.codeproject.com/Answers/646422/Making-Code-Thread-Safe#answer1
,
http://www.codeproject.com/Answers/182898/Running-exactly-one-job-thread-process-in-webservi#answer1
,
http://www.codeproject.com/Answers/317777/pause-running-thread#answer2
.

Will you accept the answer formally now? In all cases, your follow-up questions will be welcome.

—SA
kumar9avinash 15-Jun-16 3:36am    
Hi Sir,

i am trying below way to make that boolean property to true but i am not able to do
void worker_DoWork(object sender, DoWorkEventArgs e)
{
if ( System.Windows.Application.Current==null)
{
new System.Windows.Application();
}

System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => IsWaitCursurDisplay = true)); // one way

//another way to set IsWaitCursurDisplay to true but nothing is happening , the value is not setting to true
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
IsWaitCursurDisplay = true;
}));

}
Sergey Alexandrovich Kryukov 15-Jun-16 9:19am    
For System.Windows.Forms, you can use Control.BeginInvoke. The delegated method is only needed when you do something to any UI elements of the UI being executed:

BeginInvoke(new System.Action(DoSomething));

or

BeginInvoke(new System.Action(() => { DoSomething(); }));

BeginInvoke(new System.Action((oneParameter) => { DoSomething(oneParameter); }), parameterValue);

And so on...

—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