0
in the case the whole program is written without task and only one excerpt is with task, I wanted after that piece it returned to the "main thread". The code below is inside a for, and serves to open a save window, the task specifically operates this window, it starts even before the window is opened because when it opens the program "stops reading the code". Remember that the save window is not a savefileDialog, it is opened using the webbrowser tool of c# calling a click on a download button.
Clipboard.SetText(@"C:\ARQUIVOS\");
Task t = Task.Factory.StartNew(() =>
{
Thread.Sleep(5000);
SendKeys.SendWait("{HOME}");
SendKeys.SendWait("^{V}");
Thread.Sleep(1000);
SendKeys.SendWait("{ENTER}"); //esse enter salva o arquivo, depois disso o programa fica em "standby" pois acaba a thread e nao volta para o codigo principal
Task.Delay(3000);
});
await Task.Delay(3000);
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{ENTER}"); //abre janela de salvar
FileIOPermission.RevertAll(); //permissões para a janela
Task.Delay(3000);
EDIT: I got it with the following code:
private async Task OperaJanelaSaveAs()
{
Clipboard.SetText(@"C:\ARQUIVOS\");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{ENTER}"); //abre janela de salvar
await Task.Delay(4000);
SendKeys.SendWait("{HOME}");
SendKeys.SendWait("^{V}"); //cola o caminho do arquivo (clipboard)
await Task.Delay(3000);
SendKeys.SendWait("{ENTER}"); //salva o arquivo
await Task.Delay(4000);
}
and calling the delay function to wait for the click on the download button and await
await Task.Delay(6000);
await OperaJanelaSaveAs();
This is very confusing, put your code to understand better
– Ricardo Pontual
And that’s not exactly why you use a
Task
?– Maniero
@Maniero yes but the code is terminated after the task
– Milton Machado Pereira
@Ricardopunctual I put
– Milton Machado Pereira
I saw no sense in using this task with your need.
– Victor Laio
That pile of
Thread.Sleep()
already shows that this code makes no sense and is doing wrong things. We do not know exactly what you want to do, but it seems wrong choice– Maniero
@Victorlaio as I said when I open the save window the program understands that there is no more code to read so the task is already running and operates the window
– Milton Machado Pereira
I suggest not to put sensitive data in the examples. For example IP!
– phduarte
@phduarte Dude, it’s a local IP, there’s nothing sensitive about it.
– Jéf Bueno
@Miltonmachadopereira Try [Dit] your question and be clearer. Explain what you intend to do.
– Jéf Bueno
@LINQ sees if it gets better
– Milton Machado Pereira
@LINQ If I worked at the same company now I would know where the Billing files are saved!
– phduarte
@Miltonmachadopereira this window that opens to save. It is a web page or a Windows window?
– phduarte
@phduarte windows window opened from a boot of an open site in the webbrowser tool of c#
– Milton Machado Pereira
@Miltonmachadopereira you are probably using this Task because you don’t know exactly how long it will take for the Save window to appear, and you don’t want the system to be in a "Not responding" state or anything. Correct?
– phduarte
@phduarte to expect sleeps and delays. The problem in question is that after you save the file nothing else happens, and as I said the code in question is along with more code everything inside a for and is not going to the next repeat.
– Milton Machado Pereira
@Miltonmachadopereira as he is having no problem with the status Not responding and is using Sleeps in several snippets, I can’t see the benefit of using the multithreads. It seems to be getting in your way more than helping. I usually do things similar to saving files using thread, but I don’t use Sleep or Sendkeys, I do it using Windows' own api, I also use a Boolean variable to control the completion before I proceed to code execution.
– phduarte