Need help implementing multithreading in C #

I am creating an application in which for some reason, when I click the button, I need to initiate a new form and at the same time create a new document in google docs. I have successfully completed the above, but while the application is busy creating a new document in google docs at the moment when the newly loaded UI user interface freezes. I read somewhere that this can be avoided if I use multithreading. So now I want to ask if I should create two streams, and in one of them I should put the code to create a new form, and in the other I should put the code to create a document in a google document. Or should I just create in the stream where I have to put the code to create a new google document,and let the new form creation code be in the main process? Also What would be the easiest way to implement threads in already written code? Please read some reading material if necessary.

+1
source share
5 answers

You really have a lot of options.

(1) BackgroundWorker. If you really need the simplest programming model for asynchronous operation in WinForms, this will be the case. It is usually used to perform an asynchronous task and report progress, but you do not need to report progress if you do not need it.

(2) .. , , , . , BackgroundWorker. , - , WinForms.

(3) . TPL WinForms, .

(4) Async Await. , .NET 4.5, # 5.0 # 5.0, Visual Studio 11, BETA . , .

(5) ISynchronizedInvoke . , .

, . , , , , - .

+6

, , , (TPL) ( BackgroundWorker) .

/ ( ), , , Google Doc . , TPL, - ( )

// In click event.
MyForm newForm = new MyForm();
newForm.Show();

Task googleDocTask = Task.Factory.StartNew(() =>
{
    // Do your stuff with Google Docs.
    // Note you cannot access the UI thread from within this delegate.
});

# . .

TPL . .

, .

+2

. BackgroundWorker. .

:

private BackgroundWorker googleDocWorker = new BackgroundWorker();

:

googleDocWorker.DoWork += new DoWorkEventHandler(googleDocWorker_DoWork);
googleDocWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(googleDocWorker_RunWorkerCompleted);

:

void googleDocWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // You can use this to alert you that the google doc is created.
}

void googleDocWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Create google doc here.
}

, google:

googleDocWorker.RunWorkerAsync();

, BackgroundWorker, , . . :

googleDocWorker.RunWorkerAsync(new object[] { "doc name", contents });

, _DoWork:

void googleDocWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Create google doc here.
    object[] args = (object[])e.Argument;
    String docName = (string)args[0];
    SomeClass contents = (SomeClass)args[1];
}

, URL- , _RunWorkerCompleted _DoWork:

void googleDocWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Create google doc here.

    ...

    e.Result = myURL;
}

URL-, RunWorkerCompleted, , DoWork.

void googleDocWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // You can use this to alert you that the google doc is created.
    String docURL = (String)e.Result;
}

, ! (

+2

. , google ( ).

0

The easiest way is to use BackgroundWorker or use ThreadPool. Threadpool is easier if your main user interface does not care about completing other tasks.

0
source

All Articles