1

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?

sanger
  • 63
  • 1
  • 7
  • 2
    What do you mean it isn't thread safe? It doesn't seem like your problem is related to this. Could you please elaborate. – Brian Rasmussen Nov 16 '09 at 19:21
  • If it's not thread-safe then it just means that it can't be, reliably, used by more than one thread at a time. It seems you are running a single background thread, so it should be fine. Are you getting some kind of error/race condition that led you to second-guess your design? – Ryan Emerle Nov 16 '09 at 19:34

3 Answers3

2

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
  • 1,404
  • 8
  • 15
1

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
  • 48,783
  • 32
  • 145
  • 190
0

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
  • 56,041
  • 24
  • 146
  • 247