471,305 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Updating my UI from a worker function

I have been doing research on updating the UI while doing time intensive
processing. Basically, I have a click event in my Form that will call a
member function on one of my business objects, that function that gets
called takes a long time. I read that I can't make calls to the UI from
another thread directly, so I had planned on doing the following(tell me if
this is a bad design or could be better);
1) Click event calls business object method
2) Business object starts a new thread and executes the time intensive code;
3) Business object has a reference to the UI (Form) class and uses Invoke to
call the UI's refresh function
Pseudo code
class MyForm : Form
{
BusObj busObj;

public void UpdateProgress(string str)
{
// UI STUFF
}

public void LongProcess_click(object sender, System.EventArgs e)
{
busObj.StartLongProcess();
}
}

public class BusObj
{
public void StartLongProcess( MyForm ui)
{
ui.Invoke(ui.UpdateProgress);
// do other stuff
}
}

// end fake code

Does this seem like the way that most people would do it? I'm more asking
about the general approach vs small details. For example, I just read that
I need to use a delegate with Invoke, so I couldn't call UpdateProgress
directly.

I'm just trying to nail down a solution so that I can easily launch my
various long process' and still get UI updates from them.

Thanks for reading, I hope what I want to do is clear.
-Steve

Dec 9 '05 #1
1 1242
sklett,

Your plan will work fine and it's a matter of personal preference as to how
to implement it. If your just doing some simple work, I personally would
just spawn off a thread pool thread on a method within the form's class like
so:

using System.Threading;

// Spawn a thread to do some work
System.Threading.ThreadPool.QueueUserWorkItem(new
WaitCallback(WorkerMethod));
protected void WorkerMethod(object o)
{
// thread pool thread works its behind off.......

TS_WorkDone(true);
}
public delegate void UlSimpleBoolDelegate(bool bGoodResult);

protected void TS_WorkDone(bool bGoodResult)
{
if (this.InvokeRequired == false)
{
// Update the UI here....
}
else this.BeginInvoke(new UlSimpleBoolDelegate(TS_WorkDone), new
object[] { bGoodResult });

// Note: If you don't need to pass anything back to the UI thread, use
the MethodInvoker delegate
// since it is faster than any custom delegate that you can
create.....like so
// else this.BeginInvoke(new MethodInvoker(TS_MethodWithNoParameters),
null);
}

If its more complex, you can separate the work out into a new class and have
it spawn a thread pool thread to do the work....

It's up to you.

Dave
"sklett" <as**@fkd.com> wrote in message
news:Ow*************@TK2MSFTNGP14.phx.gbl...
I have been doing research on updating the UI while doing time intensive
processing. Basically, I have a click event in my Form that will call a
member function on one of my business objects, that function that gets
called takes a long time. I read that I can't make calls to the UI from
another thread directly, so I had planned on doing the following(tell me if
this is a bad design or could be better);
1) Click event calls business object method
2) Business object starts a new thread and executes the time intensive
code;
3) Business object has a reference to the UI (Form) class and uses Invoke
to call the UI's refresh function
Pseudo code
class MyForm : Form
{
BusObj busObj;

public void UpdateProgress(string str)
{
// UI STUFF
}

public void LongProcess_click(object sender, System.EventArgs e)
{
busObj.StartLongProcess();
}
}

public class BusObj
{
public void StartLongProcess( MyForm ui)
{
ui.Invoke(ui.UpdateProgress);
// do other stuff
}
}

// end fake code

Does this seem like the way that most people would do it? I'm more asking
about the general approach vs small details. For example, I just read
that I need to use a delegate with Invoke, so I couldn't call
UpdateProgress directly.

I'm just trying to nail down a solution so that I can easily launch my
various long process' and still get UI updates from them.

Thanks for reading, I hope what I want to do is clear.
-Steve

Dec 9 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tobias | last post: by
1 post views Thread by Joshua Russell | last post: by
10 posts views Thread by Curious | last post: by
7 posts views Thread by Charles Law | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.