Close with timeout is not working in Console to close splashscreen - c#

I have made a class, I make an instance of. In said instance I have these lines of code to show and close the splashscreen.
// Open (show)
public void ShowSplashScreen(bool autoClose = false)
{
splashscreen.Show(autoClose, true);
}
// Close (don't show)
public void CloseSplashScreen()
{
splashscreen.Close(TimeSpan.FromSeconds(0.3));
}
It shows up fine, but never closes, just stays there.
This is the documentation of splashscreen Close: https://learn.microsoft.com/en-us/dotnet/api/system.windows.splashscreen.close?view=netframework-4.8
[System.Security.SecurityCritical]
public void Close (TimeSpan fadeoutDuration);
Note: I am using the show method with the parameters AutoClose set to false, and TopMost set to true, this makes it not auto close as I want to close it programmatically and not subscribe to existing events.
I am running the lines of code from a Console (.NET framework) application for testing purposes before implementing it into my UI fully.
What I have tried:
Debugging and even trying to call show again before calling close.
It is definitely something going wrong with the class, as calling the class and directly manipulating the property works:
ClassSplashScreen rss = new ClassSplashScreen();
rss.splashscreen.Show(false);
rss.splashscreen.Close(TimeSpan.FromSeconds(1));
My best guess is something is hanging the UI and freezing it? But I am unsure what to do about it.
Code to run to test this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace NamespaceName
{
public class StackOverFlowCode
{
static void Main(string[] args)
{
ClassSplashScreen screen = new ClassSplashScreen();
screen.ShowSplashScreen();
screen.CloseSplashScreen();
}
}
public class ClassSplashScreen
{
public SplashScreen splashscreen { get; set; }
public ClassSplashScreen()
{
splashscreen = new SplashScreen("Resource Image Link");
}
public void ChangeSplashResource(SplashScreen resource)
{
splashscreen = resource;
}
public void ShowSplashScreen(bool autoClose = false)
{
splashscreen.Show(autoClose, true);
}
public void CloseSplashScreen()
{
splashscreen.Close(TimeSpan.FromSeconds(1));
}
}
}

The SplashScreen relies on a dispatcher but there is no one in a console application by default. If you create a System.Windows.Application, it should work as expected:
public class StackOverFlowCode
{
[STAThread]
static void Main(string[] args)
{
Application app = new Application();
app.Startup += (s, e) =>
{
ClassSplashScreen screen = new ClassSplashScreen();
screen.ShowSplashScreen();
screen.CloseSplashScreen();
};
app.Run();
}
}
public class ClassSplashScreen
{
private readonly SplashScreen splashscreen;
public ClassSplashScreen() => splashscreen = new SplashScreen("Resource Image Link");
public void ShowSplashScreen() => splashscreen.Show(false);
public void CloseSplashScreen() => splashscreen.Close(TimeSpan.FromSeconds(1));
}

Related

Can't close this form

I run a new form
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
Then I call into FormMain():
Application.Run(applicationContext);
How can I close FormMain by code?
Here is FormMain:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyTabs;
namespace CefSharp
{
public partial class FormMain : Form
{
public static AppContainer Container = new AppContainer();
public FormMain()
{
InitializeComponent();
Container.Tabs.Add(new EasyTabs.TitleBarTab(Container)
{
Content = new frmTab
{
Text = "New Tab"
}
});
Container.SelectedTabIndex = 0;
TitleBarTabsApplicationContext applicationContext = new TitleBarTabsApplicationContext();
applicationContext.Start(Container);
Application.Run(applicationContext);
this.Hide();
if(Container.ExitOnLastTabClose)
{
this.Close();
}
}
}
}
Ok guys, it's not that simple as i wrote without checking. I figured out solve with threads use. First we need delegate on form:
public delegate void closer();
partial class FormMain {
public closer Closer;
(...)
}
Inside class constructor or in InitializeComponents add Close method to it:
this.Closer += Close;
Create public static FormMain object:
static class Program {
public static FormMain form1;
(...)
}
Then you just run threads with running window and simply (in this case after 5 seconds) close the window:
Program.form1 = new FormMain();
Thread fo = new Thread(() => { Application.Run(Program.form1); });
Thread th = new Thread(() => { Thread.Sleep(5000); Program.form1.Invoke(form1.Closer); });
fo.Start();
th.Start();
You can use two different approaches:
As previously mentionded you can use formMain.Close()
And Application.Exit() to totally close the application.
If you want something more special use Bartek solution. But still, the information you gave us are not enough

Is it possible to show DisplayAlert on startup (Xamarin.Forms)

In my mobile application (xamarin forms), I'm getting data from internet so it needs internet connection. Since I have a dictionary which I initialize in App.xaml.cs and I use data from internet, I need to check for internet connection. I have seen this question where OP asks for something similar, but the answer doesn't work for me since I need to check for internet connection whenever app launches, not after MainPage is launched. For example, Clash of Clans. Whenever the app launches, the app checks for internet connection and if there's no connection, it displays a alert to user repetitively until there's a connection.
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using System;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Multi
{
public partial class App : Application
{
static GroupStage groupstage = new GroupStage();
public static HtmlWeb web = new HtmlWeb();
public static HtmlDocument doc = LoadUrlAndTestConnection();
//The reason why I have put a method is because I wanted to try if I can use try-catch to display alert, however this didn't work.
public static HtmlDocument LoadUrlAndTestConnection()
{
bool con = true;
while (con)
{
try
{
doc = web.Load(someURL);
}
catch (Exception ex)
{
var sth = new ErrorPage();
sth.InternetErrorDisplay();
con = true;
continue;
}
con = false;
}
return docSK;
}
public static Dictionary<string, Country> _countries = new Dictionary<string, Country>
{
["Australia"] = new Country(1, "Australia", false, "AU", "ausFlag.png", 3, groupstage, GetScore("Australia", 3)),
public static string[] GetScore(string name, int GroupID)
{
//Gets the score data from internet
}
public App()
{
InitializeComponent();
TwitchClass.MainAsync().Wait();
MainPage = new OpeningPage();
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
}
protected override void OnResume()
{
}
}
}
//GetScore method requires internet connection as it gets the score data from internet.
and the InternetErrorDisplay method is,
public void InternetErrorDisplay() => DisplayAlert("Connection Error", "Could not detect internet connection. This application requires access to internet.", "Retry");
Is it possible to have this behaviour in xamarin forms app? How can I achieve it?
Yes, why should it not be possible?
Here is an example which uses async/await
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Threading.Tasks;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace LoadingSample
{
public partial class App : Application
{
public App()
{
InitializeComponent();
//MainPage = new MainPage();
}
protected override async void OnStart()
{
// shows Loading...
MainPage = new LoadPage();
await Task.Yield();
// Handle when your app starts
// Just a simulation with 10 tries to get the data
for (int i = 0; i < 10; i++)
{
await Task.Delay(500);
// await internet_service.InitializeAsync();
await MainPage.DisplayAlert(
"Connection Error",
"Unable to connect with the server. Check your internet connection and try again",
"Try again");
}
await Task.Delay(2000);
// after loading is complete show the real page
MainPage = new MainPage();
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}

Why the variable trigger throws exception?

I'm stuck in declaring a variable with a trigger. This trigger is fired whenever the value of the variable changes and the trigger mechanism is supposed to alter the value of another variable.
Below code compiles fine but throws NullReferenceException (screenshot after exception).
file: Program.cs
using System;
using System.Windows.Forms;
namespace test {
class Program {
public static Active active = new Active();
public static FormMain formMain = new FormMain();
[STAThread]
static void Main() {
Application.Run(formMain);
}
}
}
file: DataStruct.cs
namespace test {
public class Active {
public string UserName {
get {
return (Program.formMain.labelUserName.Text);
}
set {
Program.formMain.labelUserName.Text = value;
}
}
}
}
file FormMain.cs
using System;
using System.Windows.Forms;
namespace test {
class FormMain : Form {
public Label labelUserName = new Label();
public FormMain() {
this.Controls.Add(labelUserName);
Program.active.UserName = "User Name";
}
}
}
It's because when you are in the FormMain constructor, the static variable Program.formMain has not been initialized yet, because you are creating the FormMain object which with you want to initialize Program.formMain.
Update labelUserName directly instead.

Changing a picture box visibility from C# code

So I tried to create a new form and reference it...the compiler didn't mind this but it clearly wasn't changing the visibility of my picturebox. this is how I was calling my method found in my form, FROM my c# script.
Form1 updateForm = new Form1();
updateForm.setLights();
It called the method, and seemed like it worked! Until I read a post about instancing forms, and how by creating a "new" instance of Form1, that anything referenced by my updateForm would not change what I would see on my Form1.
So what I need to do is to call the function in setLights() which is in my Form1, and get it to change the visibility of my image on that form, from my C# code. Please see below (i understand the issue of the instancing problem mentioned above, but I left it in so that hopefully it will give better insight into what I am "trying" to do :) ALSO, please keep in mind that setLightCall() is running in a separate thread. Thanks in advance!
This code is also in my main c# script, and is the main function that I use to call my threads
static void Main(string[] args)
{
Thread FormThread = new Thread(FormCall);
FormThread.Start();
Thread setLightThread = new Thread(setLightCall);
setLightThread.Start();
log4net.Config.XmlConfigurator.Configure();
StartModbusSerialRtuSlave();
}
This code is in my main C# script
public void setLightCall(Form1 parent)
{
Form1 updateForm = new Form1();
while(true)
{
updateForm.setLights();
}
}
The below code is in my form1
public void setLights()
{
Input1GreenLight.Visible = false;
}
Here is an example of what I think you are wanting to try. Note the use of Invoking and delegates to be able to access the PictureBox's Visible method. I had to add the System.Windows.Forms Namespace to the Console Application to be able to access the instance of the Form that was created in the FormThread Method, this is assuming that you only have 1 Form in your FormCollection.
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;
namespace ConsoleApplication59
{
class Program
{
static void Main(string[] args)
{
Thread FormThread = new Thread(FormCall);
FormThread.Start();
Thread.Sleep(2000); //Sleep to allow form to be created
Thread setLightThread = new Thread(setLightCall);
setLightThread.Start(Application.OpenForms[0]); //We can get by with this because just one form
Console.ReadLine();
}
public static void setLightCall(object parent)
{
Form1 updateForm = (Form1)parent;
while (true)
{
updateForm.Invoke(updateForm.setLights, new object[] { false });
}
}
public static void FormCall()
{
Application.Run(new Form1());
}
}
}
Form1
public partial class Form1 : Form
{
public delegate void Lights(bool state);
public Lights setLights;
public Form1()
{
InitializeComponent();
setLights = new Lights(setLightsDelegate);
}
public void setLightsDelegate(bool state)
{
Input1GreenLight.Visible = state;
}
}

MEF. How to load a winform into winform container?

I have decided to play a little bit with MEF2 and net3.5 and I have thought it would be easy but I am stuck now. Generally the idea of my toy is I want to have form containet where I am going to load form extensions and show them. I did this code
My extension:
using System.ComponentModel.Composition;
using System.Windows.Forms;
namespace MyExtantion
{
public interface IForm
{
void LoadForm(Form form);
}
[Export(typeof(IForm))]
public partial class MyExtantion : Form, IForm
{
public MyExtantion()
{
InitializeComponent();
}
public void LoadForm(Form form)
{
MdiParent = form;
Show();
}
}
}
and form container
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.Windows.Forms;
namespace FormsContainer
{
public partial class FormContainer : Form
{
public FormContainer()
{
InitializeComponent();
}
private CompositionContainer _container;
public interface IForm
{
void LoadForm(Form form);
}
[Import(typeof(IForm))]
public IEnumerable Forms { get; set; }
private bool Compose()
{
var catalog = new AggregateCatalog(
new AssemblyCatalog(Assembly.GetExecutingAssembly()),
new DirectoryCatalog("Extantions"));
var batch = new CompositionBatch();
batch.AddPart(this);
_container = new CompositionContainer(catalog);
try
{
_container.Compose(batch);
}
catch (CompositionException compositionException)
{
MessageBox.Show(compositionException.ToString());
return false;
}
return true;
}
private void FormContainer_Load(object sender, EventArgs e)
{
if (Compose())
foreach (IForm form in Forms)
{
form.LoadForm(this);
}
}
}
}
The problem is I can not load my extantion and I have this error
{"The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.\r\n\r\n1) No exports were found that match the constraint '((exportDefinition.ContractName = \"FormsContainer.FormContainer+IForm\") && (exportDefinition.Metadata.ContainsKey(\"ExportTypeIdentity\") && \"FormsContainer.FormContainer+IForm\".Equals(exportDefinition.Metadata.get_Item(\"ExportTypeIdentity\"))))'.\r\n\r\nResulting in: Cannot set import 'FormsContainer.FormContainer.Forms (ContractName=\"FormsContainer.FormContainer+IForm\")' on part 'FormsContainer.FormContainer'.\r\nElement: FormsContainer.FormContainer.Forms (ContractName=\"FormsContainer.FormContainer+IForm\") --> FormsContainer.FormContainer\r\n"}
How I can achieve it with MEF? and What I do wrong?
You are declaring the IForm interface in two different places.
If you only reference one interface that both are using this code works properly.

Categories