1

i have a windows application in visual studio 2008 framework 3.5, I need concurrent waiting window where a process is going on in a window and on that window another form appears with some moving image (animation) just like a .gif image.

I have 2 forms, on form2, on button click i perform a lengthy process, i want that my 'frmWait' appears on screen while the process on form2 is going on and disappears when it finished. When my 'frmWait' appears, then form 2 should not be accessible to user.

my code is here. Form 2

in SomeWork procedure there is a for loop i have wrote, but actually in program it is database commands that performs in sqlserver, when query executes on server, my main programs working is halt until server finishes working, afterward the program gains exectution pointer, meanwhile, software seems that is is hanged.

    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                frmWait fw = new frmWait("Wait...",0);
                fw.Show();

                SomeWork();

                fw.Close();
            }

            private void SomeWork()
            {
                for (int x = 0; x < 99999999; x++)
                { }
            }
        }
    }
Haider Ali Wajihi
  • 2,756
  • 7
  • 51
  • 82
  • thnx all to reply. but here is some more specification needed, On the process SomeWork and form Form2 i dont have access, i have access on frmwait, this form will be a generalize form where any one need to wait while long process will call frmwait in the team of 15 programmers. – Haider Ali Wajihi Jan 20 '12 at 07:03
  • i want to prepare a simple solution for this, the programmer who is using frmwait, he have to just show frmWait before work and close it after work. the some work process should be in main thread, until somework not completed user can not perform further task in application. – Haider Ali Wajihi Jan 20 '12 at 07:05
  • First of all, you should edit your question rather than adding a comment. Second: It's not clear. You mean you can't modify the code in `button1_Click` ? How do you want to solve the problem then? – Serge Wautier Jan 20 '12 at 07:06
  • What you want to do is bad design/architecture. 3 people have suggested BackgroundWorker. Don't you think it's worth investigating it? – Serge Wautier Jan 20 '12 at 07:09
  • sorry! for bad practice i am new, yes i have no access on button1_Click, and when a use backgroundworker form2 is awailable to user, i want form2 should halt until process complete and frmwait should run while process is going on. – Haider Ali Wajihi Jan 20 '12 at 07:12

6 Answers6

3

First add..

using System.Threading;

I've adapted your code above to use a seperate thread..

private void button1_Click(object sender, EventArgs e) 
{ 
    this.SomeWork();             
}

Thread t;

private void SomeWork()
{
    frmWait fw = new frmWait("Wait...", 0);

    this.t = new Thread((ThreadStart)delegate()
    {

        for (int x = 0; x < 99999; x++)
        {
            Thread.Sleep(100);

            //Debug.WriteLine(x);
        }


        if (this != null && !this.IsDisposed) this.BeginInvoke((ThreadStart)delegate() { fw.Close(); });
    });

    this.t.Start();

    fw.ShowDialog(this);
}

You also might want to handle the FormClosing event to kill the thread if user was allowed to close Form2 before completion!

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    if (t != null && t.IsAlive) t.Abort();
} 
banging
  • 2,540
  • 1
  • 22
  • 26
  • what is "fw", will you please briefly describe, i am from a different culture so i have some problem to understand the language!! – Haider Ali Wajihi Apr 25 '12 at 06:31
  • instead of fw.Show(); try fw.Show(this); – banging Apr 25 '12 at 22:29
  • this will stop execution and will not resume until fw not close – Haider Ali Wajihi Apr 26 '12 at 12:18
  • Your problem is you're trying to do everything in one thread. You should never do any work in the UI thread. This will cause your UI to hang. I'll try to update my answer above with the full code! – banging Apr 26 '12 at 17:00
  • Ohh, starting the thread and _then_ showing the dialog. Of course! Thanks. This really helped me out with my startup loading; all attempts where I opened a dialog _before_ my main form was shown caused the app to lose focus after closing the wait dialog, but if I use this while the form is already shown, it works. – Nyerguds Nov 06 '17 at 13:59
3

You should perform the lengthy process in a background thread. This is easily achieved using using a BackgroundWorker.

Once the worker is started, just display frmWait using frmWait.ShowDialog().

When the worker notifies the end of the job, close frmWait eg using frmWait.DialogResult=Cancel.

You can take advantage of the ProgressChanged event to report progress in you frmWait.

And if user clicks the Cancel button in frmWait, just call CancelAsync() on your background worker.

Full sample here.

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
2

.NET 4 introduced the Task Parallel Library which makes task like this easier. Instead of using the BackgroundWorker you should use System.Threading.Tasks.Task. There is more information in the answers to this question: Task parallel library replacement for BackgroundWorker?

Community
  • 1
  • 1
Jason
  • 15,915
  • 3
  • 48
  • 72
0

You should use BackgroundWorker in this scenario to move SomeWork part away from main UI thread

Move the SomeWork(); part to a separate DoWork method of BackgroundWorker. In this way SomeWork will be executed by BackgroundWorker in a separate background thread. Then in Background Work Completed event which will be raised once the SomeWork is complete do fw.Close();

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
-1

you can use Modal Dialg box. as it will not allow to interact with other form (i. e. form2)

frmWait.ShowDialog();

also try to use Backgroud worker background worker, which makes more easy to do background proceses.

Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
-1

i did not understand what you mean exactly, but if you want that until frmWait is open, your Form2 must not be accessible to user, use frmWait.ShowDialog(), and if you want to close the frmWait when a specific process is ended, in frmWait, use following

Process.WaitForExit();
this.Close();
Peyman
  • 3,059
  • 1
  • 33
  • 68
  • this would require your long running process be a separate executable, that your dialog would launch in a command prompt. i don't think it's what you are looking for. – Jason Jan 20 '12 at 07:11