Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two Forms:
In Form1 (Splash Screen) ProgressBar to open Form2.
In Form2 display physical memory (RAM), and it take a time to load this form.

how do the Progressbar reach to (100%) when Form2 opened?
and what Form should load first?

Generally, I want really splash screen, not just propose waiting time.

I tried to use "BackgroundWorker" by this code:

What I have tried:

private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker.RunWorkerAsync();
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
int sum = 0;
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
sum = sum + i;
backgroundWorker.ReportProgress(i);
}
e.Result = sum;
}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progrssBar.Value = e.ProgressPercentage;
}

private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Form2 form = new Form2();
form.Show();
this.Hide();
}
Posted
Updated 12-Apr-21 9:29am
Comments
Dave Kreskowiak 12-Apr-21 17:53pm    
Insufficient information to answer the question. Where is all the time being taken up? Is it in the constructor, or a function it calls, in Form2? If that's the case, whatever that process is must be moved to it's own thread.

Form2 shouldn't be handling digging up data or processing it. It should be completely divorced from the long-running operation. The form should be limited to showing some sampling of the data and/or some edit operation on it.
User_Michel 12-Apr-21 19:02pm    
When open Form2 it take time to show. and that is what in Form2 Load:

labelMemory.Text = GetPhysicalMemory();


public static string GetPhysicalMemory()
{
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oCollection = oSearcher.Get();

long MemSize = 0;
long mCap = 0;

foreach (ManagementObject obj in oCollection)
{
mCap = Convert.ToInt64(obj["Capacity"]);
MemSize += mCap;
}
MemSize = (MemSize / 1024 / 1024) / 1024;
return MemSize.ToString() + " GB";
}
Dave Kreskowiak 12-Apr-21 19:44pm    
Wrong way to do this. That GetPhysicalMemory call should be in it's own Task returning a string. You can then, in some method in Form2, await the call to GetPhysicalMemory and show the string it returns when the await returns.
private async void Form2_Load(object sender, EventArgs e)
{
    PhysicalRamLabel.Text = await GetPhysicalRam();
}

private Task<string> GetPhysicalRam()
{
    return Task.Factory.StartNew<string>(() =>
        {
            ManagementScope scope = new ManagementScope(@"\\.\root\cimv2");
            ObjectQuery query = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection collection = searcher.Get();

            long memSize = 0;
                
            foreach (ManagementObject result in collection)
            {
                memSize += Convert.ToInt64(result["Capacity"]);
            }

            memSize = memSize / 1024 / 1024 / 1024;

            return $"{memSize} GB";
    });
}
User_Michel 13-Apr-21 3:07am    
Thank you a lot. Then what about Windows Activation? it take time also, and that is the code:

public static bool IsWindowsActivated()
{
ManagementScope scope = new ManagementScope(@"\\" + System.Environment.MachineName + @"\root\cimv2");
scope.Connect();

SelectQuery searchQuery = new SelectQuery("SELECT * FROM SoftwareLicensingProduct WHERE ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f' and LicenseStatus = 1");
ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope, searchQuery);

using (ManagementObjectCollection obj = searcherObj.Get())
{
return obj.Count > 0;
}
}
Dave Kreskowiak 13-Apr-21 10:24am    
This query is not different. I already gave you the template. You didn't bother to try to understand what's going on.

1 solution

There's tons of examples on CP knowledge base: SplashScreen[^]
 
Share this answer
 
Comments
User_Michel 12-Apr-21 16:51pm    
Thank you, but unfortunately all this did not help me.

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