I can not figure out how to still pull the method into a separate thread. I ask for help in chewing information about working with threads.

There is a moment when an external file of a sufficiently large size opens in the application, and at this moment animation is performed. Well, the animation, as well as the whole application hangs at this moment (well, it goes without saying ...) It is necessary that the animation continue to work, without jerks and brakes, while the file is opened in a separate stream.

.NET 4 (above impossible)

studio 2012, if you need I can throw off the project ...

  • 3
    Tell me more specifically what you need. There are a million methods to execute code in another thread. - VladD
  • Yes, it would also be nice to argue why it is for WPF, since the work with the threads does not depend on the GUI (but the GUI depends on how to handle the controls in the threads) - Spawn
  • Is the studio used 2012 or 2010? - Spawn
  • one
    The project is not needed) To use the async / await pattern, which I mentioned in the answer, there is an official library with the .NET Framework 4.0 - nuget.org/packages/Microsoft.Bcl.Async - Spawn

4 answers 4

To begin, structure your program. The part that deals with the presentation (View) must be separated from the control part (ViewModel), and from various engines (Model).

When the ViewModel decides to open the file, the request to open and parse the content should go to the file parsing engine and build the object model. The model itself may well be single-threaded, but let business logic be engaged in transferring information between threads.

So, at the model level, your code is simple and linear:

public Document ReadFromFilename(string filename) { using (var fs = new FileStream(filename, FileMode.Open)) return ReadFromStream(fs); } protected Document ReadFromStream(IStream stream) { ... 

At the VM level, you need a little more:

 public void async OpenDocumentAsync(string filename) { try { // установить статус ожидания this.ReadStatus = ReadStatus.Reading; // распарсить документ var doc = await DocumentHelper.GetDocumentAsync(filename); // создать VM-структуру var vmDoc = new VM.Document(doc); // установить текущую страницу и создать соответствующие подструктуры await vmDoc.SwitchToPageAsync(0); // доложить, что всё в порядке this.ReadStatus = ReadStatus.Ready; // не забыть обновить свойство DataContext'а this.Document = vmDoc; } catch (<что нужно>) { // доложить, что что-то пошло не так this.ReadStatus = ReadStatus.Failed; } } 

The DocumentHelper.GetDocumentAsync method will look something like this:

 public Task<Model.Document> GetDocumentAsync(string filename) { var t = new Task(() => model.ReadFromFilename(filename)); t.Start(taskSchedulers[model]); return t; } 
  • unfortunately my knowledge in C # is very limited ... so I do everything in one place as they say ... I thought about multithreading only because the program will be used on a non-agile hardware. More precisely, there HDD are clogged so that it reads all very slowly - blackoverdose
  • If you divide the program into layers, it will become easier. This super megagur can easily and correctly do everything in one function, while mere mortals must break logic into levels. - VladD
  • I do not have enough knowledge for this, and the time is limited ... I understand that it will be easier and better - blackoverdose
  • 2
    If programming is your work tool, you must be proficient in them. Well, or own the gift of convincing customers that "slows down and crashes - this is normal." Therefore, for the next project, I would advise you to learn MVVM. - VladD
 new Thread(() => MyMethod()).Start(); 

Method in a separate thread ... What's the problem?

With data:

 int i = 0; new Thread(() => { MessageBox.Show(i.ToString()); }).Start(); 

If .Net Framework 4 or higher, then you wrote an example below. Also, as for the example below, read about async / await.

  • so he does not accept any data - blackoverdose

At choice:

 Task.Run(()=> /*code*/); Task.Factory.StartNew(Method, params); 

    Here I described two ways GUI interacts with workflows.