开发者

Windows Form Application (c#) : using an input from form y in form x (threading problem)

开发者 https://www.devze.com 2023-01-27 11:47 出处:网络
I am making an UNO game application. For this in Form Game Table, if a user plays a Wild card he has to select a color out of (yellow/green/blue/red). So I open another form(form 5) when a wild card i

I am making an UNO game application. For this in Form Game Table, if a user plays a Wild card he has to select a color out of (yellow/green/blue/red). So I open another form (form 5) when a wild card is played and use picturebox click event.

Problem - as soon as form5 is executed it returns back to the form4 without waiting for an input from the user. How do I make form5 wait till one of the 4 picturebox is clicked.

My solution -

Set a flag to 0 and use -

while(flag == 0) { }

inside 开发者_如何学Cthe pictureBox_click event i change the flag to 1.

but the problem with the solution is the form4 doesn't wait for form5 to complete and executes itself. This is causing the problem in the algo as a color is to be selected in such a case.

Please suggest something I can do about it.

Thanks, Radhika


I think you should call form5.ShowDialog(). This way form4 will wait till from5 is closed.


I think as you mentioned in your Question title (threading problem) you are calling form5 in a new thread. this is wrong because in your case it is not necessary to run it in a new thread. you should notice that there are two ways to show a form.

1. to call Form.Show()

in this case new thread is initialized and manage new form events, the caller of Form.Show() will continue doing his job;

2. to call Form.ShowDialog()

in this case the caller thread ( in your problem form4 ) will wait and the code will block until the called form (in your problem form5 ) finish it's job, also you can use DialogeResult as return type of Form.ShowDialog().


If you are using Form.Show method, instead that use Form.ShowDialog. This method waits for the input and onclick of your input write code to select and close the new dialog


create an event delegate in Form5()

which will be fired in Form4()

wait i give you code..

           Form5(){
             public delegate void Form5Activated(object sender, System.EventArgs e);
             public event Form5Activated activate;

        //some codes..
        //then 

        if(activate != null){
             System.EventArgs ea = new System.EventArgs();
             activate(this, ea);
        }


    //in form4()

    Form5 x = new Form5();

    Form5 += Form5_activate(thia.activate);
    void activate(object sender, System.Eventargs ea){
     //all ur codes in action listener here

}


}
0

精彩评论

暂无评论...
验证码 换一张
取 消