Click here to Skip to main content
15,796,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the code, i have a dropdown consisting of checkboxes,
When an item is checked- in the background a service call is made and data is updated to the UI. fetching of data is done faster but the UI thread wont be free until it is updated to the UI so till then the checkbox remains disabled.
I tried by creating a new thread for checkbox check handler but as there is a UI thread inside, i get a cross thread exception and neither dispatcher worked.

I want the checkbox to respond immediately to the user action irrespective of what is happening in the background.

Like-when the checkbox is checked by the user,it should reflect immediately and then the UI should be updated. After it is checked it may remain disabled until update is done.
Rite now the checkbox gets highlighted when the user checks but it takes time for the tick mark to appear.
Posted
Updated 29-May-11 22:02pm
v2

Thanks a lot,
M getting error at this line "this.Invoke(new MethodInvoker(UpdateTheUIs));"

do i have to add any assembly reference??
 
Share this answer
 
Comments
Tarun.K.S 30-May-11 6:26am    
How about adding using System.Windows.Forms namespace;
Let me know if it works.
Tarun.K.S 31-May-11 2:43am    
Comment from Ashakoti:
Not possible because mine is web application..
Tarun.K.S 31-May-11 2:57am    
I have updated my answer. It doesn't use the MethodInvoker function but uses a delagate instead.
Tarun.K.S 31-May-11 3:22am    
I have updated the answer again. WPF and Silverlight use the Dispatcher.
Tarun.K.S 31-May-11 5:15am    
Well I was to help you remove the cross-thread exception when you update your UI. But it's still surprising that the tick in the checkbox isn't coming quickly. How about using a BackgroundWorker(safe and easy to use) instead of using the delegates and threads. Please reply on my comment instead of writing it as an answer.
Hi Asha,

Here is a briefly explained way of doing it:

C#
private void OnSpaceStatusChecked ( object sender, EventArgsEndpointStatus e )
{
 Thread newThread = new Thread(DoYourBackgroundWork);
 newThread.IsBackground = true;
 // You can pass a parameter here, which can be retrieved from the obj parameter in the DoYourBackgroundWork  function.
 newThread.Start(param); 
}

private void DoYourBackgroundWork(object obj)
{
 //Do some backgroundwork. 
 //...
 
 //MethodInvoker should be used only if the method you want to call doesn't have any parameters.
 //If you require the use of parameters, create a delegate for it and call its invoke method.
 this.Invoke(new MethodInvoker(UpdateTheUIs));
}

private void UpdateTheUIs()
{
 //Update your UI controls here.
}


UPDATE:
Comment from OP:
Not possible because mine is web application..

Well there is another solution by using Delegates.

Declare a delegate after before the class declaration:

C#
// Please declare this delegate before your class definition, ie. it sits on top of class declaration.
public delegate void UpdateUI(object params);
// Create a reference as a class field. To be declared after the class defenition
UpdateUI update;

private void OnSpaceStatusChecked ( object sender, EventArgsEndpointStatus e )
{
 //Point your delegate to the function
 update = UpdateTheUIs;
 Thread newThread = new Thread(DoYourBackgroundWork);
 newThread.IsBackground = true;
 // You can pass a parameter here, which can be retrieved from the obj parameter in the DoYourBackgroundWork  function.
 newThread.Start(param);
}

private void DoYourBackgroundWork(object obj)
{
 //Do some backgroundwork. 
 //...
 //Pass the delegate object together with the parameter so that it can call the UpdateTheUIs function without encountering any cross-thread exception. Use the obj parameter with values that will be required to update your UI.
 this.Dispatcher.Invoke(update , obj);
}

private void UpdateTheUIs(object param)
{
 // From param you will receive "obj" called from the above invoke.
 // Use it accordingly
 //Update your UI controls here.
}


UPDATE 2:

Oops, I forgot that its silverlight. :doh:

Just replace this.Invoke with this.Dispatcher.Invoke
I have updated the answer accordingly.
Hope its clear now.
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 30-May-11 14:51pm    
I see, my 5. Next time please notify me that you have the solution when you comment on mine.
--SA
Tarun.K.S 31-May-11 2:27am    
Thanks SA, sure will notify you next time. :)
parmar_punit 31-May-11 3:26am    
good one, My 5
Tarun.K.S 31-May-11 3:32am    
Thanks! :)
Tarun.K.S 31-May-11 4:02am    
Comment from OP:
Thank u........
But ur solutions dint work.. Frown | :(
The check n uncheck still take time as before...
Thanks for responding,

Mycode,

private void OnSpaceStatusChecked ( object sender, EventArgsEndpointStatus e ) {


_selectedSpaceHeader = null;
ShowExpanderControl ( false );
_lstfilterOptions = e.Data;
GetOriginalSpaceList ();

if ( _spaceHeaderList == null ) {
_spaceHeaderList = new List<SpaceHeader> ();
}
else {
_spaceHeaderList.Clear ();
}

GetStatusSearchList ( _searchList );
//Filter on text if "search by text" is performed.
SearchOnText ();

if ( !_spaceHeaderList.Contains ( _selectedSpaceHeader ) ) {
_selectedSpaceHeader = null;
}
UpdateSpaceListDataOnUI ();
}

This handler is hooked to the checkbox in xaml...
 
Share this answer
 
You need to handle the event CheckedChanged. Assuming it's C#:

C#
my.CheckBox.CheckedChanged += (sender, eventArgs) => {
    CheckState checkState = (CheckBox)sender.CheckState;
    //or, simpler:
    bool state = (CheckBox)sender.Checked;
    RespondImmediatey(checkState); //or whatever
};


—SA
 
Share this answer
 
Comments
Tarun.K.S 30-May-11 3:27am    
Not exactly, OP says it has a "dropdown" which consists of checkboxes.
Sergey Alexandrovich Kryukov 30-May-11 14:52pm    
Oops. I see, thank you for the note. I suggest your solution as an answer then.
--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