Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

Why do I have a bug displaying a custom popup in a Thread?

I'm using a custom popup that appears in the bottom right-hand corner of the screen and disappears a few seconds later.

But when I try to display this personalized popup in a Thread, it bugs, i.e. I have to click on the desktop for the popup to appear, but without the alert icon and without the alert message. My mouse is on hold and the program no longer responds.

I have no problem displaying a message with "MessageBox".

What's strange is that if I use my "personal popup" and "messageBox" at the same time, my popup is displayed correctly but it doesn't disappear after a few seconds and the program no longer responds.

Do you know what the problem is?
I've searched but haven't found any questions that match my problem.

What I have tried:

Here's the code:
C#
<pre>     
this.ThreadAutoCheck = new Thread((ThreadStart)(async () =>
     {
      TelegramBotService telegramBotService = new 
      TelegramBotService(this.txtTelegramKey.Texts.Trim());
      string chatId = this.txtChannelID.Texts.Trim();
      while (this.ThreadAutoCheck != null)
      {
        try
        {
          List<Position> otherPositions = new List<Position>();
          foreach (string str in lstUID)
          {
            string uid = str;
            List<Position> positions = await apkApi.GetPositions(uid, type);
            if (positions == null || positions.Count == 0)
            {
              this.Alert("No data to display.", FAlert.enmType.Warning);
            }
Posted
Updated 9-Jun-23 1:34am
v4

1 solution

You should always post the error message in your question so we can see what the exact issue is.

At a guess, you are seeing a cross thread exception. This happens when you try to do UI actions from a thread other than the UI Thread. You can check this by setting a breakpoint before doing any UI work, then look at the Thread window to see what thread you are on.

If you are not on the UI thread, then you need to marshal to the UI thread before doing the UI work.

I work with both WPF and WinForms, both do the marshalling differently. So I have a common helper implementation that I use. Here is the helper class:
C#
public static class DispatcherHelper
{
    public static void Execute(Action action)
    {
        // no cross-thread concerns
        if (Application.OpenForms.Count == 0)
        {
            action.Invoke();
			return;
        }

		try
		{
			if (Application.OpenForms[0]!.InvokeRequired)
                // Marshall to Main Thread
				Application.OpenForms[0]?.Invoke(action);
            else
                // We are already on the Main Thread
                action.Invoke();
        }
		catch (Exception)
		{
			// ignore as might be thrown on shutting down
		}
    }
}

Then to use:
C#
DispatcherHelper.Execute(() => 
{
    // do UI work here...
});

So, in your case, I think you would use it like this:
C#
if (positions == null || positions.Count == 0)
{
    DispatcherHelper.Execute(() =>
        this.Alert("No data to display.", FAlert.enmType.Warning));
}
 
Share this answer
 
Comments
Anaîs laffont 9-Jun-23 4:58am    
Thanks a lot, I tried the Class and I get an error on the "Exclamation mark" in if (Application.OpenForms[0]!.InvokeRequired)

Error CS8370 The 'Nullable reference types' feature is not available in C# 7.3. Use language version 8.0 or later.

Impossible to change language version to 8.0 because the box is grayed out
Richard Deeming 9-Jun-23 5:35am    
So remove the exclamation mark, which is the only NRT feature in that code.
if (Application.OpenForms[0].InvokeRequired)
    // Marshall to Main Thread
	Application.OpenForms[0]?.Invoke(action);
Graeme_Grant 9-Jun-23 7:28am    
As Richard mentioned, I am using nullable types used in DotNet 5.0+. For .Net Framework, simply remove the '!' and the code will compile.

PS: If you move your project to DotNet from .Net Framework, without any code changes, your code will run a lot faster!
Anaîs laffont 9-Jun-23 10:06am    
@Graeme_Grant, Thank you very much it works perfectly. For the DotNet I will try.. I hope to find a tutorial on google..
Graeme_Grant 9-Jun-23 10:21am    
Glad that it works for you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900