Question

I have:

public static Thread MainThread
{
    get { return Thread.CurrentThread; }
}

in my "main" class

I want to do following in other class:

~Class()
{
    Program.MainThread.Start(this.Dispose(false));
}

How can I run method in some thread? How can I run GC in main thread?

Was it helpful?

Solution

How can I run CG in main thread?

GC always runs in it's own threads.

How can I run method in some thread?

You can not unless the other thread has an API entry point waiting for things to process (like the UI has where you can invoke into the message pump).

OTHER TIPS

You can't run a method in the thread remotly. As for GC, so it can work async, it will run in a different thread.

You can't tell the Garbage Collector when to finalize an object or what thread to finalize an object on.

You can tell the Garbage Collector that now would be a good time to collect by calling GC.Collect.

You can tell the Garbage Collector that an object that fully and properly implements IDisposable, does not need to be finalized by calling GC.SuppressFinalize when IDisposable.Dispose is called directly.

Once again, you cannot control when, other than suppressing, or on what thread that an objects finalizer will be run.

Your Finalizer/Destructor and protected Dispose(bool disposing) implementation have to be aware of this limitation and handle cross-thread and null checks appropriately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top