private async void OnSearchButtonClick(object sender, RoutedEventArgs e)
{
string searchText = SearchTextBox.Text;
if (!string.IsNullOrEmpty(searchText))
{
FolderTreeView.IsEnabled = false; // Disable the UI while searching
// Start the timer and reset the elapsed time
_elapsedTime = TimeSpan.Zero;
_searchTimer.Start();
var result = await _folderSearchService.SearchTreeViewItem(FolderTreeView, searchText);
// Stop the timer once the search is finished
_searchTimer.Stop();
TimeCountLabel.Text = $"Search Time: {_elapsedTime:mm\\:ss}"; // Display final time
if (result != null)
{
result.IsSelected = true;
result.BringIntoView();
}
else
{
MessageBox.Show("Folder not found.", "Search Result", MessageBoxButton.OK, MessageBoxImage.Information);
}
FolderTreeView.IsEnabled = true; // Re-enable the UI after searching
}
}
Я запускаю таймер на этой строке
_searchTimer.Start();
перед началом поиска по этой строке
var result = await _folderSearchService.SearchTreeViewItem(FolderTreeView, searchText);
но я вижу, что поиск начинается до запуска таймера. таймер запускается примерно через 2-3 секунды после начала поиска.
Я пробовал использовать Dispatcher.Invoke(() =>, но это не сильно изменилось.
это версия с Dispatcher.Invoke
private async void OnSearchButtonClick(object sender, RoutedEventArgs e)
{
string searchText = SearchTextBox.Text;
if (!string.IsNullOrEmpty(searchText))
{
FolderTreeView.IsEnabled = false; // Disable the UI while searching
// Start the timer and reset the elapsed time
_elapsedTime = TimeSpan.Zero;
_searchTimer.Start();
// Force UI to update before continuing with the search
await Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
// This forces the UI to update and start the timer before the search begins.
TimeCountLabel.Text = "Timer: 00:00";
});
});
// Begin searching
var result = await _folderSearchService.SearchTreeViewItem(FolderTreeView, searchText);
// Stop the timer once the search is finished
_searchTimer.Stop();
TimeCountLabel.Text = $"Timer: {_elapsedTime:mm\\:ss}"; // Display final time
if (result != null)
{
result.IsSelected = true;
result.BringIntoView();
}
else
{
MessageBox.Show("Folder not found.", "Search Result", MessageBoxButton.OK, MessageBoxImage.Information);
}
FolderTreeView.IsEnabled = true; // Re-enable the UI after searching
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... me-when-th
В С# wpf, как запустить DispatcherTimer точно в то же время, когда начинается поиск? ⇐ C#
Место общения программистов C#
1728001593
Anonymous
private async void OnSearchButtonClick(object sender, RoutedEventArgs e)
{
string searchText = SearchTextBox.Text;
if (!string.IsNullOrEmpty(searchText))
{
FolderTreeView.IsEnabled = false; // Disable the UI while searching
// Start the timer and reset the elapsed time
_elapsedTime = TimeSpan.Zero;
_searchTimer.Start();
var result = await _folderSearchService.SearchTreeViewItem(FolderTreeView, searchText);
// Stop the timer once the search is finished
_searchTimer.Stop();
TimeCountLabel.Text = $"Search Time: {_elapsedTime:mm\\:ss}"; // Display final time
if (result != null)
{
result.IsSelected = true;
result.BringIntoView();
}
else
{
MessageBox.Show("Folder not found.", "Search Result", MessageBoxButton.OK, MessageBoxImage.Information);
}
FolderTreeView.IsEnabled = true; // Re-enable the UI after searching
}
}
Я запускаю таймер на этой строке
_searchTimer.Start();
перед началом поиска по этой строке
var result = await _folderSearchService.SearchTreeViewItem(FolderTreeView, searchText);
но я вижу, что поиск начинается до запуска таймера. таймер запускается примерно через 2-3 секунды после начала поиска.
Я пробовал использовать Dispatcher.Invoke(() =>, но это не сильно изменилось.
это версия с Dispatcher.Invoke
private async void OnSearchButtonClick(object sender, RoutedEventArgs e)
{
string searchText = SearchTextBox.Text;
if (!string.IsNullOrEmpty(searchText))
{
FolderTreeView.IsEnabled = false; // Disable the UI while searching
// Start the timer and reset the elapsed time
_elapsedTime = TimeSpan.Zero;
_searchTimer.Start();
// Force UI to update before continuing with the search
await Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
// This forces the UI to update and start the timer before the search begins.
TimeCountLabel.Text = "Timer: 00:00";
});
});
// Begin searching
var result = await _folderSearchService.SearchTreeViewItem(FolderTreeView, searchText);
// Stop the timer once the search is finished
_searchTimer.Stop();
TimeCountLabel.Text = $"Timer: {_elapsedTime:mm\\:ss}"; // Display final time
if (result != null)
{
result.IsSelected = true;
result.BringIntoView();
}
else
{
MessageBox.Show("Folder not found.", "Search Result", MessageBoxButton.OK, MessageBoxImage.Information);
}
FolderTreeView.IsEnabled = true; // Re-enable the UI after searching
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79052547/in-c-sharp-wpf-how-to-start-the-dispatchertimer-at-the-same-exactly-time-when-th[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия