GUI updates only after the worker thread has ended

I have a Windows Form application and managed DLL in one solution. DLL contains some time consuming functions during which I wish to update the Form contents (callback from the DLL to the Form with progess updates). I have the following code:

Form code, where I initialize the DLL and give it a callback function in the Initialize method. I also start a separate Thread to periodicly check the message_queue for new messages from the DLL. The DLL function is also called in a separate Thread (non blocking for the UI).

private LibraryDLL library_dll;
private ConcurrentQueue<string> message_queue;

public MainForm()
{
    InitializeComponent();
    library_dll = new LibraryDLL();
    message_queue = new ConcurrentQueue<string>();
    library_dll.Initialize(ProcessMessage);

    new Thread(() =>
    {
        Thread.CurrentThread.IsBackground = true;
        string message;
        if (message_queue.TryDequeue(out message))
        {
            PrintMessage(message);
        }
    }).Start();
}

private void ProcessMessage(string message)
{
    message_queue.Enqueue(message);
}

private void PrintMessage(string message)
{
    this.Invoke((MethodInvoker)delegate
    {
        listBox_rows.Items.Add(message);
    });            
}

private void button_send_Click(object sender, EventArgs e)
{
    new Thread(() =>
    {
        Thread.CurrentThread.IsBackground = true; 
        library_dll.DoWork();           
    }).Start();
}

In DLL code, I use the callback method to report progress:

private CallBack callback;
public delegate void CallBack(string message);

public LibraryDLL() { }

public void Initialize(CallBack callback)
{
    this.callback = callback;
}

public void DoWork()
{
    callback("working...")
    Thread.Sleep(500);
    callback("working...")
    Thread.Sleep(500);
    callback("working...")
    Thread.Sleep(500);
}

My problem is, that instead of string "working" appearing every 500ms, it appears 3 times after 1500ms (only after the Thread in which the DoWork method is running ends). I also tried the Invalidate()-Update()-Refresh() sequence in the Form's PrintMessage function, but without any effect.

Thanks for the advice!

EDIT1:

I modified the code to use the BackgroundWorker, however, the problem remains (nothing for 1500ms, than all 3 strings at once).

BackgroundWorker bck_worker;

public MainForm()
{
    InitializeComponent();
    library_dll = new LibraryDLL();
    library_dll.Initialize(bck_worker);

    bck_worker = new BackgroundWorker();
    bck_worker.ProgressChanged += new ProgressChangedEventHandler(bckWorker_ProgressChanged);
    bck_worker.WorkerReportsProgress = true;
    bck_worker.WorkerSupportsCancellation = true;
} 

private void bckWorker_DoWork(object sender, DoWorkEventArgs e)
{
    library_dll.DoWork();
}

private void bckWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    PrintMessage((string)e.UserState);
}

private void button_send_Click(object sender, EventArgs e)
{
    bck_worker.DoWork += new DoWorkEventHandler(bckWorker_DoWork);
    bck_worker.RunWorkerAsync();
}

private void PrintMessage(string message)
{
    listBox_rows.Items.Add(message);
}

And the DLL:

private BackgroundWorker bck_worker;

public LibraryDLL() { }

public void Initialize(BackgroundWorker bck_worker)
{
    this.bck_worker = bck_worker;
}

public void DoWork()
{
    bck_worker.ReportProgress(25, "working...");        
    Thread.Sleep(500);
    bck_worker.ReportProgress(50, "working...");
    Thread.Sleep(500);
    bck_worker.ReportProgress(75, "working...");
    Thread.Sleep(500);
}

EDIT2:

OK, I now tried to add the Invalidate-Update-Refresh sequence at the end of the PrintMessage function and it finaly works (with the BackgroundWorker approach)!

Upvotes: 0

Views: 75

Answers (1)

vhr
vhr

Reputation: 1664

Use background worker and workers's report progress to update your UI: background worker doc

Upvotes: 2

Related Questions