0

I'm trying to do a waiting screen for my application coded in c#.

when the user click on start, to prevent possible errors just show a message in the middle, i used backgroundWorker but doesn't work like i want.

i have this:

backgroundWorker1.RunWorkerAsync(formwaiddt.ShowDialog());

the problem is the program waits until i close this dialog.

also i tried other ways like this Can you link to a good example of using BackgroundWorker without placing it on a form as a component?

and i obtain the same result.

then anyone knows how to show a message in the middle of the screen, also i create a panel but doesn't shows it correctly, to lock the user interactions

thanks.

Community
  • 1
  • 1
TiGreX
  • 1,602
  • 3
  • 26
  • 41
  • 1
    You should't be doing UI stuff from the BackgroundWorker RunWorkerAsync method; that runs on a different thread. The way you've described it sounds like you're doing the work on the main thread and trying to have something that tells the user the application is busy. You should really be doing that work in RunWorkerAsync and update the UI as you normally work (put status text, whatever). – Peter Ritchie Mar 13 '15 at 15:32
  • 1
    If you want to do as little as possible over-and-above showing status text (and doing the work in RunWorkerAsync) you can disable the whole form so that the user can't do anything while the work is being done. Keep in mind, doing that is a horrible user experience and you should be doing a lot more; but, it sounds like you might be over your head. – Peter Ritchie Mar 13 '15 at 15:34
  • @PeterRitchie yes i execute the app in the main thread and i want to execute this other ting in other one, is the first time that i edit a windows form app and i suppose that was not very difficult, but looks more that i think. (i always had been worked in web apps). for the second, i think about this but if the user clicks 30 times the aplication says (not respond) but really it's working. – TiGreX Mar 13 '15 at 15:38
  • Are you looking for a [splash screen](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23%20splash%20screen)? – crthompson Mar 13 '15 at 15:45
  • Doing this backwards is a very notorious mistake that gets programmers in deep, deep trouble. [This kind of trouble](http://blogs.msdn.com/b/dsui_team/archive/2012/10/31/debugging-windows-forms-application-hangs-during-systemevents.userpreferencechanged.aspx). It is absolutely crucial that you display a "Please wait" window on the UI thread and move the code that takes a long time to the worker thread. Now it is simple, it doesn't take anything more than ShowDialog(). – Hans Passant Mar 13 '15 at 16:03

2 Answers2

1

The simplest case is to use BackgroundWorker to create a method that matches the delegate signature, attach that method to BackgroundWorker`s DoWork event, and then call the RunWorkerAsync() method of BackgroundWorker:

// Set your cursor to busy 
Cursor.Current = Cursors.WaitCursor;
BackgroundWorker backgroundWorkerExample = new BackgroundWorker();
backgroundWorkerExample.DoWork += new     DoWorkEventHandler(backgroundWorkerExample_DoWork);
backgroundWorkerExample.RunWorkerAsync();

// elsewhere:
void backgroundWorkerExample_DoWork(object sender, DoWorkEventArgs e)
{
    // body of the work elided
}

When the background worker thread has finished, it raises the RunWorkerComplete event on the foreground thread, so you can switch your cursor back to the normal state using:

Cursor.Current = Cursors.Default;

Alternatively, you could use the WorkerReportsProgress property to inform BackgroundWorker that the worker procedure will report progress to the foreground thread at regular intervals.

user8128167
  • 6,929
  • 6
  • 66
  • 79
  • this way is more or less the think that i'm looking for, but i have one problem, if isend the dialog by parameter, i should return something, i returned null but doesn't work, if i create a global variable for the dialog this is the exception `Cross-thread operation not valid: Control 'waitView' accessed from a thread other than the thread it was created on.` obviously if i create the dialog inside the function works, but the problem is never close this dialog – TiGreX Mar 13 '15 at 16:10
  • I don't think you want to pass the dialog into your background worker thread DoWork function. The background worker if for processing a long-running task so that you can keep your main UI thread responsive to user input. Just work with the dialog in your main form class, such as by displaying the dialog after the RunWorkerComplete event is called. – user8128167 Mar 13 '15 at 18:56
0

Have you tried wiring up an event on a timer, so that it runs on a separate thread to check if the process has finished?

nichatter
  • 1
  • 3