Я использую async / await для задержки между выполнением метода.
Существует код
public bool DoPerformCommandOnClient(string commandStr, bool isOnPath, string folderPath, string ext, string doneMessage, string excludeString)
{
Task.Run(() => DoOpenProgressBarWindowWithDelay(2000));
return GetLogic().PerformCommandOnClient(commandStr, isOnPath, folderPath, ext, doneMessage, excludeString);
}
public async void DoOpenProgressBarWindowWithDelay(int delay)
{
BeginInvoke((Action)(() => {
DoOpenProgressBarWindow();
}));
await Task.Delay(delay);
}
Итак, я хотел бы выполнить метод DoPerformCommandOnClient
затем после 2 se c delay выполните следующую строку
return GetLogic().PerformCommandOnClient(commandStr, isOnPath, folderPath, ext, doneMessage, excludeString);
Но вместо этого этот метод DoPerformCommandOnClient
выполняется без задержки.
Затем я попытался добавить Thread.Sleep(2000)
между двумя строками
public bool DoPerformCommandOnClient(string commandStr, bool isOnPath, string folderPath, string ext, string doneMessage, string excludeString)
{
DoOpenProgressBarWindow();
Thread.Sleep(2000);
return GetLogic().PerformCommandOnClient(commandStr, isOnPath, folderPath, ext, doneMessage, excludeString);
}
Но этот подход останавливает мой пользовательский интерфейс, мне не нужно останавливать мой пользовательский интерфейс.
Итак, вопрос в том, как сделать задержку между двумя методами?
РЕДАКТИРОВАТЬ
теперь код выглядит следующим образом
public bool DoPerformCommandOnClient(string commandStr, bool isOnPath, string folderPath, string ext, string doneMessage, string excludeString)
{
Task.Run(() => DoOpenProgressBarWindowWithDelay(2000)).Wait();
return GetLogic().PerformCommandOnClient(commandStr, isOnPath, folderPath, ext, doneMessage, excludeString);
}
public async void DoOpenProgressBarWindowWithDelay(int delay)
{
BeginInvoke((Action)(() => {
DoOpenProgressBarWindow();
}));
await Task.Delay(delay).ConfigureAwait(false);
}
EDIT2
Теперь код выглядит следующим образом
public async Task<bool> DoPerformCommandOnClient(string commandStr, bool isOnPath, string folderPath, string ext, string doneMessage, string excludeString)
{
await DoOpenProgressBarWindowWithDelay(2000).ConfigureAwait(false);
return GetLogic().PerformCommandOnClient(commandStr, isOnPath, folderPath, ext, doneMessage, excludeString);
}
public async Task<bool> DoOpenProgressBarWindowWithDelay(int delay)
{
BeginInvoke((Action)(() =>
{
DoOpenProgressBarWindow();
}));
await Task.Delay(delay).ConfigureAwait(false);
return true;
}
EDIT3
public async Task<bool> DoPerformCommandOnClient(string commandStr, bool isOnPath, string folderPath, string ext, string doneMessage, string excludeString)
{
await DoOpenProgressBarWindowWithDelay(2000).ConfigureAwait(false);
return GetLogic().PerformCommandOnClient(commandStr, isOnPath, folderPath, ext, doneMessage, excludeString);
}
public async Task DoOpenProgressBarWindowWithDelay(int delay)
{
BeginInvoke((Action)(() =>
{
DoOpenProgressBarWindow();
}));
await Task.Delay(delay).ConfigureAwait(false);
}
Есть способ, которым я называю этот метод
if (!m_MyManagerMainForm.DoPerformCommandOnClient(cmd1, true, sourcePath, "bmp", "ColorImageDone", "_M"))
{
RestoreGUIFromCalibration();
return;
}
, но он не работает, он говорит, что я могу неявно конвертировать Task<bool> to bool