views:

388

answers:

4

I am using 3rd party library and some of the functions of the library take a long time to execute so I want to display a "Please Wait" dialog while the functions are busy.

Normally I would do something like this:

Thread longTask = new Thread (new ThreadStart(LongTask));
longTask.IsBackgroud = true;
longTask.Start();

pleaseWaitForm = new PleasWaitForm ("Please wait for task to complete");
pleaseWaitForm.ShowDialog();

void LongTask()
{
    // Do time consuming work here

    pleaseWaitForm.CanCloseFlag = true;
}

Unfortunately the 3rd party library is not thread-safe. Any workarounds? Is there any way of managing the Dialog Box as a background task?

+1  A: 

You pretty much need to build your own dialog box.

One option is to poll your completed flag in a timer or the like.

Yet another option is to let the form "own" the task and use a BackgroundWorker for progress and completion notification.

John Gietzen
A: 

Suppose you have a method, LongTask, and it is not thread safe. If that method is running and it does not need any shared variables, then you can simply wrap it around a BackgroundWorker and update the "please wait" screen the moment the task finishes.

If a certain method is not thread safe, that can still mean it is safe to use it in a thread, it just depends whether it runs multiple times asynchronously (danger!) or synchronously (not a problem).

Abel
+2  A: 

I think you are misunderstanding what "thread safe" means. If you are going to be calling methods/properties of your 3rd party component only from single thread, the component does not have to be thread safe. See this article.

Furthermore, I would suggest you use a background worker class in this case.

HTH

unclepaul84