my own dialogue box

  • Thread starter Thread starter Christ Kennedy
  • Start date Start date
C

Christ Kennedy

Guest
I'm making(trying) to make a dialogue box similar to the openFileDialogue where the program flow of the Main Thread is stalled until the OPF.ShowDialogue() function stalls while running a separate thread.

I have a OpenFileDialogue_GUi class which uses a semaphore and a background worker.

the background worker is first launched to generate a separate form with the necessary user-interface

then the semaphore stalls the main thread until the user click's one of the buttons on the back-ground thread's form.

then it releases the semaphore and the result is sent back to the calling function.

sounds nice in theory ... but there's a problem. the background-worker not displaying its form and its not working.

here's the code :

public class openfiledialogue_Gui
{
Semaphore semStall = new Semaphore(0, 1);
BackgroundWorker bck = new BackgroundWorker();

public openfiledialogue_Gui()
{
bck.WorkerSupportsCancellation = true;
bck.DoWork += Bck_DoWork;
bck.RunWorkerCompleted += Bck_RunWorkerCompleted;
}

public enum enuResult { Ok, Cancel, _num };
public enuResult ShowDialogue()
{
bck.RunWorkerAsync();
semStall.WaitOne();
return eResult;
}

enuResult eResult = enuResult.Cancel;

void EventOk_click(object sender, EventArgs e){eResult = enuResult.Ok;}

void EventCancel_click(object sender, EventArgs e){eResult = enuResult.Cancel;}

private void Bck_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){semStall.Release();}

private void Bck_DoWork(object sender, DoWorkEventArgs e)
{
Form frmShow = new Form();
Button btnOk = new Button();
btnOk.Text = "ok";

Button btnCancel = new Button();
btnCancel.Text = "Cancel";

btnOk.AutoSize
= btnCancel.AutoSize
= true;

frmShow.Controls.Add(btnOk);
frmShow.Controls.Add(btnCancel);

frmShow.ControlBox = false;

btnOk.Location = new Point(100, 25);
btnCancel.Location = new Point(100, 75);

btnCancel.Click += EventCancel_click;
btnOk.Click += EventOk_click;
frmShow.Show();

}

public string strFilename = "this is a test";
public string Filename
{
get { return strFilename; }
}
}

is there any way to make this work?

Christ


my code is perfect until i don't find a bug

Continue reading...
 
Back
Top