1
votes

I am having trouble catching the double click event in a .net treeview. In the click event, i have a little time consuming logic (i.e. a database call to update a section of the UI). so because of the time delay here, my 2nd click in the case of a double click is ignored, therefore, the normal behavior expected from a treeview (i.e. expand on double click) does not work. My click event is as follow.

private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{ 
 if (e.Button == MouseButtons.Left)
{
//database call
}
}

The database call prevents the double click being fired when the system double click time is set to the fastest. I tried to implement a thread within, but my application prevents me from using any other thread within it. therefore, pls suggest a way in which i can fire double click event (or to get the nodes to expand on double click) without using threads :(

Thank you in advance :)

3
WHat do you mean by this: but my application prevents me from using any other thread within it?npinti
i cant use another thread. it gives a thread violation exception cause the rest of the application is created in such a way. pls suggest a way around without threads, if that is possibe :(Dee
Which version of .net you're in?Sriram Sakthivel
.NET version 4 for thisDee
In the event of a double click, do you still want the DB code to run as well? A simple solution would be to start a timer in the click event, when the timer expires it will run the DB code and you can still execute the DB code in a separate thread as suggested by others. If you do not want to run the DB code on double click, you can just cancel the timer in the double click event.Chris Taylor

3 Answers

2
votes

Don't do any work in main thread which take more than few milliseconds, It is already having so many work to do. let it do those please.

You can make use of TPL here..

var uiContext = TaskScheduler.FromCurrentSynchronizationContext();
var task = Task.Factory.StartNew(() =>
{
    //Your database call here, which will be run in threadpool thread
    return resultFromDatabase;
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
task.ContinueWith(antecedent =>
{
    var resultFromDatabase = antecedent.Result;//This runs in main thread
    //Update UI
}, CancellationToken.None, TaskContinuationOptions.None, uiContext);

StartNew provides many overloads, to know why I chosen a overload with these many number of parameters you can read it here, same applies for ContinueWith too.

0
votes

Well, ideally the database call should be made in another thread, then. You should consider refactoring your application to make that possible.

A possible workaround would be to handle a right button click (or a middle button click) in treeView_NodeMouseClick, so that treeView_NodeMouseDoubleClick is still called for a left button double click.

0
votes

I doubt that you can go around this without having another thread to do the heavy lifting. What is happening in your case is that the DB call, which can be resource intensive, is happening in your event handler, which is being executed by the Event Dispatcher Thread, which is the code which takes care of the GUI (including response to events other than just rendering).

The ideal solution to this would be to use a Background Worker and let it do the heavy lifting. If you need to do some GUI operations, then, you could take a look at this previous SO thread for more information.

That being said, if you really can't use threads, what you could do would be to disable your control before your DB operation and re-enable it afterwards. This should disallow the user from sending you new events through that control, that being said however, the application will most likely become unresponsive for some time.