C
C#9mo ago
Mekasu0124

✅ make function sleep without freezing ui avalonia

public void LoginScreen()
{
var vm = new LoginViewModel();

Observable.Merge(
vm.Ok,
vm.Cancel
.Select(_ => (UserModel?)null))
.Take(1)
.Subscribe(model =>
{
if (model != null)
{
LoggedInUser.Username = model.Username;
}
else
{
Environment.Exit(0);
}
});

Content = vm;
}
public void LoginScreen()
{
var vm = new LoginViewModel();

Observable.Merge(
vm.Ok,
vm.Cancel
.Select(_ => (UserModel?)null))
.Take(1)
.Subscribe(model =>
{
if (model != null)
{
LoggedInUser.Username = model.Username;
}
else
{
Environment.Exit(0);
}
});

Content = vm;
}
this is my function for the MainWindowViewModel. and this is my ViewModel that matches the LoginViewModel vm https://pastebin.com/uDpQL2cR. 1. My first issue is that in my ReturnUser model, I can't figure out what to return in the if/else if clauses. 2. My second issue is that when i click the Exit button, it's almost immediate. It takes a second or two and then the window closes, but then if I just leave username and password blank and click login, it takes nearly 6 seconds to figure whatever it needs to out and then it displays that the login was successful ... 3. which leads into #3 before #1 became an issue, I did have it to where the else clause was outside of the if statment and not in an else clause so that the code would execute if the check systems didn't fail, however, I think there was a problem with my check system. This is a link to the copy of the code that comes before #1 being an issue https://pastebin.com/jsb8nsqA I'm not really sure what to do. I would like to keep the code I had before #1 became an issue as I was working on cleaning it up and making it better if we could go that route with this code https://pastebin.com/jsb8nsqA MainWindowViewModel.cs (for reference): https://pastebin.com/R8e77kDa
31 Replies
Mekasu0124
Mekasu0124OP9mo ago
ok so I've identified where my problem lies
MODiX
MODiX9mo ago
Use the /close command to mark a forum thread as answered
Mekasu0124
Mekasu0124OP9mo ago
public bool HandleInvalidInput(string errorText, string trigger)
{
IsError = true;
ErrorText = errorText;

Username = "";
Password = "";

Thread.Sleep(6000);

IsError = false;
ErrorText = "";

return false;
}
public bool HandleInvalidInput(string errorText, string trigger)
{
IsError = true;
ErrorText = errorText;

Username = "";
Password = "";

Thread.Sleep(6000);

IsError = false;
ErrorText = "";

return false;
}
my problem is the Thread.Sleep(6000); What's supposed to happen here is it shows the error text and clear the username and password fields, then sleep 6 seconds, then hide the error and reset its text, but instead it's sleeping first, never showing anything, and then clearing the fields so any ideas on what's wrong with my logic? Full Code: https://pastebin.com/BMZB63yu
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
yea... I can't remember what the other thing is I'm supposed to use
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
gotcha it's the dispatcher thing
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
never watched sw. sorry lol
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
😂
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
I"m a WoW player though
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
does that help?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
oh ok
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
right
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
so the DispatcherTimer class
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
ok lol I understood that part already 😂 I'm asking am I supposed to use the DispatcherTimer class?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
oh ok. tyvm
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
I don't use wpf. I use avalonia
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
yea I got the reference lol
public bool RunChecks()
{
bool usernameValid = Helpers.ValidateUsername(Username);
bool passwordValid = Helpers.ValidatePassword(Password);
bool userFound = JsonEngine.GetUser(NewUser);

string errTxt = """
Invalid Username/Password: Username/Password
Muast Be At Least 8 Characters In Length,
And Must Have At Least 1 Upper Case Letter,
1 Lower Case Letter, And 1 Number. In Addition,
Passwords Must Have At Least 1 Special Character
Such As: !, @, #, $, %, ^, &, or _
""";

// checking the inputs themselves
if (!usernameValid || !passwordValid)
{
HandleInvalidInput(errTxt);
}

// checking if the user exists.
if (!userFound)
{
HandleInvalidInput("Invalid Username/Password. Try again");
}

Success = true;
SuccessText = "Login Successful";
return true;
}
public bool RunChecks()
{
bool usernameValid = Helpers.ValidateUsername(Username);
bool passwordValid = Helpers.ValidatePassword(Password);
bool userFound = JsonEngine.GetUser(NewUser);

string errTxt = """
Invalid Username/Password: Username/Password
Muast Be At Least 8 Characters In Length,
And Must Have At Least 1 Upper Case Letter,
1 Lower Case Letter, And 1 Number. In Addition,
Passwords Must Have At Least 1 Special Character
Such As: !, @, #, $, %, ^, &, or _
""";

// checking the inputs themselves
if (!usernameValid || !passwordValid)
{
HandleInvalidInput(errTxt);
}

// checking if the user exists.
if (!userFound)
{
HandleInvalidInput("Invalid Username/Password. Try again");
}

Success = true;
SuccessText = "Login Successful";
return true;
}
https://reference.avaloniaui.net/api/Avalonia.Threading/DispatcherTimer/ see what I need to happen is when the if statement is triggered for the username or password not being right, it displays the error message and then after a few seconds hides the error message and resets the user's input. I just can't figure out where or how to do that.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Mekasu0124
Mekasu0124OP9mo ago
sure... i got it figured out
Want results from more Discord servers?
Add your server