-4

I have a program that loads data from a SharePoint site. It loads txt files, xml files, etc. Any of these "load" actions can take a lot of time because of the user's connectivity to the SharePoint. Therefore the whole windows form UI gets unresponsive until the data is loaded.

So I would like to know how can I easily create a thread for that "retrieval" of information while the whole windows forms UI still works and is operative.

Servy
  • 202,030
  • 26
  • 332
  • 449
dracebus
  • 21
  • 7

1 Answers1

1

You have a few options. I'm not going to provide exact code for any of them, but, I will provide you with research topics.

You can use a BackgroundWorker, Task.Run() or manage your own threading by doing Thread.Start(). Do you need to fire off an event when the downloading is finished? If so, you can do something like this:

var task = new Task(() => DoSomething());
task.ContinueWith(() => SignalDone(), TaskScheduler.FromCurrentSynchronizationContext());
task.Run();

The ContinueWith and TaskScheduler.FromCurrentSynchronizationContext will ensure that the signaling will be done on the UI thread to minimize race conditions. You're on your own if you're doing databinding to anything being populated.

willaien
  • 2,647
  • 15
  • 24