473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Calling BackgroundWorker synchronous

Hi,

this may be a stupid question:
How can I can call the DoWork-function of a BackgroundWorker
synchronous?
Or in other words:
How can I extend the BackgroundWorker class with a function
RunWorkerSync()?
I want to write a class that I can call functions synchronous and
asynchronous.
e.g.
This is the code for running the function DoWork asynchronous
(incomplete);

void TestAsync()
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += DoWork;
worker.RunWorkerCompleted += RunWorkerCompleted;
...
worker.RunWorkerAsync(Param);
...
}

void DoWork(object sender, DoWorkEventArgs e)
{
Object Param = (Object) e.Argument;

// do something

e.Result = something;
}

void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
m_Result = (Object) e.Result;
}

This is the code for running synchronous (incomplete);
void TestSync()
{
// call synchronous
m_Result = DoWork(Object Param);
}
How can I combime this?
Thanks for your help!

Feb 14 '06 #1
3 9925
If you don't mind polling to check if the process is completed, then
define your own class which inherits from BackgroundWorker

public class MyBackgroundWorker : BackgroundWorker
{
public DoWorkSynchronous(object Param)
{
DoWork(Param);

while(IsBusy)
sleep(100);

return;
}
}
DISCLAIMER: Code for display purposes, not tested
I hope this helped.

Sincerely,
Bobby

Feb 14 '06 #2
Hello, Pro1712!

Why do you need such a behavior, if you need sycnhrounous behavior then you can simply call DoWork method...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 14 '06 #3
Hello, bo*********@gmail.com!

b> public class MyBackgroundWorker : BackgroundWorker
b> {
b> public DoWorkSynchronous(object Param)
b> {
b> DoWork(Param);

b> while(IsBusy)
b> sleep(100);

b> return;
b> }
b> }
b> DISCLAIMER: Code for display purposes, not tested

b> I hope this helped.

IMO it is better to wait and not to poll.

public DoWorkSynchronous(object Param)
{
DoWork(Param);

internalSyncEvent.WaitOne(timeout, false);
return;
}

DoWork(param)
{
//do work
internalSyncEvent.Set(); //signal that the work is done
}

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Al Christoph | last post by:
I have a program that mixes wizard like behavior with random access to the various dialogs in the wizard. I do this by having each step map to a toolstripmenuitem. Users can randomly choose the...
2
by: Ulf | last post by:
Hi, I have some problems with a BackgroundWorker when calling a WebRequest : private void bgrdTask_DoWork(object sender, DoWorkEventArgs e) { e.Result =...
5
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
11
by: GVN | last post by:
Hi All, Can anyone guide me when asynchronous method calls will be benificial? Are there any disadvantages of using asynchronous calls? Thanks,
1
by: Bob | last post by:
Hi, I am having trouble seeing how this bolts together. The UI starts a process which involves a long running database update. All Database activity is handled by a class called DT. DT has a...
14
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to...
0
by: Chris | last post by:
I would like to be able to pass the BackgroundWorker object and DoWork Event Args to a second function (third function?) and be able to still report the progress. I'm getting the following...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
9
by: Pubs | last post by:
Hi all, I want to call a function with some intial parameters with in a thread. At the end of the function execution it should return a value to the caller. Caller is outside the thread. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.