End Google Ads 201810 - BS.net 01 --> Hi, i have a problem about threads with windows forms.
When users's sessions ended, i want to show a dialog box to inform them to relogin and then to show the relogin form.
It was ok until i use threads in some forms to improve performance.
When a thread reveices a session error, it shows the relogin form. Actualy i use invoke on main thread.
But while the dialog form is shown, another thread can call a service call and receive the same session error and so tries to show the same relogin form. But it is clear that only one instance of relogin form must be shown at the same time. So i have to use a locking mechanism. But since the thread that shows the dialog box is the main thread, this causes the main thread enter wait state. So the application freezes.


public bool Relogin() {
Interlocked.Increment(ref waitingThreadCount);
event1.WaitOne();/*Auto Reset Event*/
if(this.loginResult == null) {/*login result is type of bool?*/
loginResult = ReloginForm.Show();//Runs as modal
}
Interlocked.Decrement(ref waitingThreadCount);
if(waitingThreadCount == 0) {
loginResult = null;
}
event1.Set();
return loginResult.Value;
}