updating the UI with progress bar while Business Layer in process

Hi

I'm developing a windows based c# application.
How do I update the UI with progress bar while Business Layer in process ?
where my BL is in a seprate Machine in the same network.

I tried by placing a thread to update the progress bar and at the same time BL function will also execute. But this doesn't seems to be working properly.

can any one sugest any methodolgies for this ?

Regards
Karthik

[422 byte] By [Karthikeyan] at [2007-12-16]
# 1
Hi,
I guess you should lookup on Socket Class. So that your two processes (.exe's) could communicate over the network...
cheers,
Paul June A. Domag
PaulDomag at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 2

Hi Paul

Thanks for the reply.

I'm not having 2 exe.

I'm having a exe (that is the client) the UI and one DLL which is the Business Layer resides in a different machine. My code would look like:

MyUI.cs

-

BLClass1 blc = new BLClass1();

blc.SomeTimeTakingOperation();

since the execution control will be in SomeTimeTakingOperation() simultaneously I'm not able to update my MYUI.cs. I can only update the progress bar only after executing blc.SomeTimeTakingOperation(); or before this call.

So, Instead I tried by having a separate Thread and I'll update the UI's progress bar there and simultaneously BL method will execute. but this doesn't work.

can you please tell me how can I solve this by Socket class ?

Karthikeyan at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
how are your applications communicating? Sockets, Remoting, Web Services?

And what does not work correctly? Sample code?

Remember one thing that the ProgressBar runs in the context of the Main Form and you cannot make changes to it directly from another thread. You need to make changes to it through delegates to ensure it's updated in the right thread context.

SaurabhNandu at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
Hi,
Sorry I misunderstood your scenario.
IMO, the problem doesn't lie in the client. I guess the Business Layer must have an internal counter which acts like a countdown to when the operation ends. You could either expose an event that occurs everytime the value of the internal counter changes. Or you could just expose the internal counter, and create a thread in your client and use that internal counter's value to move the progressbar...

Hope that made sense...
cheers,
Paul June A. Domag

PaulDomag at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
My application uses Remoting to call BL methods.
following is my code for UI

MyUI.cs
-
private bool IsBusinessEnds = false;
private
void ProgressBarInc()
{
while(true)
{
//the value of the complexData is updated in BL. If it got updated I assume that BL job is finished and I make my progress bar to its max value.
if ( this.complexData.Lenght > 0
{
this.fileGenerationprogressBar.Value = 1000;
break;
}
else
{
this.fileGenerationprogressBar.Value += 1;
}
}
}

private void OkButton_Click(object sender, System.EventArgs e)
{
Thread prog = new Thread(new System.Threading.ThreadStart(ProgressBarInc));
prog.Start();

//this is the call to the controller where in turn it will call the BL method which is done by remoting.
this
.accountController.GenerateReminderFile
(
ref this.accountDataSet, ref this.complexData);
}

This method of doing is not sync up properly with progress bar.
And the progress bar is not updating properly.

how can I make changes to it through delegates ?
and how will I know whether BL method ends ?

and I can't use database for counters.

Karthikeyan at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 6
For starters you need to run your business logic in a separate thread, otherwise your GUI has no hope of updating while processing is occuring.
dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 7

presently I'm having a seprate Thread for updating the progress bar, and not for the BL. So, you r saying me to create a seprate Thread for BL and not for the UI.
Is it Right ?
So I'll call my BL method into a Thread is it right ?

Karthikeyan at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 8
Yes. If your BL is running in your GUI thread, then the GUI will not redraw until the BL is finished. So, even if you somehow update what's supposed to be on the screen, it will not get redrawn until the BL is done. You need to run the BL in a separate thread so that the GUI thread will be free to respond to the changes in what's supposed to be displayed.
dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 9
Even this doesn't work.
I belive that this can not be achived by using Threads and can be solved only by Call back functions thru Events & Delegate. I'm trying that way and once I got the solution I'll post it.
Karthikeyan at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 10

Uhmm, it "didn't work" because? OK, here it is... and yes, it works... TWO different ways! Take your pick!



public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
BL bl = new BL();

// This works ONLY because of the Thread.Sleep() call within BL.Go().
bl.Go(this);

// This is the better way, (it doesn't require the Thread.Sleep() call)
// although you also have to manage shutting
// the thread down while it's in the middle of processing.
Thread thread = new Thread(new ParameterizedThreadStart(bl.Go));
thread.Start(this);
}

public void UpdateProgress(int progress)
{
// Invoke is required if UpdateProgress was called from a separate thread.
if (InvokeRequired)
Invoke( (MethodInvoker) delegate () { progressBar1.Value = progress; });
else
progressBar1.Value = progress;
}
}

public class BL
{
public void Go(Object param1)
{
Form1 form = (Form1)param1;

for (int i = 0; i < 100; i++)
{
form.UpdateProgress(i);
Thread.Sleep(10);
}
}
}

dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...