C
C#2y ago
WTDawson9

❔ Ookii.Dialogs.Wpf ProgressDialog BackgroundWorker Dispatcher.Invoke not working.

Using Ookii.Dialogs.Wpf's ProgressDialog Full code: (Will post in bits) it's not updating the UI and idk what im doing wrong.
18 Replies
WTDawson9
WTDawson9OP2y ago
public MainWindow()
{
InitializeComponent();
}
public List<NetworkItem> Items { get; set; }
string IP;

public class NetworkItem
{
public string IP { get; set; }
public string Name { get; set; }
public bool Online { get; set; }
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Items = new List<NetworkItem>();
Data.ItemsSource = Items;
Data.Columns[0].Header = "IP Address";
Data.Columns[1].Header = "Hostname";
Data.Columns[2].Header = "Online";
}
public MainWindow()
{
InitializeComponent();
}
public List<NetworkItem> Items { get; set; }
string IP;

public class NetworkItem
{
public string IP { get; set; }
public string Name { get; set; }
public bool Online { get; set; }
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Items = new List<NetworkItem>();
Data.ItemsSource = Items;
Data.Columns[0].Header = "IP Address";
Data.Columns[1].Header = "Hostname";
Data.Columns[2].Header = "Online";
}
ProgressDialog progressDialog = new ProgressDialog();

private void MBTSNS_Click(object sender, RoutedEventArgs e)
{
string IP = Interaction.InputBox("Please provide an IP address (public or private) of the network to scan.", "NetworkScanner", "0.0.0.0", 10, 10);
this.IP = IP;
progressDialog.DoWork += ProgressDialog_DoWork;
progressDialog.WindowTitle = "NetworkScanner";
progressDialog.Text = $"Scanning network of {IP}";
progressDialog.ShowDialog();
}
ProgressDialog progressDialog = new ProgressDialog();

private void MBTSNS_Click(object sender, RoutedEventArgs e)
{
string IP = Interaction.InputBox("Please provide an IP address (public or private) of the network to scan.", "NetworkScanner", "0.0.0.0", 10, 10);
this.IP = IP;
progressDialog.DoWork += ProgressDialog_DoWork;
progressDialog.WindowTitle = "NetworkScanner";
progressDialog.Text = $"Scanning network of {IP}";
progressDialog.ShowDialog();
}
private void ProgressDialog_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
string[] IPS = IP.Split('.');
string new_IP = $"{IPS[0]}.{IPS[1]}.{IPS[2]}";

int found = 0;

for (int i = 0; i < 255; i++)
{
if (progressDialog.CancellationPending)
return;

//Thread.Sleep(50);
progressDialog.ReportProgress((int)(i / 255.0 * 100), null, string.Format(System.Globalization.CultureInfo.CurrentCulture, "Scanning network: {0}%", Math.Round(i / 255.0 * 100)) + $" ({new_IP + "." + i})" +
$" Found {found} alive IPs");

Ping ping = new Ping();
PingReply reply = ping.Send(new_IP + "." + i, 1000);
if(reply.Status == IPStatus.Success)
{
found++;
NetworkItem networkItem = new NetworkItem();
networkItem.IP = new_IP + "." + i;
IPAddress[] localIPs = Dns.GetHostAddresses(new_IP + "." + i);
IPHostEntry entry = Dns.GetHostByAddress(localIPs[0]);
networkItem.Name = entry.HostName;

Dispatcher.Invoke(() =>
{
Items.Add(networkItem);
Data.ItemsSource = Items;
});
}
}
}
private void ProgressDialog_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
string[] IPS = IP.Split('.');
string new_IP = $"{IPS[0]}.{IPS[1]}.{IPS[2]}";

int found = 0;

for (int i = 0; i < 255; i++)
{
if (progressDialog.CancellationPending)
return;

//Thread.Sleep(50);
progressDialog.ReportProgress((int)(i / 255.0 * 100), null, string.Format(System.Globalization.CultureInfo.CurrentCulture, "Scanning network: {0}%", Math.Round(i / 255.0 * 100)) + $" ({new_IP + "." + i})" +
$" Found {found} alive IPs");

Ping ping = new Ping();
PingReply reply = ping.Send(new_IP + "." + i, 1000);
if(reply.Status == IPStatus.Success)
{
found++;
NetworkItem networkItem = new NetworkItem();
networkItem.IP = new_IP + "." + i;
IPAddress[] localIPs = Dns.GetHostAddresses(new_IP + "." + i);
IPHostEntry entry = Dns.GetHostByAddress(localIPs[0]);
networkItem.Name = entry.HostName;

Dispatcher.Invoke(() =>
{
Items.Add(networkItem);
Data.ItemsSource = Items;
});
}
}
}
JakenVeina
JakenVeina2y ago
Data.ItemsSource = Items
Data.ItemsSource = Items
is definitely not what you want, for starters more interestingly, how sure are you that that line is actually getting HIT?
WTDawson9
WTDawson9OP2y ago
I don't think that it is, that is the problem
JakenVeina
JakenVeina2y ago
well, start working that
WTDawson9
WTDawson9OP2y ago
What? Everything else works, it's just I need to get data back to the main thread
JakenVeina
JakenVeina2y ago
okay, so which is it? does everything work, or is your update code not getting called?
WTDawson9
WTDawson9OP2y ago
As I've said it's not updating the UI
JakenVeina
JakenVeina2y ago
to which I asked "is that code actually getting hit" to which you answered "no" to which I said "well, start working that" to which you said "everything else works" which is it? is your code getting called, or does it all work?
WTDawson9
WTDawson9OP2y ago
"everything else works" meaning that everything but those 4 lines are working So no those 4 lines are not working Which is why I opened this help thread
JakenVeina
JakenVeina2y ago
so, are those lines being called?
WTDawson9
WTDawson9OP2y ago
No I don't think they are, the reason of this thread I created
JakenVeina
JakenVeina2y ago
then work that problem they're inside an if statement is that evaluating false? that's inside a loop is the loop empty? there's an if statement that terminates the loop early is that getting triggered? also
I don't think they are
don't guess, find out for sure
WTDawson9
WTDawson9OP2y ago
Well it's clearly not because it's not showing up. That's what I'm trying to do but you're not helping
JakenVeina
JakenVeina2y ago
that's an assumption prove it
WTDawson9
WTDawson9OP2y ago
What do you mean prove it? It's not showing up
JakenVeina
JakenVeina2y ago
set a breakpoint
WTDawson9
WTDawson9OP2y ago
Can't right now I will when I can (It is doing stuff on the background thread since thats updating with the progress and it's actually finding things.)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server