473,657 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_cli ck(object sender, System.EventArg s e)
{
busObj.StartLon gProcess();
}
}

public class BusObj
{
public void StartLongProces s( MyForm ui)
{
ui.Invoke(ui.Up dateProgress);
// 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 1330
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.Threadin g;

// Spawn a thread to do some work
System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
WaitCallback(Wo rkerMethod));
protected void WorkerMethod(ob ject o)
{
// thread pool thread works its behind off.......

TS_WorkDone(tru e);
}
public delegate void UlSimpleBoolDel egate(bool bGoodResult);

protected void TS_WorkDone(boo l bGoodResult)
{
if (this.InvokeReq uired == false)
{
// Update the UI here....
}
else this.BeginInvok e(new UlSimpleBoolDel egate(TS_WorkDo ne), 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.BeginInvok e(new MethodInvoker(T S_MethodWithNoP arameters),
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******** *****@TK2MSFTNG P14.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_cli ck(object sender, System.EventArg s e)
{
busObj.StartLon gProcess();
}
}

public class BusObj
{
public void StartLongProces s( MyForm ui)
{
ui.Invoke(ui.Up dateProgress);
// 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12069
by: Tobias | last post by:
Hi! I had Apache 2.0.47 and PHP 4.3.0 DEV running successfully on a W2k Server. For some reason, I couldn't get PHP to read XML-Attributes with the DOM XML -functions. So I thought, it would be time to update PHP to a newer version. So I simply replaces the old php-files with the new ones and of course kept the php.ini. But now, PHP doesn't work anymore. As soon as I request a page with php code, I get a "document contains no data"...
6
5764
by: Hennie de Nooijer | last post by:
Hi, Currently we're a building a metadatadriven datawarehouse in SQL Server 2000. We're investigating the possibility of the updating tables with enormeous number of updates and insert and the use of checkpoints (for simple recovery and Backup Log for full recovery). On several website people speak about full transaction log and the pace of growing can't keep up with the update. Therefore we want to create a script which flushes the...
1
1401
by: Joshua Russell | last post by:
Firstly my main method is like this: static void Main(string args) { // New Form Thread FormHandler myFormHandler = new FormHandler(); ThreadStart myThreadStart = new ThreadStart(myFormHandler.ShowForm); Thread formWorkerThread = new Thread(myThreadStart); formWorkerThread.Start();
11
2220
by: DW | last post by:
I've gotten this question a couple of times in interviews and I don't know what they are looking for: How do you update a control's property, such as a textbox.text property, from a thread, in .NET? It seems to me you just update it normally. What are the interviewers looking for here? Thanks. - W
10
8431
by: Curious | last post by:
Hi, I have a thread running, and at a point in this thread I am calling a method to update GUI controls. From the net I have read that updating GUI controls from the worker thread is bad practice. Thread myThread = new Thread(new ThreadStart(Worker));... myThread.Start();
9
23068
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated to a pool. If I assign only one application to a applicaton pool and have multiple worker processes assigned to that pool. Will my application be processed by many worker processes?
6
4984
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
7
2683
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have a lock pending, for example. I want to be able to stop a thread temporarily, and then optionally resume it or stop it for good.
4
1882
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That part is easy. The part that I am having trouble with is using the code in a form and having a Label's Text property update as a new directory or file is found. What I keep getting is the work being done before it is shown to the user via the...
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.