2

I feel a bit out of my depths with this.

I have a seperate timer thread which ticks once a second. If a value hits <= 0, then this bit of code is run:

...
EnemyHP = EnemyMaxHP;
//Toast t = new Toast("You died!", "Oh no, you died! the " + Enemies[CurrentEnemy].Name + " killed you, and you lost " + lost + " GP!", NotificationType.Warning);
Log.Info("Player Died!!! Enemy responsible: " + Enemies[CurrentEnemy].Name + ". GP Lost: " + lost + ".");

If I run that as is, The Log.Info line is run, and the log line appears in the file. If, however, I uncomment the middle line, it starts to run that, but seems to stop soon and then NEVER runs the logfile line. Here is the CTOR in Toast upon which it is calling:

public Toast(string Title, string Description, NotificationType Type = NotificationType.Information)
    {
        toast = new ToastPopUp(Title, Description, "", Type);
        toast.Background = new SolidColorBrush(Colors.AliceBlue);
        toast.BorderBrush = new SolidColorBrush(Colors.Black);
        toast.FontColor = new SolidColorBrush(Colors.Black);
        //toast.HyperlinkClicked += this.ToastHyperlinkClicked;
        //toast.ClosedByUser += this.ToastClosedByUser;
        toast.Show();
    }

ToastPopUp refers to https://toastspopuphelpballoon.codeplex.com/ - I am using this. The toast popup does not show up when run through this code, but runs fine when using identical code elsewhere in the code.

Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
pingu2k4
  • 956
  • 2
  • 15
  • 31
  • So you are accessing UI thread from Toast popup created in another background thread? – rt2800 Jul 15 '15 at 10:47
  • I access the toastpopup elsewhere in the same thread as the main viewmodel... But from the viewmodel I launch a seperate thread which ticks each second. This modifies a value, and if that value reaches 0, that then triggers the above snippet of code to run. – pingu2k4 Jul 15 '15 at 10:49
  • I'm unfamiliar with a toast but it sounds like a UI thing. Are you sure you are allowed to use it from a background thread without invoking the main? – Nick Otten Jul 15 '15 at 10:53
  • Check [this](http://stackoverflow.com/a/1644254/525251) ans. – rt2800 Jul 15 '15 at 10:54
  • Since we're talking about Toast notifications, I assume you're not using WPF, but WinRT? I suggest you modify your tags to reflect that, people searching for this answer could be misled by the current WPF tag. – almulo Jul 15 '15 at 13:31
  • No, I am using WPF. :) – pingu2k4 Jul 16 '15 at 14:10

2 Answers2

2

I suspect the Toast can be created only from the UI thread, so the constructor throws an exception. All UI operations in WPF can only be done from the UI thread, as a rule.

Try to invoke the Toast constructor on the UI thread. This assumes that your application is an WPF app (that is not 100% clear from your question).

You should also learn how to handle exceptions in non-UI threads in WPF, so you can actually see why is your code breaking.

Community
  • 1
  • 1
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
  • Thankyou - the invoke link worked. My googling had showed up Invoke was something I should do, but I couldn't find out how to do it properly. The link you gave explained it perfectly, and it works. Thanks! :) – pingu2k4 Jul 15 '15 at 11:07
1

You can use Dispatcher.

 await Dispatcher.InvokeAsync<Task>(
  new Action(() => {

// Put your logic here

  }));
Kashif
  • 2,926
  • 4
  • 20
  • 20