-1

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?

Yuliia Ashomok
  • 8,336
  • 2
  • 60
  • 69
  • What do you expect to happen if this arbitrary thread is busy doing something else? – Jon Skeet May 12 '14 at 09:20
  • I never heard od `CG` other than "Computer graphics". Did you mean `GC` ? – Sriram Sakthivel May 12 '14 at 09:21
  • You can call `GC.Collect`, thats about it. http://stackoverflow.com/questions/478167/when-is-it-acceptable-to-call-gc-collect. You can't call Finalizers/Destructors directly, (I think.) Perhaps you want to implement `IDisposable` properly? – Jodrell May 12 '14 at 09:24
  • 2
    This seems to be an XY problem. What problem are you *really* trying to solve. Focus on that instead of a workaround switching the threads. – Thomas Weller May 12 '14 at 09:27
  • Yes, I implement IDisposable. And ~Class(){this.Dispose(false);} works fine but have only one important problem: Program wants load and unload objects in one thread. I want call this.Dispose(false); in main thread but not in separete thread of GC. – Yuliia Ashomok May 12 '14 at 09:30
  • You should be calling `GC.SuppressFinalize()`, when your protected `Dispose` is called via `IDisposable`, then the finalizer will not be called by the Garbage Colletor and your memory will be recovered more optimally. http://stackoverflow.com/questions/3038571/whats-the-purpose-of-gc-suppressfinalizethis-in-dispose-method. Your finalizer and your protected `Dispose` have to deal with cross thread situations and null checks because you can't rely on IDisposable always being used or, on when the finalizer will be called. – Jodrell May 12 '14 at 09:39

3 Answers3

1

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).

TomTom
  • 61,059
  • 10
  • 88
  • 148
0

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.

Itzik Gili
  • 324
  • 3
  • 16
0

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.

Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124