hengi
hengi
CC#
Created by ggppjj on 4/6/2023 in #help
❔ Trigger MediaElement.play() the MVVM way?
Someone asked that exact question on SO: https://stackoverflow.com/questions/10631748/mvvm-pattern-violation-mediaelement-play TL;DR you could raise an event PlayRequested in the VM and subscribe to the event in the View. There, you just call MediaElement.Play(). The other way would be a bit more elaborate but actually "feels" how MVVM should be used. Basically the second answer of the StackOverflow question. You wrap the MediaElement in an Interface, create a new UserControl with them and then use the UserControl.
4 replies
CC#
Created by hengi on 4/1/2023 in #help
Unsigned data types for reference implementation
thanks for your response! I guess you're right, it would be a pain to project the properties of unsigned types on a signed one..
3 replies
CC#
Created by Trace on 3/13/2023 in #help
❔ Batching
It would basically just come down to group by ID into a List and then distributing the Lists into N buckets of size 100 where List.Count is the weight or size of the item
24 replies
CC#
Created by Trace on 3/13/2023 in #help
❔ Batching
You might wanna look into the bin packing problem If you treat identical IDs as being a single object with weight=count, you should be able to find a good solution
24 replies
CC#
Created by edgarka_ on 3/9/2023 in #help
✅ BackgroundWorker in WPF MVVM
BackgroundWorkers take one ThreadPool Thread, Thread.Sleep sleeps the current Thread ^^
17 replies
CC#
Created by edgarka_ on 3/9/2023 in #help
✅ BackgroundWorker in WPF MVVM
BackgroundWorkers are just a bit old and they feel a bit like Java Threads? Where you subclass a Thread but with Events instead. Just treat the Events like enclosed actions, do not manipulate the BackgroundWorker outside of them except for Cancellation
17 replies
CC#
Created by edgarka_ on 3/9/2023 in #help
✅ BackgroundWorker in WPF MVVM
ExecuteCommand.cs
public class ExecuteCommand : CommandBase
{
private readonly MainPageViewModel _mainPageViewModel;
BackgroundWorker _bw;

public ExecuteCommand(MainPageViewModel mainPageViewModel, BackgroundWorker bw)
{
_mainPageViewModel = mainPageViewModel;
_bw = bw;
}

public event EventHandler CanExexuteChanged;

public bool CanExecute(object parameter)
{
return true;
}

public override void Execute(object parameter)
{
_bw.RunWorkerAsync();
}
}
public class ExecuteCommand : CommandBase
{
private readonly MainPageViewModel _mainPageViewModel;
BackgroundWorker _bw;

public ExecuteCommand(MainPageViewModel mainPageViewModel, BackgroundWorker bw)
{
_mainPageViewModel = mainPageViewModel;
_bw = bw;
}

public event EventHandler CanExexuteChanged;

public bool CanExecute(object parameter)
{
return true;
}

public override void Execute(object parameter)
{
_bw.RunWorkerAsync();
}
}
17 replies
CC#
Created by edgarka_ on 3/9/2023 in #help
✅ BackgroundWorker in WPF MVVM
PerformTask.cs
public static class PerformTask
{
public static void InitializeTask(MainPageViewModel _mainPageViewModel, BackgroundWorker _bw)
{
for (int i = 0; i < 10; i++)
{
Thread.Sleep(100);
//
}
_bw.ReportProgress(10);


for (int i = 0; i < 3; i++)
{
Thread.Sleep(1000);
//
}
_bw.ReportProgress(20);

for (int i = 0; i < 3; i++)
{
Thread.Sleep(1000);
}
}
}
public static class PerformTask
{
public static void InitializeTask(MainPageViewModel _mainPageViewModel, BackgroundWorker _bw)
{
for (int i = 0; i < 10; i++)
{
Thread.Sleep(100);
//
}
_bw.ReportProgress(10);


for (int i = 0; i < 3; i++)
{
Thread.Sleep(1000);
//
}
_bw.ReportProgress(20);

for (int i = 0; i < 3; i++)
{
Thread.Sleep(1000);
}
}
}
17 replies
CC#
Created by edgarka_ on 3/9/2023 in #help
✅ BackgroundWorker in WPF MVVM
oh yeah, you should not manipulate the background worker outside the Events if you want to use the PerformTask class, you have to do it like this: MainPageViewModel.cs
public void InitializeBackgroundWorker()
{
bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_RunWorker_DoWork);
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
}

private void bw_RunWorker_DoWork(object? sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
PerformTask.InitializeTask(this, bw);
}

private void bw_RunWorkerCompleted(object? sender, RunWorkerCompletedEventArgs e)
{
CurrentProgress = 100;
ProgressText = "Completed";
MessageBox.Show("Completed");
}

private void bw_ProgressChanged(object? sender, ProgressChangedEventArgs e)
{
ProgressText = "In progress";
CurrentProgress = e.ProgressPercentage;
}
public void InitializeBackgroundWorker()
{
bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_RunWorker_DoWork);
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
}

private void bw_RunWorker_DoWork(object? sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
PerformTask.InitializeTask(this, bw);
}

private void bw_RunWorkerCompleted(object? sender, RunWorkerCompletedEventArgs e)
{
CurrentProgress = 100;
ProgressText = "Completed";
MessageBox.Show("Completed");
}

private void bw_ProgressChanged(object? sender, ProgressChangedEventArgs e)
{
ProgressText = "In progress";
CurrentProgress = e.ProgressPercentage;
}
17 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
22 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
ohh
22 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
how does your code look now?
22 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
yes
22 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
this will basically just increase count after one whole process of doing the Collatz conjecture on a number. to count the steps, you want to count inside the while loop
22 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
yes, because you don't actually need the for loop
22 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
it runs infinitely because you did a semicolon after the for parantheses
22 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
OH
22 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
the inner loop will run until the number reaches 1, then the outer loop should immediately break after that? because num == 1, count will always be 1 i think you want a counter variable inside the while loop at the start of the loop, you just increase it by 1, no need for two loops
22 replies
CC#
Created by crab2_ on 3/9/2023 in #help
can't make a cycle properly
it's the Collatz conjecture peepoHappy you want to count how many steps it took to get to 1, right?
22 replies
CC#
Created by edgarka_ on 3/9/2023 in #help
✅ BackgroundWorker in WPF MVVM
yes ofc, most of the stuff you do in WPF usually does not require even that, Task.Run is only really needed when you do CPU heavy stuff. Everything else is awaitable at least somewhere so you can just use async Events or expand your Command class to support Async Commands and stuff
17 replies