0

I have to do the following:

Public Sub OnMouseMove

  If mouseDownButNotYetMoved Then 
    myObjectsStateArchive.SaveObjectsState(myCurrentObjectsState.Clone())
  End If    

  MoveObjectsWithMouse()

End Sub

The problem is that SaveObjectsState method is very "heavy". So, when the user moves the mouse, it expets that the object moves accordingly, but the object is "delayed", because it waits the SaveObjectsState to be finished...

I would like to probably asynchronously do the save... I mean, all the objects are in the current thread, myObjectsStateArchive and myCurrentObjectState... just the operation of cloning and saving to be "paralelized"

What would be the best method, using ThreadPool or something like this?

Public Sub OnMouseMove    
  If mouseDownButNotYetMoved Then 
    System.Threading.ThreadPool.QueueUserWorkItem( _
      New Threading.WaitCallback( _
        Sub(o)
          myObjectsStateArchive.SaveObjectsState(myCurrentObjectsState.Clone())
        End Sub))
  End If    

  MoveObjectsWithMouse()
End Sub
serhio
  • 28,010
  • 62
  • 221
  • 374

2 Answers2

2

You can use TaskFactory.StartNew Method available in .NET 4. From another article:

the new Task pattern in .NET 4 has built-in support for synchronization with the UI thread. This makes writing parallel applications that interact with the user interface easier than ever before.

SlavaGu
  • 817
  • 1
  • 8
  • 15
0

You could use the BackgroundWorker, using a thread to achieve the task. You can show a progress bar (or any other notification way), but be care about updating the GUI from a thread, ti link maybe helpful: How to update GUI with backgroundworker?

Community
  • 1
  • 1
Miguel Prz
  • 13,718
  • 29
  • 42
  • I don't need any progress bar. I just want that the heavy "save" be executed "in paralel" with the graphical objects movement, and not delay the first moving... I mean, all the objects are in the current thread, myObjectsStateArchive and myCurrentObjectState... just the operation of cloning and saving to be "paralelized" – serhio Oct 13 '11 at 13:23