Forms and threads

mberube

Member
Joined
Jun 17, 2005
Messages
10
Location
Quebec, Quebec, Canada
Programming Experience
1-3
Hi there! I'm currently making an application that calculate area of some regions that are in some others. This is a pretty long process that takes hours. This is a normal process as it is heavily complicated. Due to this, I made a form wich would be the status form. It has 1 progress bar and a cancel button. When I first implemented it, the form wasn't showing at all, because the processes. I asked for help and someone told me to make a different thread that would refreh the form. I did this and now the form is perfectly refreshing and i can see the % of the completed work. Now here comes my problem, the form is showing but i can't access it. Means that i can't press the cancel button and i can't even move the form.¸

Does any1 know why? or have a better solution to this?

That would be greatly appreciated since i''ve been blocked on this problem for a week now and that i can't advance without that. Thanks for any help
 
Code At Form Load

I had the same problem with a status bar. I had it on a form and I did all the forms code in the form load event (no status bar shown). I got the status bar to work by not having the process code in the form load event, but instead have the user press a begin button. The form is loaded and everything is initailized before the process begins.

hope that makes sense, may not be the best way but it works.
 
what do you mean?

In the first form, the user press a button for the process to begin. Before the precedd begins, a new thread is started and the form appears. but there is no code into the form_load of the statusbar form.

Actually, the thread calls a method from a class. That class is used to have some parameters. the method change the parameter of the form and refresh it. the refreh works but i can't touch the form. If i minimize it to the taskbar, i can't mazimize it anymore. Look strange, If your method works, i'd like you (if you're willing to) to tell me exactly how you did it,

Thanks alot for replying and hepling
 
I am sorry if I do not understand you correctly... but I think you need to put Application.DoEvents() somewhere necessary (propapbly within the loop) in the codes. It allows the form to react to events such as mouse click.
 
You should be doing UI stuff, e.g. the form that shows the progress, in the main thread and the long calculations in a new thread. Then you just handle the Click event of the Cancel button to stop the additional thread.
 
ayozzhero said:
I am sorry if I do not understand you correctly... but I think you need to put Application.DoEvents() somewhere necessary (propapbly within the loop) in the codes. It allows the form to react to events such as mouse click.

I tried that and it didn't change anything, because no event were called. Look like the desing is there but the form is untouchable during the calculations

jmcilhinney said:
You should be doing UI stuff, e.g. the form that shows the progress, in the main thread and the long calculations in a new thread. Then you just handle the Click event of the Cancel button to stop the additional thread.

This would be a hard thing to do because everything is in my first form and that's why i'm doing my calculations
 
The thing is, you can't cancel the main thread from the worker, only the other way around. The general rule is UI in the main thread and long, time-consuming operations in the worker thread. I guess I don't know the structure of your project but I'd think that it would be mostly cut and paste. Better than spending another week trying to work it out.
 
The reason I suggested that you open the status form first is because you can call it using ShowDialog and it will behave as a modal dialog. If you start a new thread and then open the status form from that thread using ShowDialog, it will behave as a modeless dialogue, i.e. you will still be able to access the main form.

I suggest that you open the status form from the main thread using ShowDialog. Then have the status form start a new thread to do the calculations. You can start the new thread at a procedure in the main form or you could move that procedure to the status form itself. The new thread can still update the progress bar and/or label in the status form as long as it can get a reference to them. It's up to you to make sure that that reference is available. If the thread starting point is in the status form then that is not a problem, as you would just use "Me.". If the thread starting point is in the main form, then you'll need to set a reference to the status form or its controls as a form-level variable that can then be accessed by the new thread as well.
 
but how could i start the thread in the child form (status) that would refer to the main form,the child form doesn't know the existence of the main one, does it? Anyway, the modal thing isn't a problem since I made the main form not visible for certain reasons.


Thanx for your help alot
 
Once again, it's up to you to make the child form aware of the main form. You could change child form constructor to accept a form reference and then the parent can pass a reference to itself when it creates the child. The child could then store that reference in a class-level variable that it can refer to at any time. You would then have to make the thread starting point a public method of the parent form. I've never tried this but an alternative would be to just pass the child form a delegate rather than the whole parent. The AddressOf operator creates a delegate, so you should be able to change the child constructor to accept a delegate that can be passed directly to the Thread constructor.
 
Kulrom, I don't think this is an MDI app.
Mberube, I would suggest that you do make the child form a modal dialogue even if you do hide the parent. This is because the parent will be instantly aware of when the child closes because the call to ShowDialog will return, and you will get a DialogResult to indicate whether the process completed successfully or not. If you call Show to display the child, you then need to notify the parent somehow that the child has closed. That is not difficult but it is extra work that you needn't do, unless there is a particular reason for it.
 
Back
Top