Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no way to do this in one method if I correctly understand your needs. Yes, except </p> <pre><code>Application.DoEvents(); </code></pre> <p>The main issue that you want use procedure-based approach but you should message-based. I'll try to explain.</p> <p>You have something like </p> <pre><code>public partial class MyForm : Form { ProgressBar progress; Button startTaskButton; void OnClick_startTaskButton(); </code></pre> <p>You want start some time-consuming task in background using Thread/Process/BackgroundWorker or ThreadPool. And you need that Form interact with user and show him task progress during mentioned task is executing.</p> <p>So you need break your ProgressStaff at least two methods: the first will start task executing and the second will react on task complete event.</p> <p>//Off top: As I see your ThirdPartyObject class doesn't receive instance of Form so you should syncronize manually.</p> <p>So your code will looks like this:</p> <pre><code>void OnClick_startTaskButton() { ProcessStuff(GetTask(), this); } private void ProcessStuff(Task sometask, Form progressform) { if (sometask.foo == "A") DoStuff(); //This one is SYNchronous else { ThirdPartyObject Q = new ThirdPartyObject(); Q.ProgessChanged += delegate(object sender, ProgressChangedEventArgs e) { TaskProgessChanged(sender, e); }; Q.TaskCompleted += delegate(object sender, TaskResult result) { TaskCompleted(sender, result); }; Q.Execute("X", "Y", "Z"); //This one executes ASYNchronous } } void TaskProgessChanged(object sender, ProgressChangedEventArgs e) { if (InvokeRequired) Invoke(TaskProgessChanged, new object[] {sender, e} ); else ShowProgress(e.ProgressPercentage); } void TaskCompleted(object sender, TaskResult result) { if (InvokeRequired) Invoke(TaskComplete, new object[] {sender, result} ); else { MessageBox.Show("Task is completed with result :" + result.ToString()); } } </code></pre> <p>======================</p> <p>Well, if you <strong>really</strong> want to fell like your ProcessStuff method executes synchronously and in the same time Form reflect progress and you can't divide ProcessStuff into two methods - you may modify my solutions into the following way:</p> <pre><code>private void ProcessStuff(Task sometask, Form progressform) { ProcessStuff(sometask, progressform, true); } private void ProcessStuff(Task sometask, Form progressform, bool isNewTask) { if (isNewTask) if (sometask.foo == "A") DoStuff(); //This one is SYNchronous else { ThirdPartyObject Q = new ThirdPartyObject(); Q.ProgessChanged += delegate(object sender, ProgressChangedEventArgs e) { progressform.ShowProgress(e.ProgressPercentage); }; Q.TaskCompleted += delegate(object sender, TaskResult result) { ProcessStuff(sometask, this, false); }; Q.Execute("X", "Y", "Z"); //This one executes ASYNchronous } else { if (InvokeRequired) Invoke(TaskComplete, new object[] {sender, result, isNewTask} ); else { //Task is completed MessageBox.Show("Task is completed"); } } } </code></pre> <p>But imho that will be bad style ;)</p> <p>(I don't check is this code compiled and suppose you haven't problems with that)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload