By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

HOW TO SHOW: Processing...

100+
P: 153
There are some textboxes placed on a form along with one button.When the user presses the button a modal window pops up till the work is finished and that window shows that circling processing GIF image.
By showing the form as modal , the user will not be able to press the button again untill the shown modal window is closed, which happens automatically when the processing is done.
This I tried to achieve this effect by creating the button's click event as follows:

Futile attempt 1
{
OneForm oneform = new OneForm();
oneform.Show();// mod-less form


Thread.Sleep(); //Emulate work.Coz the Thread sleeps, the GIF icon can not animate

oneform.Close();
}

Futile attempt 2
{
OneForm oneform = new OneForm();
oneform.ShowDialog();// modal form


Thread.Sleep(); //Emulate work.GIF will animate but this line will never execute untill I close the modal form ! USELESS

}

Futile attempt 3
OneForm onefrm = new OneForm(); //oneform is a field so that I can access it from all methods
{
Thread th = new Thread(HeavyWork);
th.Start();
oneform.ShowDialog();//main thread stops here untill I close it but I satrted another thread to execute my work, thank God
}

void HeavyWork()
{
Thread.Sleep(10000);
onefrm.Close(); //OOps this is created in GUI thread so cant access/close it from any other thread
}


Futile attempt 4 //I used this coz it was doing my work, well...almost
{
Thread th = new Thread(SHowForm);
th.Start();


Thread.Sleep(10000);

th.Abort(); //work done not close the form by ending the thread
}

void ShowForm()
{
new OneForm().ShowDialog();
}

Now what happens is that the OneForm does not disappear untill I move the mouse on it.Even the dialog boxes are coming under it and when I move the mouse, it closes.

How to solve this ?

Thanks !
Sep 4 '09 #1
Share this Question
Share on Google+
6 Replies


GaryTexmo
Expert 100+
P: 1,501
Why does the thread need to sleep? Try a loop that checks whether or not your form should close with the animation in it. Put an Application.DoEvents() in that loop if you need other things to respond.

Better yet, put a timer in your form that checks when the form should close and handles it then. I'm guessing the thread.sleep is blocking everything else from happening... though I thought animated gifs took care of themselves. To be honest, this is all speculation, I've never done it before. But experiment about and let us know :)
Sep 5 '09 #2

100+
P: 153
Thread.Sleep() emulates some actual work.
Anyways I have found the answer.
Here is the code

Expand|Select|Wrap|Line Numbers
  1. WaitForm waitfrm = new WaitForm();
  2. delegate void del();
  3. void CLOSE()
  4. {
  5. waitfrm.Close();<br/>}
  6. private void button1_Click(object sender, EventArgs e)
  7. {
  8. Thread th = new Thread(FUNC);
  9. th.Start();
  10. waitfrm = new WaitForm();
  11. waitfrm.ShowDialog();
  12. }
  13. void FUNC()
  14. {
  15. Thread.Sleep(10000);
  16. this.Invoke(new del(CLOSE));
  17. }
Sep 5 '09 #3

P: 2
if you know your form is in center of screen, you can use this comic code:
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
Cursor.Position = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2, Screen.PrimaryScreen.WorkingArea.Height / 2);
Cursor.Position = new Point(x, y);
Sep 5 '09 #4

100+
P: 153
What's that for ?
:)
Sep 5 '09 #5

P: 2
i had the same problem, when stopping thread the form is showing until move mouse over.
i solved it with this code after "thread.abort()", programmatically move mouse on it and move to the old position again. it's not sensible for user because speed is very high.
Sep 5 '09 #6

100+
P: 153
hahahaha :)
Thanks !
Sep 5 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.