tags:

views:

52

answers:

2

Hi, this is probably a nobraner, but I can't figure it out. I've got this function which converts wav files to mp3. I call this from my button_click handle. The thing is, when the conversion is started, the rest of the code in the button_click handler continue, while the conversion is happening in a different thread.

Now, I need the rest of the code in the button_click handle so continue to try until a boolean is true, so that I know that the conversion is done before the rest of the code continues.

I've tried using Do While but it didn't seem to do the trick. Perhaps it's just me though..

+5  A: 

Is this a client application? Sounds like a great application for BackgroundWorker.

To execute a time-consuming operation in the background, create a BackgroundWorker and listen for events that report the progress of your operation and signal when your operation is finished.

BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler
                                   (bw_RunWorkerCompleted);
....

private void button1_Click(object sender, EventArgs e)
{
    bw.RunWorkerAsync(filename);
}

static void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = (BackgroundWorker)sender;
    string filename = (string)e.Argument;
    e.Result = DoConversion(filename);
}

static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    label.Text = "Done: " + e.Result.ToString();
    DoSomethingWhenConversionComplete();
}
Michael Petrotta
Oooh, nice! :) I'll take a look at this. Looks very promising! :)
Kenny Bones
+1  A: 

This is called a spin-wait and is not the best way to accomplish your task:

// IsConversionComplete will be set by some other thread
while(!IsConversionComplete){
  Thread.Sleep(100);
}
// carry on

A much more efficient solution requires a synchronization structure like a mutex or use of events.

Michael Haren
Well, I'm not a super programmer, so I'm not really sure what's going on technically. But I do know that a thread is closed at the end of the conversion. Dont think backgroundworker is used though. But, isn't is possible to just do: "Do while condition = false --> Do while condition = true -->'Execute code --> Loop --> Loop ? Giving that I set the condition to True in the other function converting the mp3? Or is this a stupid way of doing it?
Kenny Bones
I just thought that trapping the code in a loop while the mp3 is converting could work?
Kenny Bones
@Kenny: it will work, but spinwaiting the UI thread (and it sounds like you're building a client application) will make your UI unresponsive during the conversion, and make it look like it's hung.
Michael Petrotta
You could hack something together, like a timer that checks for completion every 100ms, but at that point, it's easier (and definitely cleaner) to use an event-based solution (BackgroundWorker, or a thread with a callback, essentially what BW is doing under the covers).
Michael Petrotta
Ok, just read a little about backgroundworker. Seems very smart :) Would that require alot of rewriting of code? How would that work exactly? Is backgroundworker something like a thread? Or is it just something I can use along with other stuff, just to raise events?How would adding a backgroundworker look like in this particular case? The whole process works like this, calling these subs;ConvertWavToMp3()UploadFile()
Kenny Bones
Agreed. This will be tricky the first time since you haven't done it before; it will be worth it though as an event-based solution like @Michael Petrotta provided is the right way to do this and once you figure it out, it's quite elegant.
Michael Haren