TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Santhosh
NA
300
140.2k
Threads Dispatchers Static Code
Sep 27 2019 3:43 AM
public
partial
class
TestWindow : Window
{
public
ICommand ExecuteAction;
public
MainWindow()
{
InitializeComponent();
}
Thread th;
Thread thMain;
private
void
BtnTest_Click(
object
sender, RoutedEventArgs e)
{
th =
new
Thread(UpdateValue);
th.Start();
}
private
void
BtnSearch_Click(
object
sender, RoutedEventArgs e)
{
string
sourceDir =
string
.Empty;
string
searchText =
string
.Empty;
wpMain.Children.Clear();
sourceDir = txtInputDir.Text.Trim();
searchText = txtInput.Text.Trim();
thMain =
new
Thread(() => PerformAction(sourceDir, searchText));
thMain.SetApartmentState(ApartmentState.MTA);
thMain.Start();
}
private
void
PerformAction(
string
sourceDir,
string
searchText)
{
try
{
//sourceDir = @"C:\Users\santhosh.polu\source\repos\TFS_JIRA_SUPPORT_Solution";
if
(Directory.Exists(sourceDir))
{
List<
string
> lstDirsFiles = Directory.GetDirectories(sourceDir,
"*"
, SearchOption.AllDirectories).ToList();
List<
string
> lstDirsFiles1 = Directory.GetFiles(sourceDir,
"*"
, SearchOption.AllDirectories).ToList();
lstDirsFiles.AddRange(lstDirsFiles1);
int
totalCount = lstDirsFiles.Count;
if
(totalCount < 1)
return
;
if
(lstDirsFiles.Count > 0)
{
int
i = 0;
while
(i < lstDirsFiles.Count)
{
string
currentItem = lstDirsFiles[i];
Dispatcher.Invoke(
new
Action(() =>
{
lblStatus.Content = currentItem;
}));
if
(Directory.Exists(currentItem))
{
DirectoryInfo dir =
new
DirectoryInfo(currentItem);
String dirName = dir.Name.ToString();
if
(dirName.ToLower().Contains(searchText.ToLower()))
{
Dispatcher.Invoke(
new
Action(() =>
{
Button btn =
new
Button();
btn.Content = dirName;
btn.Background = Brushes.AliceBlue;
btn.ToolTip =
"Directory : "
+ dir.FullName;
btn.Tag = dir.FullName;
btn.Click += Btn_Click;
wpMain.Children.Add(btn);
}));
}
}
else
if
(File.Exists(currentItem))
{
FileInfo fi =
new
FileInfo(currentItem);
String fileName = fi.Name.ToString();
if
(fileName.ToLower().Contains(searchText.ToLower()))
{
Dispatcher.Invoke(
new
Action(() =>
{
Button btn =
new
Button();
btn.Content = fileName;
btn.ToolTip =
"File : "
+ System.IO.Path.GetDirectoryName(fileName);
btn.Background = Brushes.IndianRed;
btn.Tag = System.IO.Path.GetDirectoryName(fileName);
btn.Click += Btn_Click1;
wpMain.Children.Add(btn);
}));
}
}
i++;
}
}
else
{
MessageBox.Show(
"No Directories exist."
);
}
}
else
{
MessageBox.Show(
"Directory Does not exist."
);
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message.ToString() + System.Environment.NewLine + ex.StackTrace.ToString());
}
}
private
void
Btn_Click1(
object
sender, RoutedEventArgs e)
{
Button btn = sender
as
Button;
Process.Start(
"explorer.exe"
, btn.Tag.ToString());
}
private
void
Btn_Click(
object
sender, RoutedEventArgs e)
{
Button btn = sender
as
Button;
Process.Start(
"explorer.exe"
, btn.Tag.ToString());
}
public
void
UpdateValue()
{
while
(
true
)
{
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Background,
new
Action(() =>
{
txtCurrent.Text = DateTime.Now.ToString(
"DD MM yyyy hh mm ss"
);
}));
}
}
private
void
BtnStop_Click(
object
sender, RoutedEventArgs e)
{
if
(
thMain
!=
null
)
{
thMain.Abort();
}
}
}
Hi,
I am trying to write code for a small application, which reads all the directories and performs some operations on the .xml file types on each directory.
So
When i Read Directory without using any Threads UI is freezing untill the execution of the method is completed.
Lets say, without Thread and Dispatcher, it has taken 1.5minutes of time.
after using thread and dispatchers, UI is not freezing and it is taking lot of time to complete the action.
Code snippet is given.
Kindly tell me, how to reduce the time for Execution (Without UI Freeze).
Thanks.
Reply
Answers (
2
)
Running WPF application On Windows Start
How to open windows forms in WPF application