Wysyłanie Argumentów Do Pracownika W Tle?
Powiedzmy, że chcę wysłać parametr int do pracownika w tle, jak można to osiągnąć?
private void worker_DoWork(object sender, DoWorkEventArgs e) {
}
Wiem, kiedy to jest pracownik.RunWorkerAsync ();, nie rozumiem jak zdefiniować w worker_DoWork, że powinien przyjmować parametr int.
8 answers
Zaczynasz TAK:
int value = 123;
bgw1.RunWorkerAsync(argument: value); // the int will be boxed
A następnie
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
int value = (int) e.Argument; // the 'argument' parameter resurfaces here
...
// and to transport a result back to the main thread
double result = 0.1 * value;
e.Result = result;
}
// the Completed handler should follow this pattern
// for Error and (optionally) Cancellation handling
private void worker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
// check error, check cancel, then use result
if (e.Error != null)
{
// handle the error
}
else if (e.Cancelled)
{
// handle cancellation
}
else
{
double result = (double) e.Result;
// use it on the UI thread
}
// general cleanup code, runs when there was an error or not.
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-17 10:50:02
Mimo, że jest to już odpowiedź na pytanie, zostawiłbym inną opcję, która IMO jest dużo łatwiejsza do odczytania:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (obj, e) => WorkerDoWork(value, text);
worker.RunWorkerAsync();
Oraz w metodzie obsługi:
private void WorkerDoWork(int value, string text) {
...
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-17 11:41:14
Możesz przekazać wiele takich argumentów.
List<object> arguments = new List<object>();
arguments.Add(argument 1);
arguments.Add(argument 1);
arguments.Add(argument n);
backgroundWorker2.RunWorkerAsync(arguments);
private void worker_DoWork(object sender, DoWorkEventArgs e) {
List<object> genericlist = e.Argument as List<object>;
extract your multiple arguments from this list and cast them and use them.
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-17 06:53:59
Możesz użyć właściwości DoWorkEventArgs.Argument
.
Pełny przykład (nawet używając argumentu int) można znaleźć na stronie Microsoftu:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-26 16:40:32
Zobacz DoWorkEventArgs.Właściwość Argumentu :
...
backgroundWorker1.RunWorkerAsync(yourInt);
...
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Do not access the form's BackgroundWorker reference directly.
// Instead, use the reference provided by the sender parameter.
BackgroundWorker bw = sender as BackgroundWorker;
// Extract the argument.
int arg = (int)e.Argument;
// Start the time-consuming operation.
e.Result = TimeConsumingOperation(bw, arg);
// If the operation was canceled by the user,
// set the DoWorkEventArgs.Cancel property to true.
if (bw.CancellationPending)
{
e.Cancel = true;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-26 16:49:42
Możesz to wypróbować, jeśli chcesz przekazać więcej niż jeden typ argumentów, najpierw dodaj je wszystkie do tablicy obiektu type i przekaż ten obiekt do RunWorkerAsync () oto przykład :
some_Method(){
List<string> excludeList = new List<string>(); // list of strings
string newPath ="some path"; // normal string
Object[] args = {newPath,excludeList };
backgroundAnalyzer.RunWorkerAsync(args);
}
Teraz w metodzie doWork pracownika tła
backgroundAnalyzer_DoWork(object sender, DoWorkEventArgs e)
{
backgroundAnalyzer.ReportProgress(50);
Object[] arg = e.Argument as Object[];
string path= (string)arg[0];
List<string> lst = (List<string>) arg[1];
.......
// do something......
//.....
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-20 21:26:59
Potrzebujesz RunWorkerAsync(object) Metody i DoWorkEventArgs.Argument właściwość.
worker.RunWorkerAsync(5);
private void worker_DoWork(object sender, DoWorkEventArgs e) {
int argument = (int)e.Argument; //5
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-26 16:40:30
Należy zawsze próbować używać obiektu kompozytowego z konkretnymi typami (używając kompozytowego wzorca projektowego), a nie listy typów obiektów. Kto by pamiętał, czym do cholery jest każdy z tych obiektów? Pomyśl o utrzymaniu kodu później... Zamiast tego spróbuj czegoś takiego:
Public (Class or Structure) MyPerson
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public int ZipCode { get; set; }
End Class
A następnie:
Dim person as new MyPerson With { .FirstName = “Joe”,
.LastName = "Smith”,
...
}
backgroundWorker1.RunWorkerAsync(person)
A następnie:
private void backgroundWorker1_DoWork (object sender, DoWorkEventArgs e)
{
MyPerson person = e.Argument as MyPerson
string firstname = person.FirstName;
string lastname = person.LastName;
int zipcode = person.ZipCode;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-12 13:43:05